-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
168 lines (140 loc) · 4.27 KB
/
backup
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
#!/bin/bash
MASK=127
ROOT=""
BACKUP="$HOME"
RELEASE=$( lsb_release -rs )
TIME=$( date +%s )
usage() { echo "Usage: $0 [-m <mask-for-options>] [-r <some-other-root>] [-b <backup-directory>]" 1>&2; exit 1; }
while getopts ":m:r:b:" o; do
case "${o}" in
m)
m=${OPTARG}
if [ $m -gt 127 ]; then echo -e "Invalid mask\n"; usage; else (( MASK = m )); fi
;;
r)
r=${OPTARG}
if [[ ! -d "$r" ]]; then echo -e "Root directory does not exist\n"; usage; fi
ROOT=$r;
;;
b)
BACKUP=${OPTARG}
if [[ ! -d "$BACKUP" ]]; then echo -e "Backup directory does not exist\n"; usage; fi
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
BACKUP=$(echo "$BACKUP/CustomBackup/$TIME-$RELEASE/")
echo "$BACKUP"
if [ -d "$BACKUP" ]; then
echo "Backup directory already exists."
read -p "Do you want to override any existing backup? (yes/[No])" choice
case "$choice" in
[Yy][Ee][sS]|[Yy])
rm "$BACKUP" -r
mkdir "$BACKUP" -p || exit 1
echo "Changing working directory..."
cd "$BACKUP"
;;
*)
echo "Exiting..."
echo "No backup made!!"
exit 0
;;
esac
else
mkdir "$BACKUP" -p
echo "Changing working directory..."
cd "$BACKUP"
fi
if [ -z "${m}" ] ; then
read -p "Backup installed snaps? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 63 ))
;;
esac
read -p "Backup installed packages? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 95 ))
;;
esac
read -p "Backup third party software repositories? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 111 ))
;;
esac
read -p "Backup third party public keys? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 119 ))
;;
esac
read -p "Backup network configurations and saved networks? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 123 ))
;;
esac
read -p "Backup system PATH variable? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 125 ))
;;
esac
read -p "Backup mount options? ([yes]/no): " choice
case "$choice" in
[Nn][Oo]|[Nn])
(( MASK = MASK & 126 ))
;;
esac
fi
snaps() {
input=$(snap list | cut -f -1 -d ' ')
echo "$input" | sed 1d > snaps.list
echo "Snaps list saved"
}
packages() {
sudo apt list --manual-installed | grep "\[installed\]" | cut -f -1 -d '/' > package.list
echo "Packages list saved"
}
repositories() {
mkdir ./etc/apt/sources.list.d -p || exit 1
cp "$ROOT/etc/apt/sources.list.d/" ./etc/apt/ -r
echo "Third party repositories backed up"
}
public_keys() {
mkdir ./etc/apt/trusted.gpg.d -p || exit 1
cp "$ROOT/etc/apt/trusted.gpg" ./etc/apt/
cp "$ROOT/etc/apt/trusted.gpg.d/" ./etc/apt/ -r
mkdir ./usr/share/keyrings -p || exit 1
cp "$ROOT/usr/share/keyrings/" ./usr/share/ -r
echo "Public Keys of repositories backed up"
}
saved_nw() {
mkdir ./etc/NetworkManager/system-connections -p || exit 1
sudo cp "$ROOT/etc/NetworkManager/system-connections/" ./etc/NetworkManager/ -r
echo "Network configurations backed up"
}
env_path() {
mkdir ./etc -p || exit
cp "$ROOT/etc/environment" ./etc/
echo "Environment variable backed up"
}
mount_options() {
input=$(cat "$ROOT/etc/fstab" | grep "^#" -v)
echo "$input" | awk '{if ($2 == "none" || $2 == "/" || $2 == "/home" || $2 == "/boot/efi" || $2 == "/boot" ) ; else print $0; }' > ./etc/fstab
echo "Mount configurations backed up"
}
if (( ( $MASK & 64 ) > 0 )); then snaps ; fi
if (( ( $MASK & 32 ) > 0 )); then packages ; fi
if (( ( $MASK & 16 ) > 0 )); then repositories ; fi
if (( ( $MASK & 8 ) > 0 )); then public_keys ; fi
if (( ( $MASK & 4 ) > 0 )); then saved_nw ; fi
if (( ( $MASK & 2 ) > 0 )); then env_path ; fi
if (( ( $MASK & 1 ) > 0 )); then mount_options ; fi
echo "$TIME $RELEASE" >> ../backups.list