-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot
executable file
·272 lines (234 loc) · 6.26 KB
/
snapshot
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
#!/usr/bin/env bash
#-*-coding:utf-8 -*-
#Auto updated?
# Yes
#File :
# snapshot
#Author :
# The-Repo-Club [[email protected]]
#Github :
# https://github.com/The-Repo-Club/
#
#Created:
# Sun 17 July 2022, 11:04:01 PM [GMT+1]
#Last edited:
# Mon 18 July 2022, 11:03:15 PM [GMT+1]
#
#Description:
# <Todo>
#
#Dependencies:
# <None>
#
# shellcheck disable=all
## 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() {
runas=$(whoami)
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root${ALL_OFF}\n" "$SCRIPTNAME"
exit
fi
if ! command -v snaphome &>/dev/null; then
printf ${RED}"snaphome could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running snaphome.${ALL_OFF}\n"
snaphome
fi
if ! command -v snaproot &>/dev/null; then
printf ${RED}"snaproot could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running snaproot.${ALL_OFF}\n"
snaproot
fi
if ! command -v grub-mkconfig &>/dev/null; then
printf ${RED}"grub-mkconfig could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running grub-mkconfig.${ALL_OFF}\n"
grub-mkconfig -o /boot/grub/grub.cfg
fi
printf "\n"
}
#=== FUNCTION =================================================================
# Name: home_only
# Description: The home script only
#==============================================================================
home_only() {
runas=$(whoami)
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root${ALL_OFF}\n" "$SCRIPTNAME"
exit
fi
if ! command -v snaphome &>/dev/null; then
printf ${RED}"snaphome could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running snaphome.${ALL_OFF}\n"
snaphome
fi
if ! command -v grub-mkconfig &>/dev/null; then
printf ${RED}"grub-mkconfig could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running grub-mkconfig.${ALL_OFF}\n"
grub-mkconfig -o /boot/grub/grub.cfg
fi
printf "\n"
}
#=== FUNCTION =================================================================
# Name: root_only
# Description: The root script only
#==============================================================================
root_only() {
runas=$(whoami)
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root${ALL_OFF}\n" "$SCRIPTNAME"
exit
fi
if ! command -v snaproot &>/dev/null; then
printf ${RED}"snaproot could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running snaproot.${ALL_OFF}\n"
snaproot
fi
if ! command -v grub-mkconfig &>/dev/null; then
printf ${RED}"grub-mkconfig could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running grub-mkconfig.${ALL_OFF}\n"
grub-mkconfig -o /boot/grub/grub.cfg
fi
printf "\n"
}
#=== FUNCTION =================================================================
# Name: pacman
# Description: this is ran if -p or --pacman is ran
#==============================================================================
pacman() {
runas=$(whoami)
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root${ALL_OFF}\n" "$SCRIPTNAME"
exit
fi
if ! command -v snaphome &>/dev/null; then
printf ${RED}"snaphome could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running pacman hook snaphome.${ALL_OFF}\n"
snaphome --pacman
fi
if ! command -v snaproot &>/dev/null; then
printf ${RED}"snaproot could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running pacman hook snaproot.${ALL_OFF}\n"
snaproot --pacman
fi
if ! command -v grub-mkconfig &>/dev/null; then
printf ${RED}"grub-mkconfig could not be found${ALL_OFF}\n"
exit
else
printf ${GREEN}"running pacman hook grub-mkconfig.${ALL_OFF}\n"
grub-mkconfig -o /boot/grub/grub.cfg
fi
printf "\n"
}
#=== 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 @home 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.
--home Exec the script home script only.
--root Exec the script root script only.
\n" "$SCRIPTNAME" "$SCRIPTNAME"
}
while true; do
case $1 in
-h | --help | -\?)
usage
exit 0
;;
-v | --version)
version
exit 0
;;
-p | --pacman)
pacman
exit 0
;;
--home)
home_only
exit 0
;;
--root)
root_only
exit 0
;;
--)
shift
break
;;
-*)
error "Unknown option: $1"
usage
exit 1
;;
*)
main
break
;;
esac
done