aboutsummaryrefslogtreecommitdiff
path: root/local/bin/system-backup
blob: 956ba317ec30c6fe614fd51bcff20229e31782e7 (plain)
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
#!/bin/bash
# vim:ft=sh
# New line separated list of all directories to backup
DIRS="/etc
/home
/home_hdd"
# Path where backup will be mounted
MPATH=/media/backup-hdd
# UUID of disk
UUID=b162ea95-38bb-42c6-b36a-1be98c65392c
# If disk is encrypted
CRYPT=true
####################################################################
# Check if running as root
if [ `id -u` -ne "0" ]; then
	echo Please run this as root.
	exit 1
fi
set -e

# Mount disk
PART=$(lsblk -fpl | grep "$UUID" | awk '{print $1}')
if [ -z "$PART" ]; then
	echo Disk not detected. Exiting
	exit -1
fi
if $CRYPT; then
	cryptsetup open "$PART" backup_enc || (echo Decryption failed && exit 1)
	PART=/dev/mapper/backup_enc
fi
mkdir -p "$MPATH"
mount "$PART" "$MPATH" || (echo Mount failed && exit 2)

# 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)/"
done <<< "$DIRS"

# Unmount disk
sync
umount "$MPATH" || (echo Unmount failed. Unmount by hand. && exit 3)
if $CRYPT; then
	cryptsetup close backup_enc
fi

# Store when we did last backup and update i3blocks status
date +"%s" > /home/cynerd/.backup_date
pkill -RTMIN+13 i3blocks

echo
echo Backup finished