aboutsummaryrefslogtreecommitdiff
path: root/local
diff options
context:
space:
mode:
Diffstat (limited to 'local')
-rwxr-xr-xlocal/bin/system-backup47
1 files changed, 32 insertions, 15 deletions
diff --git a/local/bin/system-backup b/local/bin/system-backup
index 956ba31..fde63c4 100755
--- a/local/bin/system-backup
+++ b/local/bin/system-backup
@@ -4,10 +4,14 @@
DIRS="/etc
/home
/home_hdd"
+# Name of the machine used in backup subvolumes
+MNAME=asus
# Path where backup will be mounted
-MPATH=/media/backup-hdd
+MPATH=/media/backup
# UUID of disk
UUID=b162ea95-38bb-42c6-b36a-1be98c65392c
+# Mount additional arguments
+MARGS="-o compress=lzo"
# If disk is encrypted
CRYPT=true
####################################################################
@@ -25,28 +29,41 @@ if [ -z "$PART" ]; then
exit -1
fi
if $CRYPT; then
- cryptsetup open "$PART" backup_enc || (echo Decryption failed && exit 1)
- PART=/dev/mapper/backup_enc
+ echo -e "\e[1;33mDecrypting filesystem\e[0m"
+ # TODO what if already opened
+ cryptsetup open "$PART" backup || (echo Decryption failed && exit -2)
+ PART=/dev/mapper/backup
+ echo $PART
fi
+echo -e "\e[1;33mMounting\e[0m"
mkdir -p "$MPATH"
-mount "$PART" "$MPATH" || (echo Mount failed && exit 2)
+mount $MARGS "$PART" "$MPATH" || (echo Mount failed && exit -3)
+
+[ -d "$MPATH"/@"$MNAME" ] || (echo There seems to be no subvolume $MNAME && exit -4)
# Do backup
while read -r DIR; do
- echo Backing up: $DIR
- rdiff-backup -b --exclude-device-files --exclude-fifos --exclude-sockets \
- --terminal-verbosity=5 --print-statistics --exclude-other-filesystems \
- "$DIR" "$MPATH/$(basename $DIR)/"
- rdiff-backup --remove-older-than 100D --force \
- --terminal-verbosity=5 --print-statistics \
- "$MPATH/$(basename $DIR)/"
+ echo -e "\e[1;33mBacking up: $DIR\e[0m"
+ rsync -a --progress "$DIR" "$MPATH"/@"$MNAME"/
done <<< "$DIRS"
+# Do snapshot (read only)
+(cd "$MPATH"; btrfs subvolume snapshot -r @asus @asus-$(date -u +%y%m%d))
+
+# Remove snapshots older than 2 months
+# TODO ensure that at least five stays
+(cd "$MPATH"
+for s in @"$MNAME"-*; do
+ if [ $(expr $(date +%s) - $(stat -c %Y "$s")) -gt 5529600 ]; then
+ btrfs subvolume delete "$s"
+ fi
+done)
+
# Unmount disk
-sync
-umount "$MPATH" || (echo Unmount failed. Unmount by hand. && exit 3)
+sync -f "$MPATH"/@"$MNAME"
+umount "$MPATH" || (echo Unmount failed. Unmount by hand. && exit -5)
if $CRYPT; then
- cryptsetup close backup_enc
+ cryptsetup close backup_enc || (echo Encryption close failed. Do by hand. && exit -6)
fi
# Store when we did last backup and update i3blocks status
@@ -54,4 +71,4 @@ date +"%s" > /home/cynerd/.backup_date
pkill -RTMIN+13 i3blocks
echo
-echo Backup finished
+echo -e "\e[1;34mBackup finished\e[0m"