-
Notifications
You must be signed in to change notification settings - Fork 0
/
bup.sh
executable file
·78 lines (60 loc) · 1.38 KB
/
bup.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
71
72
73
74
75
76
77
#!/bin/sh -e
# sign+encrypt a file w/ ssh keys + passphrase
DATE_BIN=/bin/date
TAR_BIN=/bin/tar
SIGNIFY_BIN=/usr/bin/signify
AGE_BIN=/usr/local/bin/age
seal_archive() {
archive="$1"
paths="$2"
pubkey="$(date '+%F').pub"
seckey="$(date '+%F').sec"
# create archive
$TAR_BIN cezf "${archive}.tgz" $paths
# encyrpt archive
$AGE_BIN -p -o "${archive}.tgz.age" < "${archive}.tgz"
# setup signify keys, sign
$SIGNIFY_BIN -G -n -c "verify with ${pubkey}; $(sha256 ${archive}.tgz.age)" -p "$pubkey" -s "$seckey"
$SIGNIFY_BIN -S -m "$archive.tgz.age" -s "$seckey"
rm -f "$seckey" "${archive}.tgz"
}
open_archive() {
enc_archive="$1"
pubkey="$2"
output="$3"
# verify signature first
$SIGNIFY_BIN -V -p "$pubkey" -m "$enc_archive"
$AGE_BIN -d -o "$output" < "$enc_archive"
}
extract_archive() {
enc_archive="$1"
pubkey="$2"
# verify signature first
$SIGNIFY_BIN -V -p "$pubkey" -m "$enc_archive"
$AGE_BIN -d -o - < "$enc_archive" | tar xezf - -C /
}
case "$1" in
"seal")
shift
archive="$1"
# read paths to backup from stdin
paths=''
while read -r line; do
paths="${paths} ${line}"
done
seal_archive "$1" $paths
;;
"open")
shift
# if a third argument is given, decrypt the archive to that
if [ -n "$3" ]; then
open_archive "$1" "$2" "$3"
else
extract_archive "$1" "$2"
fi
;;
*)
echo "invalid command." >&2
exit 1
;;
esac