-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore
203 lines (176 loc) · 6.59 KB
/
restore
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
#!/bin/bash
MASK=127
ROOT=""
BACKUP="$HOME/CustomBackup"
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 "Invalid root directory\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))
if [ -d "$BACKUP" ]; then
echo "Changing working directory..."
cd "$BACKUP"
else
echo -e "\nERROR: Backup directory not found"
exit 1
fi
if [ -z "${m}" ] ; then
read -p "Restore snaps list? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 63 ))
fi
read -p "Restore packages list? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 95 ))
fi
read -p "Restore third party software repositories? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 111 ))
fi
read -p "Restore third party public keys? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 119 ))
fi
read -p "Restore network configurations and saved networks? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 123 ))
fi
read -p "Restore system PATH variable? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 125 ))
fi
read -p "Restore mount options? ([yes]/no): " choice
if [ "$choice" == "n" ] || [ "$choice" == "no" ] || [ "$choice" == "N" ] || [ "$choice" == "No" ] || [ "$choice" == "NO" ] || [ "$choice" == "nO" ]; then
(( MASK = MASK & 126 ))
fi
fi
snaps() {
if [ -f "./snaps.list" ]; then
list=( "$( cat ./snaps.list )" )
for i in ${list[@]}; do sudo snap install $i; done
echo -e "\nSUCCESS: Snap softwares restored"
else
echo -e "\nWARNING: Could not find snaps backup list"
fi
}
packages() {
if [ -f "./package.list" ]; then
sudo apt update
list=$( cat ./package.list )
sudo apt install $list -y
echo -e "\nSUCCESS: Software packages restored"
else
echo -e "\nWARNING: Could not find packages backup list"
fi
}
repositories() {
if [ -d "./etc/apt/sources.list.d" ]; then
sudo cp ./etc/apt/sources.list.d/* "$ROOT/etc/apt/sources.list.d/"
echo -e "\nSUCCESS: Third party repositories restored"
else
echo -e "\nWARNING: Could not find third party repositories backup"
fi
}
public_keys() {
if [ -d "./etc/apt/trusted.gpg.d" ]; then
sudo cp ./etc/apt/trusted.gpg.d/* "$ROOT/etc/apt/trusted.gpg.d/"
echo -e "\nSUCCESS: Trusted GPG directory restored"
else
echo -e "\nWARNING: Could not find trusted gpg directory"
fi
if [ -f "./etc/apt/trusted.gpg" ]; then
sudo cp "$ROOT/etc/apt/trusted.gpg" "$ROOT/etc/apt/trusted.gpg"
sudo cp ./etc/apt/trusted.gpg "$ROOT/etc/apt/"
echo -e "\nSUCCESS: Trusted GPG file restored"
else
echo -e "\nWARNING: Could not find trusted gpg file backup"
fi
if [ -d "./usr/share/keyrings" ]; then
sudo cp ./usr/share/keyrings/* "$ROOT/usr/share/keyrings/" -i
echo -e "\nSUCCESS: Shared keyrings directory restored"
else
echo -e "\nWARNING: Could not find shared keyrings backup directory"
fi
}
saved_nw() {
if [ -d "./etc/NetworkManager/system-connections" ]; then
sudo cp ./etc/NetworkManager/system-connections/* "$ROOT/etc/NetworkManager/system-connections/" -i
echo -e "\nSUCCESS: Network configurations restored"
else
echo -e "\nWARNING: Could not find network configurations backup"
fi
}
env_path() {
if [ -f "./etc/environment" ]; then
sudo cp "$ROOT/etc/environment" "$ROOT/etc/environment.bak"
sudo cp ./etc/environment "$ROOT/etc/"
echo -e "\nSUCCESS: System environment variable restored"
else
echo -e "\nWARNING: Could not find system environment variable backup"
fi
}
mount_options() {
if [ -f "./etc/fstab" ]; then
dirs=( "$( awk '{ print $2; }' ./etc/fstab )" )
for i in ${dirs[@]}; do sudo mkdir "$i"; done
sudo cp "$ROOT/etc/fstab" "$ROOT/etc/fstab.bak"
sudo su -c 'cat ./etc/fstab >> "$ROOT/etc/fstab"'
echo -e "\nSUCCESS: Mount configurations restored"
sudo mount -a
else
echo -e "\nWARNING: Could not find Mount configurations backup"
fi
}
backup_dirs=( "" )
if [ ! -f backups.list ]; then
echo "Backup Index file does not exist.";
exit 1;
fi
echo -e "\nAvailable Backups\n\t Ubuntu\tBackup Date"
i=0
while read -r line; do
(( i++ ))
val=( $line )
echo -e "\t$i) ${val[1]}\t`date -d @${val[0]}`"
backup_dirs=( "${backup_dirs[@]}" "${val[0]}-${val[1]}" )
done < backups.list
echo ""
read -p "Select a backup to restore. (1-$i): " choice
(( i++ ))
if [ $choice -lt $i ] && [ $choice -gt 0 ]; then
if [ -d "${backup_dirs[$choice]}" ]; then
BACKUP="$BACKUP/${backup_dirs[$choice]}/"
cd "$BACKUP" || exit 1
else
echo -e "\nInvalid backup or backup directory has been deleted."
exit 1
fi
else
echo -e "\nInvalid backup selected."
exit 1
fi
echo ""
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
if (( ( $MASK & 64 ) > 0 )); then snaps ; fi
if (( ( $MASK & 32 ) > 0 )); then packages ; fi