-
Notifications
You must be signed in to change notification settings - Fork 0
/
snaproot
executable file
·343 lines (289 loc) · 8.08 KB
/
snaproot
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
#!/usr/bin/env bash
#-*-coding:utf-8 -*-
#Auto updated?
# Yes
#File :
# snaproot
#Author :
# The-Repo-Club [[email protected]]
#Github :
# https://github.com/The-Repo-Club/
#
#Created:
# Sun 17 July 2022, 11:03:52 PM [GMT+1]
#Last edited:
# Sun 31 July 2022, 05:02:25 PM [UTC]
#
#Description:
# <Todo>
#
#Dependencies:
# <None>
#
# shellcheck disable=all
set -e
SNAP_ROOT=$(snapexec root | sed s/\'//g)
SNAP_PACMAN=$(snapexec pacman | sed s/\'//g)
## Script metadata
SCRIPTNAME=${0##*/}
VERSION="2021.09.19"
DESCRIPTION="A bash BTRFS Sanpshot wrapper"
AUTHOR="The-Repo-Club <[email protected]>"
## Set Colors (copied from makepkg)
ALL_OFF="\e[1;0m"
RED="\e[1;31m"
GREEN="\e[1;32m"
YELLOW="\e[1;33m"
BLUE="\e[1;34m"
PURPLE="\e[1;35m"
CYAN="\e[1;36m"
readonly ALL_OFF RED GREEN YELLOW BLUE PURPLE CYAN
#=== FUNCTION =================================================================
# Name: error
# Description: Print message with a red pretag an ERROR
# Parameter 1: Message to print
#==============================================================================
# copied from makepkg
error() {
local mesg=$1
shift
printf "${RED}==> ERROR:${ALL_OFF}${BOLD} %s${ALL_OFF}\n" "$mesg" "$@" >&2
}
#=== FUNCTION =================================================================
# Name: main
# Description: The main function if nothing is passed
#==============================================================================
main() {
#number of snapshots allowed
sn=$SNAP_ROOT
runas=$(whoami)
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root\n" "$SCRIPTNAME"
exit
fi
#root subvolume device
hsvdvl=1
while IFS= read -r Line; do
if [ -n "$hsvdvl" ]; then
unset hsvdvl
continue
fi
set -- $Line
[ "$2" = 'btrfs' ] && [ "${7}" = '/' ] && hsvdv="$1"
done <<<$(df -Th)
#current date
cdt=$(date +"%F-%H-%M-%S")
#root subvolume
while IFS= read -r Line; do
set -- $Line
[ "$7" = '5' ] && [ "$9" = '@' ] && hsv="${@: -1}"
done <<<$(btrfs subvolume list /)
#snapshot subvolume
while IFS= read -r Line; do
set -- $Line
[ "$7" = '5' ] && [ "$9" = '@.snapshots' ] && ssv="${@: -1}"
done <<<$(btrfs subvolume list /)
#number of root snapshots
hsl=0
while IFS= read -r Line; do
LastColumn=${Line##* }
[ "${LastColumn:0:17}" = '@.snapshots/@root' ] && [ "${LastColumn:17:7}" != '-pacman' ] && hsl=$((hsl + 1))
done <<<$(btrfs subvolume list /)
#oldest root snapshot
while IFS= read -r Line; do
LastColumn=${Line##* }
[ "${LastColumn:0:17}" = '@.snapshots/@root' ] && [ "${LastColumn:17:7}" != '-pacman' ] && ohs="$LastColumn" && break
done <<<$(btrfs subvolume list /)
#Mount root subvolume to /mnt/500GB/.snapshots
mnt=$(snapexec mnt | sed s/\'//g)
$(mount $hsvdv $mnt)
if [ -e "$hsvdv" ]; then
if [ -d "$mnt" ]; then
printf "${YELLOW}Mounted $hsvdv to $mnt${ALL_OFF}\n"
else
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi
else
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi
#MUST be in /mnt to perform task
cd $mnt
# Check root subvolume exists...
if [ -e "$hsv" ]; then
printf "${GREEN}Found root subvolume... $hsv${ALL_OFF}\n"
else
printf "${RED}No root subvolume found... exiting${ALL_OFF}\n"
exit 1
fi
# check snapshot subvolume exists...
if [ -e "$ssv" ]; then
printf "${GREEN}Found snapshot suvolume... $ssv${ALL_OFF}\n"
else
printf "${RED}No snapshot subvolume found${ALL_OFF}\n"
exit 1
fi
#Make snapshot and place in snapshot directory
btrfs subvolume snapshot $hsv $ssv/@root-$cdt
#remove a root snapshot if there are more than 5
printf "${PURPLE}Found $hsl snapshots...${ALL_OFF}\n"
if [ "$hsl" -ge "$sn" ]; then
printf "${RED}Removing oldest $hsv snapshot... $ohs${ALL_OFF}\n"
btrfs subvolume delete $ohs
else
printf "${YELLOW}Not enough $hsv snapshots...${ALL_OFF}\n"
printf "${YELLOW}No snapshots to delete...${ALL_OFF}\n"
fi
printf "\n"
cd ..
$(umount $hsvdv)
}
#=== FUNCTION =================================================================
# Name: pacman
# Description: this is ran if -p or --pacman is ran
#==============================================================================
pacman() {
#number of snapshots allowed
sn=$SNAP_PACMAN
runas=$(whoami)
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root\n" "$SCRIPTNAME"
exit
fi
#root subvolume device
hsvdvl=1
while IFS= read -r Line; do
if [ -n "$hsvdvl" ]; then
unset hsvdvl
continue
fi
set -- $Line
[ "$2" = 'btrfs' ] && [ "${7}" = '/' ] && hsvdv="$1"
done <<<$(df -Th)
#current date
cdt=$(date +"%F-%H-%M-%S")
#root subvolume
while IFS= read -r Line; do
set -- $Line
[ "$7" = '5' ] && [ "$9" = '@' ] && hsv="${@: -1}"
done <<<$(btrfs subvolume list /)
#snapshot subvolume
while IFS= read -r Line; do
set -- $Line
[ "$7" = '5' ] && [ "$9" = '@.snapshots' ] && ssv="${@: -1}"
done <<<$(btrfs subvolume list /)
#number of root snapshots
hsl=0
while IFS= read -r Line; do
LastColumn=${Line##* }
[ "${LastColumn:0:24}" = '@.snapshots/@root-pacman' ] && hsl=$((hsl + 1))
done <<<$(btrfs subvolume list /)
#oldest root snapshot
while IFS= read -r Line; do
LastColumn=${Line##* }
[ "${LastColumn:0:24}" = '@.snapshots/@root-pacman' ] && ohs="$LastColumn" && break
done <<<$(btrfs subvolume list /)
#Mount root subvolume to /mnt/500GB/.snapshots
mnt=$(snapexec mnt | sed s/\'//g)
$(mount $hsvdv $mnt)
if [ -e "$hsvdv" ]; then
if [ -d "$mnt" ]; then
printf "${YELLOW}Mounted $hsvdv to $mnt${ALL_OFF}\n"
else
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi
else
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi
#MUST be in /mnt to perform task
cd $mnt
# Check root subvolume exists...
if [ -e "$hsv" ]; then
printf "${GREEN}Found root subvolume... $hsv${ALL_OFF}\n"
else
printf "${RED}No root subvolume found... exiting${ALL_OFF}\n"
exit 1
fi
# check snapshot subvolume exists...
if [ -e "$ssv" ]; then
printf "${GREEN}Found snapshot suvolume... $ssv${ALL_OFF}\n"
else
printf "${RED}No snapshot subvolume found${ALL_OFF}\n"
exit 1
fi
#Make snapshot and place in snapshot directory
btrfs subvolume snapshot $hsv $ssv/@root-pacman-$cdt
#remove a root snapshot if there are more than 5
printf "${PURPLE}Found $hsl snapshots...${ALL_OFF}\n"
if [ "$hsl" -ge "$sn" ]; then
printf "${RED}Removing oldest $hsv snapshot... $ohs${ALL_OFF}\n"
btrfs subvolume delete $ohs
else
printf "${YELLOW}Not enough $hsv snapshots...${ALL_OFF}\n"
printf "${YELLOW}No snapshots to delete...${ALL_OFF}\n"
fi
printf "\n"
cd ..
$(umount $hsvdv)
}
#=== FUNCTION =================================================================
# Name: version
# Description: Prints version information
#==============================================================================
version() {
printf "
%s v%s - %s
This program may be freely redistributed
under the terms of The MIT License.
%s
\n" "$SCRIPTNAME" "$VERSION" "$DESCRIPTION" "$AUTHOR"
exit
}
#=== FUNCTION =================================================================
# Name: usage
# Description: Prints the usage
#==============================================================================
usage() {
printf "
Snapshot the @ and @root btrfs subvolumes.
Usage:
%s...
%s [options]...
Options:
-h, --help Display help.
-v, --version Print version information and exits.
-p, --pacman Exec the scripts with the pacman hook.
\n" "$SCRIPTNAME" "$SCRIPTNAME"
}
while true; do
case $1 in
-h | --help | -\?)
usage
exit 0
;;
-v | --version)
version
exit 0
;;
-p | --pacman)
pacman
exit 0
;;
--)
shift
break
;;
-*)
error "Unknown option: $1"
usage
exit 1
;;
*)
main
break
;;
esac
done