-
Notifications
You must be signed in to change notification settings - Fork 0
/
bak.sh
executable file
·46 lines (35 loc) · 1.15 KB
/
bak.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
#!/usr/bin/env sh
# /etc/fstab
# /dev/mapper/Pictures /home/aleks/Pictures ext4 noauto,rw,noatime 0 2
# /etc/crypttab
# Backup /dev/disk/by-label/Backup /root/key noauto
# Documents /dev/disk/by-label/Documents /root/key noauto
# Pictures /dev/disk/by-label/Pictures /root/key noauto
[ ! -x "$(command -v rsync)" ] && echo rsync not on the PATH && exit
TARGET=Documents
SRC=$HOME/$TARGET/
DEST=/mnt/$TARGET
DEVICE=/dev/disk/by-label/$TARGET
MAPPER=/dev/mapper/$TARGET
clean_up() {
sleep 1 # give rsync some time to terminate
is_mounted "$DEST" && sudo umount -v "$DEST"
if is_open "$TARGET"; then
echo "close: $TARGET"
sudo cryptsetup close "$TARGET"
fi
exit
}
trap clean_up INT HUP TERM EXIT
is_open() { sudo dmsetup ls | grep -q "$1" ; }
is_mounted() { grep -q "$1" /proc/mounts ; }
# DECRYPT
if ! is_open "$TARGET"; then
echo "open: $DEVICE as $TARGET"
sudo cryptsetup open "$DEVICE" "$TARGET" -d /root/key
fi
# MOUNT
if ! is_mounted "$DEST"; then
sudo mount -v "$MAPPER" --mkdir "$DEST" || exit
fi
sudo rsync -ahiv --delete --exclude '.~*' --exclude 'lost+found/' --exclude '.Trash-1000/' "$SRC" "$DEST"