-
Notifications
You must be signed in to change notification settings - Fork 6
/
snapper-rollback.py
executable file
·170 lines (146 loc) · 5.03 KB
/
snapper-rollback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env -S python3
# -*- coding: utf-8 -*-
"""
Script to rollback to snapper snapshot using the layout proposed in the snapper
archwiki page
https://wiki.archlinux.org/index.php/Snapper#Suggested_filesystem_layout
"""
from datetime import datetime
import argparse
import btrfsutil
import configparser
import logging
import os
import pathlib
import sys
LOG = logging.getLogger()
LOG.setLevel("INFO")
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
ch = logging.StreamHandler()
ch.setFormatter(formatter)
LOG.addHandler(ch)
def parse_args():
parser = argparse.ArgumentParser(
description="Rollback to snapper snapshot based on snapshot ID",
)
parser.add_argument(
"snap_id", metavar="SNAPID", type=str, help="ID of snapper snapshot"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="don't actually do anything, just print the actions out",
)
parser.add_argument(
"-c",
"--config",
type=str,
default="/etc/snapper-rollback.conf",
help="configuration file to use (default: /etc/snapper-rollback.conf)",
)
args = parser.parse_args()
return args
def read_config(configfile):
config = configparser.ConfigParser()
config.read(configfile)
return config
def ensure_dir(dirpath, dry_run=False):
if not os.path.isdir(dirpath):
try:
if dry_run:
LOG.info("mkdir -p '{}'".format(dirpath))
else:
os.makedirs(dirpath)
except OSError as e:
LOG.fatal("error creating dir '{}': {}".format(dirpath, e))
raise
def mount_subvol_id5(target, source=None, dry_run=False):
"""
There is no built-in `mount` function in python, let's shell out to an `os.system` call
Also see https://stackoverflow.com/a/29156997 for a cleaner alternative
"""
ensure_dir(target, dry_run=dry_run)
if not os.path.ismount(target):
shellcmd = "mount -o subvolid=5 {} {}".format(source or "", target)
if dry_run:
LOG.info(shellcmd)
ret = 0
else:
ret = os.system(shellcmd)
if ret != 0:
raise OSError("unable to mount {}".format(target))
def rollback(subvol_main, subvol_main_newname, subvol_rollback_src, dev, dry_run=False):
"""
Rename linux root subvolume, then create a snapshot of the subvolume to
the old linux root location
"""
try:
if dry_run:
LOG.info("mv {} {}".format(subvol_main, subvol_main_newname))
LOG.info(
"btrfs subvolume snapshot {} {}".format(
subvol_rollback_src, subvol_main
)
)
LOG.info("btrfs subvolume set-default {}".format(subvol_main))
else:
os.rename(subvol_main, subvol_main_newname)
btrfsutil.create_snapshot(subvol_rollback_src, subvol_main)
btrfsutil.set_default_subvolume(subvol_main)
LOG.info(
"{}Rollback to {} complete. Reboot to finish".format(
"[DRY-RUN MODE] " if dry_run else "", subvol_rollback_src
)
)
except FileNotFoundError as e:
LOG.fatal(
f"Missing {subvol_main}: Is {dev} mounted with the option subvolid=5?"
)
except btrfsutil.BtrfsUtilError as e:
# Handle errors from btrfs utilities
LOG.error(f"{e}")
# Restore old linux root if btrfs utilities fail
if not os.path.isdir(subvol_main):
LOG.info(f"Moving {subvol_main_newname} back to {subvol_main}")
if dry_run:
LOG.warning("mv {} {}".format(subvol_main_newname, subvol_main))
else:
os.rename(subvol_main_newname, subvol_main)
def main():
args = parse_args()
config = read_config(args.config)
mountpoint = pathlib.Path(config.get("root", "mountpoint"))
subvol_main = mountpoint / config.get("root", "subvol_main")
subvol_rollback_src = (
mountpoint / config.get("root", "subvol_snapshots") / args.snap_id / "snapshot"
)
try:
dev = config.get("root", "dev")
except configparser.NoOptionError as e:
dev = None
confirm_typed_value = "CONFIRM"
try:
confirmation = input(
f"Are you SURE you want to rollback? Type '{confirm_typed_value}' to continue: "
)
if confirmation != confirm_typed_value:
LOG.fatal("Bad confirmation, exiting...")
sys.exit(0)
except KeyboardInterrupt as e:
sys.exit(1)
date = datetime.now().strftime("%Y-%m-%dT%H:%M")
subvol_main_newname = pathlib.Path(f"{subvol_main}{date}")
try:
mount_subvol_id5(mountpoint, source=dev, dry_run=args.dry_run)
rollback(
subvol_main,
subvol_main_newname,
subvol_rollback_src,
dev,
dry_run=args.dry_run,
)
except PermissionError as e:
LOG.fatal("Permission denied: {}".format(e))
exit(1)
if __name__ == "__main__":
main()