-
Notifications
You must be signed in to change notification settings - Fork 39
/
btrfs-sync-WIP
executable file
·420 lines (349 loc) · 13.7 KB
/
btrfs-sync-WIP
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/bin/bash
set -o errtrace
version="0.0"
name="btrfs-sync"
SNAPPER_CONFIG=/etc/conf.d/snapper
TMPDIR=$(mktemp -d)
PIPE=$TMPDIR/$name.out
mkfifo $PIPE
systemd-cat -t "$name" < $PIPE &
exec 3>$PIPE
donotify=0
which notify-send &> /dev/null
if [[ $? -ne 0 ]]; then
donotify=1
fi
error() {
printf "==> ERROR: %s\n" "$@"
notify_error 'Error' 'Check journal for more information.'
} >&2
die() {
error "$@"
exit 1
}
traperror() {
printf "Exited due to error on line %s.\n" $1
printf "exit status: %s\n" "$2"
printf "command: %s\n" "$3"
printf "bash line: %s\n" "$4"
printf "function name: %s\n" "$5"
exit 1
}
trapkill() {
die "Exited due to user intervention."
}
trap 'traperror ${LINENO} $? "$BASH_COMMAND" $BASH_LINENO "${FUNCNAME[@]}"' ERR
trap trapkill SIGTERM SIGINT
usage() {
cat <<EOF
$name $version
Usage: $name [options]
Options:
-c, --config <config> snapper configuration to backup
-d, --description <desc> snapper description
-h, --help print this message
-n, --noconfirm do not ask for confirmation
-q, --quiet do not send notifications; instead print them.
-s, --subvolid <subvlid> subvolume id of the mounted BTRFS subvolume to back up to
-u, --UUID <UUID> UUID of the mounted BTRFS subvolume to back up to
See 'man snap-sync' for more details.
EOF
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-d|--description)
description="$2"
shift 2
;;
-c|--config)
selected_configs="$2"
shift 2
;;
-u|--UUID)
uuid_cmdline="$2"
shift 2
;;
-s|--subvolid)
subvolid_cmdline="$2"
shift 2
;;
-n|--noconfirm)
noconfirm="yes"
shift
;;
-h|--help)
usage
exit 1
;;
-q|--quiet)
donotify=1
shift
;;
*)
die "Unknown option: '$key'. Run '$name -h' for valid options."
;;
esac
done
notify() {
for u in $(users | tr ' ' '\n' | sort -u); do
sudo -u $u DISPLAY=:0 \
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(sudo -u $u id -u)/bus \
notify-send -a $name "$1" "$2" --icon="dialog-$3"
done
}
notify_info() {
if [[ $donotify -eq 0 ]]; then
notify "$1" "$2" "information"
else
printf "$1: $2\n"
fi
}
notify_error() {
if [[ $donotify -eq 0 ]]; then
notify "$1" "$2" "error"
else
printf "$1: $2\n"
fi
}
[[ $EUID -ne 0 ]] && die "Script must be run as root. See '$name -h' for a description of options"
! [[ -f $SNAPPER_CONFIG ]] && die "$SNAPPER_CONFIG does not exist."
description=${description:-"latest incremental backup"}
uuid_cmdline=${uuid_cmdline:-"none"}
subvolid_cmdline=${subvolid_cmdline:-"5"}
noconfirm=${noconfirm:-"no"}
if [[ "$uuid_cmdline" != "none" ]]; then
notify_info "Backup started" "Starting backups to $uuid_cmdline subvolid=$subvolid_cmdline..."
else
notify_info "Backup started" "Starting backups. Use command line menu to select disk."
fi
if [[ "$(findmnt -n -v --target / -o FSTYPE)" == "btrfs" ]]; then
EXCLUDE_UUID=$(findmnt -n -v -t btrfs --target / -o UUID)
TARGETS=$(findmnt -n -v -t btrfs -o UUID,TARGET --list | grep -v $EXCLUDE_UUID | awk '{print $2}')
UUIDS=$(findmnt -n -v -t btrfs -o UUID,TARGET --list | grep -v $EXCLUDE_UUID | awk '{print $1}')
else
TARGETS=$(findmnt -n -v -t btrfs -o TARGET --list)
UUIDS=$(findmnt -n -v -t btrfs -o UUID --list)
fi
declare -a TARGETS_ARRAY
declare -a UUIDS_ARRAY
declare -a SUBVOLIDS_ARRAY
i=0
for x in $TARGETS; do
SUBVOLIDS_ARRAY[$i]=$(btrfs subvolume show $x | awk '/Subvolume ID:/ { print $3 }')
TARGETS_ARRAY[$i]=$x
i=$((i+1))
done
i=0
disk=-1
disk_count=0
for x in $UUIDS; do
UUIDS_ARRAY[$i]=$x
if [[ "$x" == "$uuid_cmdline" && ${SUBVOLIDS_ARRAY[$((i))]} == "$subvolid_cmdline" ]]; then
disk=$i
disk_count=$(($disk_count+1))
fi
i=$((i+1))
done
if [[ "${#UUIDS_ARRAY[$@]}" -eq 0 ]]; then
die "No external btrfs subvolumes found to backup to. Run '$name -h' for more options."
fi
if [[ "$disk_count" > 1 ]]; then
printf "Multiple mount points were found with UUID %s and subvolid %s.\n" "$uuid_cmdline" "$subvolid_cmdline"
disk="-1"
fi
if [[ "$disk" == -1 ]]; then
if [[ "$disk_count" == 0 && "$uuid_cmdline" != "none" ]]; then
error "A device with UUID $uuid_cmdline and subvolid $subvolid_cmdline was not found to be mounted, or it is not a BTRFS device."
fi
printf "Select a mounted BTRFS device on your local machine to backup to.\nFor more options, exit and run '$name -h'.\n"
while [[ $disk -lt 0 || $disk -gt $i ]]; do
for x in "${!TARGETS_ARRAY[@]}"; do
printf "%4s) %s (uuid=%s, subvolid=%s)\n" "$((x+1))" "${TARGETS_ARRAY[$x]}" "${UUIDS_ARRAY[$x]}" "${SUBVOLIDS_ARRAY[$x]}"
done
printf "%4s) Exit\n" "0"
read -e -r -p "Enter a number: " disk
if ! [[ $disk == ?(-)+([0-9]) ]] || [[ $disk -lt 0 || $disk -gt $i ]]; then
printf "\nNo disk selected. Select a disk to continue.\n"
disk=-1
fi
done
if [[ $disk == 0 ]]; then
exit 0
fi
disk=$(($disk-1))
fi
selected_subvolid="${SUBVOLIDS_ARRAY[$((disk))]}"
selected_uuid="${UUIDS_ARRAY[$((disk))]}"
selected_mnt="${TARGETS_ARRAY[$((disk))]}"
printf "\nYou selected the disk with uuid=%s, subvolid=%s.\n" "$selected_uuid" "$selected_subvolid" | tee $PIPE
printf "The disk is mounted at '%s'.\n" "$selected_mnt" | tee $PIPE
source $SNAPPER_CONFIG
if [[ -z $selected_configs ]]; then
printf "\nInteractively cycling through all snapper configurations...\n"
fi
selected_configs=${selected_configs:-$SNAPPER_CONFIGS}
declare -a BACKUPDIRS_ARRAY
declare -a MYBACKUPDIR_ARRAY
declare -a OLD_NUM_ARRAY
declare -a OLD_SNAP_ARRAY
declare -a NEW_NUM_ARRAY
declare -a NEW_SNAP_ARRAY
declare -a NEW_INFO_ARRAY
declare -a BACKUPLOC_ARRAY
declare -a CONT_BACKUP_ARRAY
# Initial configuration of where backup directories are
i=0
for x in $selected_configs; do
if [[ "$(snapper -c $x list -t single | awk '/'"subvolid=$selected_subvolid, uuid=$selected_uuid"'/ {cnt++} END {print cnt}')" -gt 1 ]]; then
error "More than one snapper entry found with UUID $selected_uuid subvolid $selected_subvolid for configuration $x. Skipping configuration $x."
continue
fi
if [[ "$(snapper -c $x list -t single | awk '/'$name' backup in progress/ {cnt++} END {print cnt}')" -gt 0 ]]; then
printf "\nNOTE: Previous failed %s backup snapshots found for '%s'.\n" "$name" "$x" | tee $PIPE
if [[ $noconfirm == "yes" ]]; then
printf "'noconfirm' option passed. Failed backups will not be deleted.\n" | tee $PIPE
else
read -e -r -p "Delete failed backup snapshot(s)? (These local snapshots from failed backups are not used.) [y/N]? " delete_failed
while [[ -n "$delete_failed" && "$delete_failed" != [Yy]"es" &&
"$delete_failed" != [Yy] && "$delete_failed" != [Nn]"o" &&
"$delete_failed" != [Nn] ]]; do
read -e -r -p "Delete failed backup snapshot(s)? (These local snapshots from failed backups are not used.) [y/N] " delete_failed
if [[ -n "$delete_failed" && "$delete_failed" != [Yy]"es" &&
"$delete_failed" != [Yy] && "$delete_failed" != [Nn]"o" &&
"$delete_failed" != [Nn] ]]; then
printf "Select 'y' or 'N'.\n"
fi
done
if [[ "$delete_failed" == [Yy]"es" || "$delete_failed" == [Yy] ]]; then
snapper -c $x delete $(snapper -c $x list | awk '/'$name' backup in progress/ {print $1}')
fi
fi
fi
SNAP_SYNC_EXCLUDE=no
if [[ -f "/etc/snapper/configs/$x" ]]; then
source /etc/snapper/configs/$x
# TODO: snapper -c "$x" --jsonout get-config
else
die "Selected snapper configuration $x does not exist."
fi
if [[ $SNAP_SYNC_EXCLUDE == "yes" ]]; then
continue
fi
printf "\n"
old_num=$(snapper -c "$x" list -t single | awk '/'"subvolid=$selected_subvolid, uuid=$selected_uuid"'/ {print $1}')
old_snap=$SUBVOLUME/.snapshots/$old_num/snapshot
OLD_NUM_ARRAY[$i]=$old_num
OLD_SNAP_ARRAY[$i]=$old_snap
if [[ -z "$old_num" ]]; then
printf "No backups have been performed for '%s' on this disk.\n" "$x"
read -e -r -p "Enter name of subvolume to store backups, relative to $selected_mnt (to be created if not existing): " mybackupdir
printf "This will be the initial backup for snapper configuration '%s' to this disk. This could take awhile.\n" "$x"
BACKUPDIR="$selected_mnt/$mybackupdir"
test -d "$BACKUPDIR" || btrfs subvolume create "$BACKUPDIR"
else
mybackupdir=$(snapper -c "$x" list -t single | awk -F"|" '/'"subvolid=$selected_subvolid, uuid=$selected_uuid"'/ {print $5}' | awk -F "," '/backupdir/ {print $1}' | awk -F"=" '{print $2}')
BACKUPDIR="$selected_mnt/$mybackupdir"
test -d $BACKUPDIR || die "%s is not a directory on %s.\n" "$BACKUPDIR" "$selected_uuid"
fi
BACKUPDIRS_ARRAY[$i]="$BACKUPDIR"
MYBACKUPDIR_ARRAY[$i]="$mybackupdir"
printf "Creating new local snapshot for '%s' configuration...\n" "$x" | tee $PIPE
# new_num=$(snapper -c "$x" create --print-number -d "$name backup in progress")
new_num=TODO
new_snap=$SUBVOLUME/.snapshots/$new_num/snapshot
new_info=$SUBVOLUME/.snapshots/$new_num/info.xml
sync
backup_location=$BACKUPDIR/$x/$new_num/
printf "Will backup %s to %s\n" "$new_snap" "$backup_location/snapshot" | tee $PIPE
if (test -d "$backup_location/snapshot") ; then
printf "WARNING: Backup directory '%s' already exists. This configuration will be skipped!\n" "$backup_location/snapshot" | tee $PIPE
printf "Move or delete destination directory and try backup again.\n" | tee $PIPE
fi
NEW_NUM_ARRAY[$i]="$new_num"
NEW_SNAP_ARRAY[$i]="$new_snap"
NEW_INFO_ARRAY[$i]="$new_info"
BACKUPLOC_ARRAY[$i]="$backup_location"
cont_backup="K"
CONT_BACKUP_ARRAY[$i]="yes"
if [[ $noconfirm == "yes" ]]; then
cont_backup="yes"
else
while [[ -n "$cont_backup" && "$cont_backup" != [Yy]"es" &&
"$cont_backup" != [Yy] && "$cont_backup" != [Nn]"o" &&
"$cont_backup" != [Nn] ]]; do
read -e -r -p "Proceed with backup of '$x' configuration [Y/n]? " cont_backup
if [[ -n "$cont_backup" && "$cont_backup" != [Yy]"es" &&
"$cont_backup" != [Yy] && "$cont_backup" != [Nn]"o" &&
"$cont_backup" != [Nn] ]]; then
printf "Select 'Y' or 'n'.\n"
fi
done
fi
if [[ "$cont_backup" != [Yy]"es" && "$cont_backup" != [Yy] && -n "$cont_backup" ]]; then
CONT_BACKUP_ARRAY[$i]="no"
printf "Not backing up '%s' configuration.\n" $x
# snapper -c $x delete $new_num
fi
i=$(($i+1))
done
# Actual backing up
printf "\nPerforming backups...\n" | tee $PIPE
i=-1
for x in $selected_configs; do
i=$(($i+1))
SNAP_SYNC_EXCLUDE=no
if [[ -f "/etc/snapper/configs/$x" ]]; then
source /etc/snapper/configs/$x
else
die "Selected snapper configuration $x does not exist."
fi
cont_backup=${CONT_BACKUP_ARRAY[$i]}
if [[ $cont_backup == "no" || $SNAP_SYNC_EXCLUDE == "yes" ]]; then
notify_info "Backup in progress" "NOTE: Skipping $x configuration."
continue
fi
notify_info "Backup in progress" "Backing up $x configuration."
printf "\n"
old_num="${OLD_NUM_ARRAY[$i]}"
old_snap="${OLD_SNAP_ARRAY[$i]}"
BACKUPDIR="${BACKUPDIRS_ARRAY[$i]}"
mybackupdir="${MYBACKUPDIR_ARRAY[$i]}"
new_num="${NEW_NUM_ARRAY[$i]}"
new_snap="${NEW_SNAP_ARRAY[$i]}"
new_info="${NEW_INFO_ARRAY[$i]}"
backup_location="${BACKUPLOC_ARRAY[$i]}"
if (test -d "$backup_location/snapshot") ; then
printf "ERROR: Backup directory '%s' already exists. Skipping backup of this configuration!\n" "$backup_location/snapshot" | tee $PIPE
continue
fi
mkdir -p $backup_location
if [[ -z "$old_num" ]]; then
printf "Sending first snapshot for '%s' configuration...\n" "$x" | tee $PIPE
# btrfs send "$new_snap" | btrfs receive "$backup_location" &>/dev/null
else
printf "Sending incremental snapshot for '%s' configuration...\n" "$x" | tee $PIPE
# Sends the difference between the new snapshot and old snapshot to the
# backup location. Using the -c flag instead of -p tells it that there
# is an identical subvolume to the old snapshot at the receiving
# location where it can get its data. This helps speed up the transfer.
# btrfs send -c "$old_snap" "$new_snap" | btrfs receive "$backup_location"
# printf "Modifying data for old local snapshot for '%s' configuration...\n" "$x" | tee $PIPE
# snapper -v -c "$x" modify -d "old snap-sync snapshot (you may remove)" -u "backupdir=,subvolid=,uuid=" -c "number" "$old_num"
fi
cp "$new_info" "$backup_location"
# It's important not to change this userdata in the snapshots, since that's how
# we find the previous one.
# userdata="backupdir=$mybackupdir, subvolid=$selected_subvolid, uuid=$selected_uuid"
# Tag new snapshot as the latest
# printf "Tagging local snapshot as latest backup for '%s' configuration...\n" "$x" | tee $PIPE
# snapper -v -c "$x" modify -d "$description" -u "$userdata" "$new_num"
printf "Backup complete for '%s' configuration.\n" "$x" > $PIPE
done
printf "\nDone!\n" | tee $PIPE
exec 3>&-
if [[ "$uuid_cmdline" != "none" ]]; then
notify_info "Finished" "Backups to $uuid_cmdline complete!"
else
notify_info "Finished" "Backups complete!"
fi