-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapper_sync.py
executable file
·275 lines (254 loc) · 10.4 KB
/
snapper_sync.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# snapper_sync
# Transfers all snapper snapshots from the local drive to a backup disk.
# Both drives have to be btrfs drives as this uses btrfs send/receive.
# Example use case:
# * laptop backup to an external disk
# * you could execute this script on usb attach ( triggered by udev / dbus / whatever)
# Supply a config file with similar content:
# ATT: pathes are relative to the mount point
# [root]
# source_mountpoint = "/"
# source_path = ".snapshots"
# target_mountpoint = "/var/run/media/awerner/btrfs_backup"
# target_path = "backup/root"
# target_uuid = "ed918d69-3e7a-4798-bf83-cb9ad49b6d10"
# target_min_space = 20
# Important notes:
# * This is a script, every information is extracted from the source
# and target drives at each run.
# * You need to be root, because it involves btrfs send/receive
# * This script depends on the folder structure created by snapper
# TODO:
# * backup over the network, involving intermediate files and scp
# * get the snapshot list from snapper instead of btrfs -> use python bindings of snapper
# * check what happens when this is interrupted by a power loss/ target disk removal?
# * purge snapshots with a similar mechanism to snapper
# * check if this could be integrated into snapper - one daemon which does snapshots and
# transfers them if the disk is attached
# * use btrfs send | pv -L N | btrfs receive to limit the transfer rate when this is a backgroup job
import re
import subprocess
import optparse
import ConfigParser
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="be verbose")
parser.add_option("--dry-run", action="store_true", dest="dry_run", help ="do not execute any commands")
parser.add_option("-c","--config",dest="config",help="which config section from the config file to use, default=all")
(options, args) = parser.parse_args()
if len(args)==0:
print "Supply one or more valid config file(s) as argument"
quit(-1)
try:
config = ConfigParser.ConfigParser()
configfiles = config.read(args)
if options.verbose:
print "Processed config files:",configfiles
except:
print "Could not parse config file"
quit(-1)
# process multiple sections / .snapshots directories
if not options.config:
mysections = config.sections()
else:
mysections = [ options.config ]
error = 0
for mysection in mysections:
if options.verbose:
print "Processing config",mysection
source_mountpoint = config.get(mysection, "source_mountpoint")
source_path = config.get(mysection, "source_path")
target_mountpoint = config.get(mysection, "target_mountpoint")
target_path = config.get(mysection, "target_path")
target_uuid = config.get(mysection, "target_uuid")
target_min_space = config.get(mysection, "target_min_space")
if options.verbose:
print "Setting from config file"
print "source_mountpoint=",source_mountpoint
print "source_path=",source_path
print "target_mountpoint=",target_mountpoint
print "target_path=",target_path
print "target_uuid=",target_uuid
print "target_min_space=",target_min_space
# check if correct backup storage is attached & mounted using the given uuid
try:
mounts_file= open('/proc/mounts')
mounts = mounts_file.read()
myre = r"(\S+) "+target_mountpoint
if options.verbose:
print "Checking /proc/mounts for",myre
my_device = re.search(myre,mounts).group(1)
blkid_out = subprocess.check_output(["/sbin/blkid",my_device])
uuid = re.search(r"UUID=\"(.*?)\"",blkid_out).group(1)
except:
print "Could not determine of the correct medium is mounted at target_path="+target_path
error = -1
continue
if uuid!=target_uuid:
print "Wrong medium is mounted at target_path="+target_path
error = -1
continue
# check if there is enough space on the target disk
try:
# TODO: there may be a better way to check this
btrfs_df_output = subprocess.check_output("btrfs fi show "+my_device,shell=True)
res = re.search(r"size ([\d.]+)\S+ used ([\d.]+)\S+ path (\S+)",btrfs_df_output)
# TODO: hopefully the units are the same
freespace_m = float(res.group(1))-float(res.group(2))
if options.verbose:
print "Free space on target disk", freespace_m
if freespace_m < float(target_min_space):
print "Not enough spaced left on target disk (target_min_space)"
error = -1
continue
except:
print "Could not determine free space on target disk"
def parse_btrfs_subvolume_list(raw):
table = []
for line in raw.split('\n'):
if len(line.strip())==0: continue
match = re.match(r"ID (\S+) .* uuid (\S+) path (\S+)",line.strip())
if match == None:
print "Failed to parse btrfs subvolume list output"
quit(-1)
snapper_id_ = ""
try:
snapper_id_ = int(re.search(r"/(\d+)/snapshot",match.group(3)).group(1))
except:
snapper_id_ = -1
class entry:
def __repr__(self):
return str(self)
def __str__(self):
mystr = "[ "+btrfs_id+" | "+ uuid+" | "+ path + " | "+snapper_id+" ]"
return mystr
btrfs_id = match.group(1)
uuid = match.group(2)
path = match.group(3)
snapper_id = snapper_id_
table.append(entry)
return table
def get_source_uuid_tag(path):
try:
from lxml import etree
tag_file = target_mountpoint + "/" +re.search(r"(\S+)/snapshot",path).group(1)+"/info.xml"
tag_file_content = open(tag_file).read()
xml = etree.XML(tag_file_content)
return filter(lambda z: z.tag=="source_uuid", xml)[0].text
except:
return -1
# check which snapshots are present on the source drive
source_snaps_raw = subprocess.check_output(["btrfs","subvolume","list","-s","-u",source_mountpoint])
source_snaps = parse_btrfs_subvolume_list(source_snaps_raw)
source_snaps = filter(lambda x: not x.path.find(source_path+"/"),source_snaps)
if options.verbose:
print "Found snapshots in source_path:"
for i in source_snaps:
print i.path,"/",i.snapper_id
# check which snapshots are present on the target drive
target_snaps_raw = subprocess.check_output(["btrfs","subvolume","list","-a","-u",target_mountpoint])
target_snaps = parse_btrfs_subvolume_list(target_snaps_raw)
target_snaps = filter(lambda x: not x.path.find(target_path+"/"),target_snaps)
for snap in target_snaps:
snap.source_uuid = get_source_uuid_tag(snap.path)
if options.verbose:
print "Found snapshots in target_path:"
for i in target_snaps:
print i.path,"/",i.snapper_id,"/",i.source_uuid
# find common snapshots -> list of clone-sources
# find missing snapshots -> snapshots to be transfered
common_snaps = []
source_only_snaps = []
for snap in source_snaps:
if snap.snapper_id==-1:
continue
is_common = False
for other_snap in target_snaps:
if snap.snapper_id == other_snap.snapper_id:
if snap.uuid!=other_snap.source_uuid:
print "Error: snapshot uuid of a snapshot present on both drives do not match:"
print source_mountpoint+"/"+snap.path+":"+snap.uuid
print target_mountpoint+"/"+other_snap.path+":"+str(other_snap.source_uuid)
error = -1
quit(-1) # TODO: propper error handling
common_snaps.append(snap)
is_common = True
break
if not is_common:
source_only_snaps.append(snap)
common_snaps.sort(lambda x,y: x.snapper_id < y.snapper_id)
source_only_snaps.sort(lambda x,y: x.snapper_id < y.snapper_id)
if options.verbose:
print "Common snapshots:"
print [ snap.snapper_id for snap in common_snaps ]
print "Snapshots to be transfered:"
print [ snap.snapper_id for snap in source_only_snaps ]
# use btrfs send/receive to transfer the new snapshot(s) to the target drive
# TODO: specifing all existing snapshots as clone sources, if multiple snapshots
# to transfer, extend list of clone-source after each transfer
# DOES NOT WORK YET - strange error message
for snap in source_only_snaps:
if options.verbose:
print "Processing snapshot ",str(snap.snapper_id)
new_folder = target_mountpoint+"/"+target_path+"/"+str(snap.snapper_id)
if options.verbose:
print "Creating folder",new_folder," to store snapshot ",str(snap.snapper_id)
if not options.dry_run:
retval = subprocess.call("mkdir "+new_folder,shell=True)
if retval != 0:
print "New folder",new_folder, " could not be created"
error = -1
continue
if options.verbose:
print "Saving source uuid to info.xml"
if not options.dry_run:
from lxml import etree
xml = etree.Element("snapshot")
source_uuid_tag = etree.Element("source_uuid")
source_uuid_tag.text = snap.uuid
xml.append(source_uuid_tag)
snapper_sync_tag = etree.Element("snapper_sync")
snapper_sync_tag.text = "1"
xml.append(snapper_sync_tag)
tag_file = open(new_folder+"/info.xml","w")
tag_file.write(etree.tostring(xml,pretty_print=True,xml_declaration=True))
clone_cmdline = ""
#using multiple clone sources
#for clone in common_snaps:
# clone_cmdline = clone_cmdline + "-c " + source_mountpoint + "/" + source_path + "/" + str(clone) + "/snapshot "
#using a single parent snapshot
previous_snap = -1
for clone in common_snaps:
if clone.snapper_id < snap.snapper_id and clone.snapper_id > previous_snap:
previous_snap = clone.snapper_id
clone_cmdline = "-p "+source_mountpoint+"/"+source_path+"/"+str(clone.snapper_id)+"/snapshot "
if options.verbose:
if previous_snap != -1:
print "Found parent snapshot ",str(previous_snap)," for this snapshot"
else:
print "Found no common parent snapshot for this snapshot"
cmdline = "btrfs send " + clone_cmdline + source_mountpoint + "/" + source_path + "/" + str(snap.snapper_id) + "/snapshot | btrfs receive " + target_mountpoint + "/" + target_path + "/" + str(snap.snapper_id)
if options.verbose:
print cmdline
if not options.dry_run:
retval = subprocess.call(cmdline,shell=True)
if retval != 0:
print "Problem detected"
error = -1
continue
common_snaps.append(snap)
# use kernel io priorities or something like that to prevent locking up the system
# or block the external drive
quit(error)