-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·70 lines (55 loc) · 2.12 KB
/
backup.sh
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
#!/bin/bash
set -e
cd "$(dirname "$0")"
echo "Downloading remote hashes"
./download.sh hashes.txt remote_hashes.txt || touch remote_hashes.txt
cd sources
# clean up old
rm *.tar* || echo "Last run was without fault"
for source in *; do
echo "Checking $source"
if echo "$source" | grep -Pv '^[a-zA-Z0-9-_]+$'; then
echo "Backup name '$source' is INVALID. Allowed charset: a-zA-Z0-9-_"
../notify.sh "Backup name '$source' is INVALID. Allowed charset: a-zA-Z0-9-_"
continue
fi
# collect files
set +e # tar returns exit code 0 for success, 1 for warnings, 2 for fatal error
tar --exclude-caches -cf "$source.tar" "$source/."
TAR_EXIT_CODE="$?"
if [ "$TAR_EXIT_CODE" != "0" ] && [ "$TAR_EXIT_CODE" != "1" ]; then
exit 1
fi
set -e
# calculate / load hashes
LOCAL_HASH="$(sha512sum -b "$source".tar | cut -d ' ' -f 1)"
REMOTE_HASH="$(cat ../remote_hashes.txt | grep -P -- "^$source=[0-9a-fA-F]+$" | cut -d '=' -f 2)"
if [ "$LOCAL_HASH" != "$REMOTE_HASH" ]; then
echo "Hash for $source differs... Uploading new version"
# remove old hash
echo "Removing old hash"
grep -Pv "^$source=[0-9a-fA-F]+$" ../remote_hashes.txt > ../remote_hashes_new.txt || echo "Nothing to remove"
mv ../remote_hashes_new.txt ../remote_hashes.txt
# Compress
echo "Compressing"
time xz -z -T0 "$source.tar"
# Encrypt
echo "Encrypting"
KEYFILE="../keyfiles/$source.keyfile"
if [ ! -f "$KEYFILE" ]; then
echo "No keyfile for $source found. Using fallback keyfile..."
KEYFILE="../keyfiles/fallback"
fi
time openssl aes-256-cbc -e -in "$source.tar.xz" -out "$source.tar.xz.bin" -kfile "$KEYFILE" -pbkdf2 -iter 1500000
rm "$source.tar.xz"
# Uploading new file
../upload.sh "$source.tar.xz.bin" "$source.tar.xz.bin"
# add new hash
echo "Adding new hash"
echo "$source=$LOCAL_HASH" >> ../remote_hashes.txt
fi
done
cd ..
echo "Uploading (updated) remote hashes"
./upload.sh remote_hashes.txt hashes.txt
echo "Done!"