-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2backup.sh
executable file
·371 lines (300 loc) · 9.72 KB
/
2backup.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
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
#!/usr/bin/env bash
# 2backup (btrfs backup), a simple snapshots management and backup tool for btrfs.
# Copyright (C) 2023 Babilinx
#
# 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 <https://www.gnu.org/licenses/>.
usage() {
cat << EOF
2backup Copyright (C) 2023 Babilinx
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see 'https://www.gnu.org/licenses/gpl-3.0.en.html' for details.
Usage:
2backup [options] [arguments]
bb [options] [arguments]
Options:
profile Interact with profiles.
arguments:
create <profile> Create a profile.
delete <profile> Delete a profile.
list List all profiles.
show <profile> Show infos of a profile.
snapshot Interact with snapshots.
arguments:
create <profile> Create a snapshot of a profile, with the description given after.
create <profile> -m <description> Create a snapshot of a profile with the giver description.
delete <snapshot ID> -p <profile> Delete a profile's snapshot (can contain more than one snapshot).
delete <snapshot hash> Delete a given snapshot.
list List all snapshots.
list -p <profile> List all snapshots of a profile.
show <snapshot hash> Show infos on a snapshot.
show <snapshot ID> -p <profile> Show infos on a snapshot of a profile.
mount <snapshot ID> -p <profile> Mount a snapshot of a profile.
mount <snapshot hash> Mount a given snapshot.
umount <snapshot ID> -p <profile> Unmount a snapshot of a profile.
umount <snapshot hash> Unmount a given snapshot.
rollback Do rollback stuff.
arguments:
<snapshot hash> Rollback the given snapshot.
<snapshot ID> -p <profile> Rollback a profile to the given snapshot.
EOF
}
init() {
[ ! -d "/etc/2backup" ] && mkdir /etc/2backup
[ ! -d "/etc/2backup/profiles" ] && mkdir /etc/2backup/profiles
}
profile_create() {
if [[ ! "$1" == "" ]]; then
PROFILE="$1"
cat > /etc/2backup/profiles/$PROFILE << "EOF"
SUBV=("@default1" "@default2")
SUBV_MNTPT=("/default1" "/default1/default2")
TIMELINE_LIMIT_HOURLY="0"
TIMELINE_LIMIT_DAILY="0"
TIMELINE_LIMIT_WEEKLY="0"
TIMELINE_LIMIT_MONTHLY="0"
TIMELINE_LIMIT_YEARLY="0"
EOF
echo "Profile '$PROFILE' created. Configure it at '/etc/2backup/profiles/$PROFILE'."
exit
else
>&2 echo "Profile name can't be blank!"
exit 1
fi
}
profile_delete() {
PROFILE="$1"
if [ -e "/etc/2backup/profiles/$PROFILE" ]; then
read -p "Do you really want to delete profile '$PROFILE'? [y/N] " ASK
if [[ "$ASK" == "y" || "$ASK" == "Y" ]]; then
rm /etc/2backup/profiles/$PROFILE
echo "Profile '$PROFILE' as been deleted."
exit
else
echo "Deletion aborted."
exit
fi
else
>&2 echo "Profile '$PROFILE' do not exist!"
exit 1
fi
}
profile_show() {
PROFILE="$1"
if [ ! -e "/etc/2backup/profiles/$PROFILE" ]; then
>&2 echo "Profile '$PROFILE' do not exist!"
exit 1
fi
source /etc/2backup/profiles/$PROFILE
echo "Affected subvolume(s): ${SUBV[@]}"
echo "Subvolume(s) mountpoint: {SUBV_MNTPT[@]}"
echo "Max hourly snapshots: $TIMELINE_LIMIT_HOURLY"
echo "Max daily snapshots: $TIMELINE_LIMIT_DAILY"
echo "Max weekly snapshots: $TIMELINE_LIMIT_WEEKLY"
echo "Max mounthly snapshots: $TIMELINE_LIMIT_MONTHLY"
echo "Max yearly snapshots: $TIMELINE_LIMIT_YEARLY"
exit
}
profile_list() {
echo "List of profiles:"
ls /etc/2backup/profiles
exit
}
snapshot_create() {
PROFILE="$1"
if [ ! -e "/etc/2backup/profiles/$PROFILE" ]; then
>&2 echo "Profile '$PROFILE' do not exist!"
exit 1
fi
source /etc/2backup/profiles/$PROFILE
if [[ ! "${#SUBV[@]}" == "${#SUBV_MNTPT[@]}" ]]; then
>&2 echo "There are error(s) in '$PROFILE' configuration file!"
>&2 echo "The number of subvolumes affected to it is different than the number of mountpoints!"
>&2 echo "${#SUBV[@]} and ${#SUBV_MNTPT[@]}"
exit 1
fi
if [[ "$GREATEST_SUBV_ID" == "" ]]; then
NUMBER=1
echo "GREATEST_SUBV_ID=1" >> /etc/2backup/profiles/$PROFILE
else
NUMBER=$(($GREATEST_SUBV_ID+1))
sed -i "s/GREATEST_SUBV_ID=.*/GREATEST_SUBV_ID=${NUMBER}/g" /etc/2backup/profiles/$PROFILE
fi
if [[ ! "$2" == "" ]]; then
DESCRIPTION="$2"
else
if [[ $EDITOR == "" ]]; then
nano "/tmp/2backup_snapshot_description-$NUMBER"
else
$EDITOR "/tmp/2backup_snapshot_description-$NUMBER"
fi
DESCRIPTION=$(cat "/tmp/2backup_snapshot_description-$NUMBER")
fi
TYPE="manual"
DATE="$(date)"
for i in $(seq 0 $(("${#SUBV[@]}"-1))); do
mkdir "${SUBV_MNTPT[$i]}/.snapshots/$NUMBER"
btrfs subvolume snapshot -r "${SUBV_MNTPT[$i]}" "${SUBV_MNTPT[$i]}/.snapshots/$NUMBER/snapshot"
cat << EOF > "${SUBV_MNTPT[$i]}/.snapshots/$NUMBER/infos"
TYPE=${TYPE}
DATE="${DATE}"
DESCRIPTION="${DESCRIPTION}"
SUBV_MNTPT="${SUBV_MNTPT[$i]}"
EOF
SHASUM_RAW=$(echo "$(cat ${SUBV_MNTPT[$i]}/.snapshots/$NUMBER/infos) $(date --rfc-3339=ns)" | shasum)
IFS=' ' read -r -a SHASUM <<< "$SHASUM_RAW"; unset IFS
echo "SHASUM=$SHASUM" >> "${SUBV_MNTPT[$i]}/.snapshots/$NUMBER/infos"
done
}
snapshot_delete() {
if [[ "$2" == "" ]]; then
SNAPSHOT_HASH=$1
else
SNAPSHOT_ID=$1
PROFILE=$2
fi
TO_DELETE=("")
if [[ ! "$PROFILE" == "" ]]; then
source /etc/2backup/profiles/$PROFILE
for mountpoint in ${SUBV_MNTPT[@]}; do
TO_DELETE+=("$mountpoint/.snapshots/$SNAPSHOT_ID")
done
fi
TO_BE_DELETED=""
for mountpoint in ${TO_DELETE[@]}; do
TO_BE_DELETED+="'$mountpoint' "
done
read -p "Do you really want to delete these subvolumes: ${TO_BE_DELETED}? [y/N] " ASK
if [[ "$ASK" == "y" || "$ASK" == "Y" ]]; then
for path in ${TO_DELETE[@]}; do
echo "Deleting ${path}..."
btrfs subvolume delete $path/snapshot
rm $path/infos
rmdir $path
done
echo "Done."
else
echo "Deletion aborted."
fi
}
# Main
init
echo
for i in "$@"; do
case $i in
profile)
OPTION="profile"
shift
;;
snapshot)
OPTION="snapshot"
shift
;;
rollback)
OPTION="rollback"
OBJECT="$2"
shift; shift
;;
create)
create=true
OBJECT="$2"
shift; shift
;;
delete)
delete=true
OBJECT="$2"
shift; shift
;;
list)
list=true
shift
;;
show)
show=true
OBJECT="$2"
shift; shift
;;
mount)
mount=true
OBJECT="$2"
shift; shift
;;
umount)
umount=true
OBJECT="$2"
shift; shift
;;
-p)
PROFILE="$2"
shift; shift
;;
-m)
MESSAGE="$2"
echo $MESSAGE
shift; shift
;;
-h|--help)
usage
exit
;;
# *)
# #>&2 echo "Unknown option $1"
# #exit 1
# ;;
esac
done
set -e
case $OPTION in
profile)
if [[ "$create" == true ]]; then
profile_create $OBJECT
elif [[ "$delete" == true ]]; then
profile_delete $OBJECT
elif [[ "$show" == true ]]; then
profile_show $OBJECT
elif [[ "$list" == true ]]; then
profile_list
else
>&2 echo "Unknown argument(s): $ARGS"
>&2 echo "See -h|--help."
exit 1
fi
;;
snapshot)
if [[ "$create" == true ]]; then
snapshot_create $OBJECT \"$MESSAGE\"
elif [[ "$delete" == true ]]; then
snapshot_delete $OBJECT $PROFILE
elif [[ "$show" == true ]]; then
snapshot_show $OBJECT $PROFILE
elif [[ "$list" == true ]]; then
snapshot_list $PROFILE
elif [[ "$mount" == true ]]; then
snapshot_mount $OBJECT $PROFILE
elif [[ "$umount" == true ]]; then
snapshot_umount $OBJECT $PROFILE
else
>&2 echo "Unknown argument(s): $ARGS"
>&2 echo "See -h|--help."
exit 1
fi
;;
rollback)
rollback $OPTIONS $PROFILE
;;
*)
>&2 echo "Unknown option: '$ARGS'"
exit 1
;;
esac