-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_btrfs_timeshift.sh
executable file
·294 lines (251 loc) · 7.86 KB
/
backup_btrfs_timeshift.sh
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
#! /usr/bin/env zsh
#set -x
# Todo: dry-run
VERBOSE=
VVERBOSE=
MOUNT=
DELETE=true
QUIET=
DEBUG=
DRY=
SUBVOL_PATTERN="@*"
SYNC_DEST="/mnt/backup-timeshift"
# --- ARGUMENT PARSING ---
for i in "$@"
do
case $i in
--help|-h)
echo "-h, --help Shows this help message and exits"
echo "-r=<ROOT>, --root=<ROOT> Sets root from where to backup (default: /run/timeshift/PID/backup/timeshift-btrfs/snapshots)"
echo "-d=<DEST>, --destination=<DEST> Sets root wherre to save the copy of the snapshots (default: /mnt/backup-timeshift)"
echo "-v,--verbose Enables verbose output"
echo "-vb,--vbrtfs Enables verbose output for btrfs send/receive"
echo "--force-mount Forces script to mount <DEST> first"
echo "--no-delete Does not sync deletion of snapshot at <ROOT>. Does delete obsolute readonly subvolume at <ROOT>"
echo "-q,--quiet Supresses output to a minimum"
echo "--subvol=@<subv> Only backup specified subvolume"
echo "--subvol-pattern=<pattern> Only backup subvolumes matching the shell pattern (default: @*)"
#echo "--dry-run Will only simulate backup process without altering any files" # WIP
exit 0
shift
;;
--root=*|-r=*)
ROOT="${i#*=}"
shift
;;
--dest*=*|-d=*)
SYNC_DEST="${i#*=}"
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
-vb|--vbtrfs)
VVERBOSE=true
shift
;;
--force-mount)
MOUNT=true
shift
;;
--no-delete)
DELETE=
shift
;;
-q|--quiet)
QUIET=true
shift
;;
--dry-run)
DRY=true
shift
;;
--debug)
DEBUG=true
shift
;;
--subvol=@*)
subv="${i#*=}"
shift
;;
--subvol-pattern=*)
SUBVOL_PATTERN="${i#*=}"
shift
;;
*)
echo "\e[1m\e[31mERROR: \e[0mUnknown argument detected: \"$i\""
exit 2
;;
esac
done
ROOT="${ROOT:=/run/timeshift/$(pgrep timeshift-gtk)/backup/timeshift-btrfs/snapshots}"
## HELPERS ##
err() {
echo "\e[1m\e[31mERROR: \e[0m$@"
exit 1
}
indent() {
sed 's/^/ /'
}
log() {
[ ! $QUIET ] && echo "$@"
return 0
}
logv() {
{ [ $VERBOSE ] || [ $VVERBOSE ] ; } && echo "$@"
return 0
}
logvv() {
[ $VVERBOSE ] && echo "$@"
return 0
}
## END HELPERS ##
function umount_dest() {
if [ $MOUNT ]
then
logv "Unmount and removing created mountpoint"
sleep 3
umount "$SYNC_DEST" && rmdir "$SYNC_DEST"
log "Finished."
fi
}
# a snapshot is deleted if original DIRECTORY by timeshift isn't present anymore
# WARNING: this does NOT cover partial deletion, e.g. only deleting @home but leaving @
function snapshot_deleted() {
! [ -d "$ROOT/$(basename $1)" ]
}
function backup_incremental() {
CRITICAL="$3/$(basename $2)"
[ $DEBUG ] && set -x
btrfs $(logvv "-v") send -p "$1" "$2" | btrfs $(logvv "-v") receive "$3" # todo indent output
[ $DEBUG ] && set +x
unset CRITICAL
}
function backup() {
CRITICAL="$2/$(basename $1)"
[ $DEBUG ] && set -x
btrfs $(logvv "-v") send "$1" | btrfs $(logvv "-v") receive "$2" # todo indent output
[ $DEBUG ] && set +x
unset CRITICAL
}
function sync_subv_deletion() {
local subdir
[ $DELETE ] && log "$(tput bold)${subv}$(tput sgr0) Syncing deletion of deleted timeshift backups:"
! [ $DELETE ] && logv "$(tput bold)${subv}$(tput sgr0) Syncing deletion of readonly backups at destination only:"
for subdir in "$ROOT/../readonly/"*
do
if snapshot_deleted "$subdir" && [ -d "$subdir/$subv" ]
then
logv "Deleting $(basename $subdir)..." | indent
btrfs subvolume delete "$subdir/$subv" | indent
[ $DELETE ] && btrfs subvolume delete "$SYNC_DEST/snapshots/$(basename $subdir)/$subv" | indent
fi
done
log " "
}
function sync_subv() {
local subdir
local past_subdir
log "$(tput bold)${subv}$(tput sgr0) Syncing timeshift backups:"
# iterate over all subdirs of $ROOT to find the $subvol
for subdir in "$ROOT/"*
do
# subvol not present e.g. later enabled backup of @home
if ! [ -d "$subdir/$subv" ]
then
logv " Skipping since $subv not present in $subdir"
continue
fi
local readonly_subdir="$ROOT/../readonly/$(basename $subdir)"
if [ -d "$SYNC_DEST/snapshots/$(basename $subdir)/$subv" ] && [ -d "$readonly_subdir/$subv" ]
then
logv "Skipping already synced snapshot '$(basename $subdir)'" | indent
past_subdir="$readonly_subdir"
continue
fi
# readonly subv exists
if [ -d "$readonly_subdir/$subv" ]
then
logvv "Readonly snapshot '$(basename $subdir)' already exists." | indent
else
logv "Creating readonly snapshot of '$(basename $subdir)'" | indent
mkdir -p "$readonly_subdir"
CRITICAL="$readonly_subdir/$subv"
btrfs subvolume snapshot -r "$subdir/$subv" "$readonly_subdir/$subv" | indent
unset CRITICAL
fi
# if readonly is synced
if [ -d "$SYNC_DEST/snapshots/$(basename $subdir)/$subv" ]
then
logvv "Readonly already synced" | indent
else
log "Syncing '$readonly_subdir' to '$SYNC_DEST'..." | indent
mkdir -p "$SYNC_DEST/snapshots/$(basename $subdir)"
if [ -d "$past_subdir/$subv" ]
then
logv "Creating incremental backup of $(basename $subdir)" | indent
backup_incremental "$past_subdir/$subv" "$readonly_subdir/$subv" "$SYNC_DEST/snapshots/$(basename $subdir)"
else
backup "$readonly_subdir/$subv" "$SYNC_DEST/snapshots/$(basename $subdir)"
fi
fi
past_subdir="$readonly_subdir"
done
log " "
}
function cleanup() {
local subdir
log "Cleaning up left over directorys:"
for subdir in "$ROOT/../readonly/"*
do
if snapshot_deleted "$subdir"
then
logv "Deleting $(basename $subdir)" | indent
rmdir $subdir | indent
rmdir "$SYNC_DEST/snapshots/$(basename $subdir)" | indent
fi
done
}
function ihandler() {
# delete snapshot if in critical section
[ $CRITICAL ] && btrfs subv del "$CRITICAL"
umount_dest
exit 130
}
## MAIN ##
# check for privileges first
[ "$(id -u)" -ne 0 ] \
&& err "This utility needs to run as root to create btrfs subvolumes!"
# check if root & dest do exist
[ -d "$ROOT" ] || err "Folder '$ROOT' doesn't exist!"
# mount sync destination
if ! [ -d "$SYNC_DEST" ] || [ $MOUNT ] # if dest doesn't exist or forced mount
then
{ mkdir -p "$SYNC_DEST" && mount "$SYNC_DEST" } \
|| err "Folder '$SYNC_DEST' doesn't exist or can't be mounted!"
MOUNT=true # set mount to true if not already by forced mount
logv "Mounted '$SYNC_DEST'"
fi
trap "ihandler" INT HUP TERM QUIT
if [ $subv ] # only perform backup of specific subvolume (e.g. @home)
then
sync_subv_deletion
sync_subv
else
declare -A synced_subv
# detect all subvolumes of which there are backups
# subv = [@, @home, @var, ...]
for subv in $(find "$ROOT" -maxdepth 2 -mindepth 2 -type d -iname "$SUBVOL_PATTERN" -exec basename {} \;)
do
# skip already iterated subvolume prefixes
[ -n "${synced_subv[$subv]}" ] && continue
sync_subv_deletion
sync_subv
# to suppress multiple syncing attempts for the same prefix (@, @home...)
synced_subv+=([$subv]=1)
done
fi
cleanup
umount_dest
trap -