/
backupd
executable file
·361 lines (342 loc) · 9.67 KB
/
backupd
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
#!/bin/bash
# backupd - Copyleft (Ↄ) 2015 gustawho - https://github.com/gustawho/backupd
#
# 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 <http://www.gnu.org/licenses/>.
DATE=$(date "+%Y-%m-%d")
baseName=$(hostname)-$DATE
bkpFile=$baseName.tar.xz
gpgFile="${bkpFile%%.*}.gpg"
tmpDir=/tmp/backupd-$DATE
configFile=$HOME/.config/backupd.conf
distro=$(lsb_release -si)
userName=$(whoami)
preReq() {
if ! [ -x /usr/bin/pigz ]; then
echo -ne "Error: pigz executable not found or not executable. "
askInstall
fi
}
pkgInstall() {
case "$distro" in
Arch)
sudo pacman -S pigz
;;
Debian)
sudo aptitude install pigz
;;
Parabola)
sudo pacman -S pigz
;;
"SUSE LINUX")
sudo zypper install pigz
;;
Ubuntu)
sudo apt install pigz
esac
}
askInstall() {
echo -ne "Do you want me to install it for you? [y/n] "
read pkgConfirm
if [ "$pkgConfirm" == 'y' ]; then
pkgInstall
else
xzPkg
fi
}
xzPkg() {
case "$distro" in
Arch)
echo "Install 'pigz' from [community]."
;;
Debian)
echo -e "Please, install pigz."
;;
Parabola)
echo "Install 'pigz' from [community]."
;;
"SUSE LINUX")
echo -e "Install the pigz pacakge."
;;
Ubuntu)
echo -e "Install 'pigz': sudo apt install pigz"
;;
*)
echo -e "Please, install the pigz package provided by your system packager or build it yourself."
esac
}
davInstall() {
case "$distro" in
Arch)
sudo pacman -S davfs2
;;
Debian)
sudo aptitude install davfs2
;;
Parabola)
sudo pacman -S davfs
;;
"SUSE LINUX")
sudo zypper install davfs2
;;
Ubuntu)
sudo apt install davfs2
esac
}
davPkg() {
case "$distro" in
Arch)
echo "Install 'davfs2' from [extra]."
;;
Debian)
echo -e "Please, install davfs2."
;;
Parabola)
echo "Install 'davfs2' from [extra]."
;;
"SUSE LINUX")
echo -e "Install the davfs2 pacakge."
;;
Ubuntu)
echo -e "Install 'davfs2': sudo apt install davfs2"
;;
*)
echo -e "Please, install the davfs2 package provided by your system packager or build it yourself."
esac
}
davConf() {
echo -ne "Enter the WebDAV URL: "
read webdavUrl
echo -ne "What's your WebDAV username? "
read webdavUser
echo -ne "And your password? "
read webdavPass
f=$(cat /etc/fstab | grep davfs)
if [ -z "$f" ]; then
sudo echo "${webdavUser} /home/${userName}/${syncDir} davfs user,noauto,uid=${userName},file_mode=600,dir_mode=700 0 1" >> /etc/fstab
fi
sudo usermod -a -G network $userName
if ! [ -d "$HOME/.davfs2/" ]; then
mkdir $HOME/.davfs2/
fi
echo "${webdavUrl} ${webdavUser} ${webdavPass}" >> $HOME/.davfs2/secrets
chmod 0600 $HOME/.davfs2/secrets
echo "use_locks = 0" >> $HOME/.davfs2/davfs2.conf
}
davCheck() {
if [ "$webDavAsk" == 'y' ]; then
if ! [ -x /usr/bin/mount.davfs ]; then
echo -ne "Coulnd't detect the davfs2 module installed on your system. Should I install it now? [y/n] "
read installDav
if [ "$installDav" == 'y' ]; then
davInstall
davConf
else
davPkg
fi
else
davConf
fi
fi
}
mountDir() {
if [ "$webDav" == 'y' ]; then
if ! [[ $(cat /etc/mtab | grep ${syncDir}) ]]; then
echo -ne ">>> Mounting remote folder..."
mount $syncDir
echo -e " Done"
else
echo -e " Directory already mounted!"
fi
fi
}
compressBkp() {
echo -ne ">>> Compressing backup..."
XZ_OPT=-9 tar -Jpcf - $sourceDir | pigz > $bkpFile
echo -e " Done"
}
gpgFunc() {
if [ "$gpgEncrypt" == 'true' ]; then
echo -e ">>> Encrypting and signing backup..."
gpg --quiet --default-key=$keyId -r $recipient -e $bkpFile && mv $bkpFile.gpg $gpgFile
gpg --quiet -s -o $gpgFile.asc --detach-sig $gpgFile
echo -e "Done"
fi
}
syncNow() {
echo -ne ">>> Syncing file..."
mv -u $gpgFile $syncDir/
mv -u $gpgFile.asc $syncDir/
echo -e " Done"
if [ -f "$syncDir/$gpgFile" ]; then
echo -ne ">>> Cleaning temporary files..."
cd "$OLDPWD" && rm -rf $tmpDir
echo -e " Done\n"
echo -e " Backup job completed successfully."
else
echo -e " An error occurred while backing up. Please, try again."
fi
}
makeConf() {
rm -f $HOME/.config/backupd.conf
echo -ne "Directory to backup: "
read askSrc
mkdir -p $askSrc
echo -ne "Destination: "
read askSync
mkdir -p $askSync
echo -ne "Is the sync path a WebDAV mounted folder? [y/n] "
read webDavAsk
davCheck
echo -ne "Do you want to encrypt your files with GPG? [y/n] "
read encryptYn
echo
if [ "$encryptYn" == 'y' ]; then
echo -ne "Do you already have a valid GPG key I can use? [y/n] "
read certYn
if [ "$certYn" == 'y' ]; then
echo -ne "Please, insert your key ID and press enter (E.g. 0x12345678): "
read givenId
echo -ne "Recipient email: "
read recipientG
echo -e "sourceDir=${askSrc}\nsyncDir=${askSync}\n\nwebDav=${webDavAsk}\ngpgEncrypt=true\nkeyId=${givenId}\nrecipient=${eMail}" > $HOME/.config/backupd.conf
. ${configFile}
fi
else
echo -ne "Do you want to create one now? [y/n] "
read newKey
if [ "$newKey" == 'y' ]; then
gpg --gen-key
echo -e "### Make sure that you WILL NOT FORGET your password. If you do, you'll never be able to decrypt your files! "
getEmail=$(gpg -K | grep @)
eMail=$(grep -oP '(?<=<).*?(?=>)' <<< "$getKeyId")
echo -e "sourceDir=${askSrc}\nsyncDir=${askSync}\n\nwebDav=${webDavAsk}\ngpgEncrypt=true\nkeyId=${getKeyId}\nrecipient=${eMail}" > $HOME/.config/backupd.conf
. ${configFile}
fi
fi
}
systemD() {
systemctl --user enable backupd.service
if [ $? -eq 0 ]; then
echo "Service backupd enabled"
else
echo "Cannot enable the backupd service. Run 'sudo journalctl -xe' for log information."
fi
}
showHelp(){
echo "backupd version 0.1 Copyleft (Ↄ) 2015 gustawho - https://github.com/gustawho/backupd"
echo "Create and sync compressed and (optionally) encrypted backup files using xz and GPG"
echo
echo "Usage: backupd [OPTION...]" >&2
echo
echo "Options:"
echo
echo " -s, --sync Force the creation of the backup file"
echo " -c, --config Re-generate the configuration file (~/.config/backupd.conf)"
echo " --status Display the current settings"
echo " -d, --daemon Use as daemon with systemd"
echo " -w, --webdav Configure backupd to use WebDAV"
echo " -h, --help Display this message"
echo
echo "The output files will be named by hostname and creation date,"
echo "E.g., myPC-2015-12-05.gpg <- Backup file"
echo " myPC-2015-12-05.gpg.asc <- Digital signature"
echo
echo "You can run this script manually or let the systemd service do it for you."
echo -e "By default, it will run the sync process weekly, 10 minutes after boot, but you can\nchange this editing the section [Timer] of /usr/lib/systemd/user/backupd.timer"
}
if [ -f "${configFile}" ]; then
. ${configFile}
else
echo -e "It seems this is the first time you run the program.\nWe'll begin with the creation of the configuration file."
echo -ne "Directory to backup: "
read askSrc
mkdir -p $askSrc
echo -ne "Destination: "
read askSync
mkdir -p $askSync
echo -ne "Is the sync path a WebDAV mounted folder? [y/n] "
read webDavAsk
davCheck
echo -ne "Do you want to encrypt your files with GPG? [y/n] "
read encryptYn
if [ "$encryptYn" == 'y' ]; then
echo -ne "Do you already have a valid GPG key I can use? [y/n] "
read certYn
if [ "$certYn" == 'y' ]; then
echo -ne "Please, insert your key ID and press enter (E.g. 0x12345678): "
read givenId
echo -ne "Recipient email: "
read recipientG
echo -e "sourceDir=${askSrc}\nsyncDir=${askSync}\n\nwebDav=${webDavAsk}\ngpgEncrypt=true\nkeyId=${givenId}\nrecipient=${eMail}" > $HOME/.config/backupd.conf
. ${configFile}
fi
else
echo -ne "Do you want to create one now? [y/n] "
read newKey
echo
if [ "$newKey" == 'y' ]; then
gpg --gen-key
echo -e "### Make sure that you WILL NOT FORGET your password. If you do, you'll never be able to decrypt your files! "
getEmail=$(gpg -K | grep @)
eMail=$(grep -oP '(?<=<).*?(?=>)' <<< "$getKeyId")
echo -e "sourceDir=${askSrc}\nsyncDir=${askSync}\n\nwebDav=${webDavAsk}\ngpgEncrypt=true\nkeyId=${getKeyId}\nrecipient=${eMail}" > $HOME/.config/backupd.conf
. ${configFile}
fi
fi
fi
while :
do
case "$1" in
-s | --sync)
preReq
mkdir -p $tmpDir && cd $tmpDir
mountDir
compressBkp
gpgFunc
syncNow
break
;;
-c | --config)
makeConf
break
;;
--status)
cat $HOME/.config/backupd.conf
break
;;
-d | --daemon)
systemD
break
;;
-w | --webdav)
davCheck
break
;;
-h | --help)
showHelp
break
;;
--)
shift
break
;;
-*)
echo "Error: Unknown option: $1" >&2
exit
;;
*)
showHelp
break
esac
done