summaryrefslogtreecommitdiffstats
path: root/backup.sh
blob: a59cf3e41c0c8b3dfe19748208889b968baebc01 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#
# This is a simple backup script using duplicity. It's supposed to serve as a
# starting point and to be adjusted to your system.
#
# Important steps:
#  - define a host "backup" in root's .ssh/config
#  - read the script and adjust to your needs

set -e

main() {
	if [[ $UID != 0 ]]; then
		exec sudo "$0" "$@"
	fi

	TMPDIR="$(mktemp -d "/tmp/${0##*/}.XXXXXX")"
	trap "rm -rf '${TMPDIR}'" EXIT TERM

	# ensure duplicity keeps its cache at a central location
	export HOME=/root

	# if you want to encrypt the backups remove --no-encryption in the duplicity call
	# and uncomment the lines that contain PASSPHRASE
	#PASSPHRASE="randomstringhere"

	# these mountpoints will be excluded
	excludeMountpoints=(
	/tmp/
	/sys/
	/dev/
	/proc/
	/run/
	/mnt/levant/nfs/
	/media/
	)

	# first line that matches wins
	IFS='' read -r -d '' excludeList <<EOF || true
+ /home/flo/.local/share/Steam/steamapps/common/Counter-Strike Global Offensive/csgo/cfg
- /home/*/.local/share/Steam/steamapps/common/*/*
- /home/*/.cache/*
- /home/*/.claws-mail/imapcache
- /root/.cache/*
- /var/cache/pacman/pkg/*
EOF

	exclude_mountpoints
	echo "$excludeList" > "$TMPDIR/exclude-list"

	# save some data that's useful for restores
	local backupDataDir=/root/backup-data/
	mkdir -p "$backupDataDir"
	fdisk -l > "$backupDataDir/fdisk"
	vgdisplay > "$backupDataDir/vgdisplay"
	pvdisplay > "$backupDataDir/pvdisplay"
	lvdisplay > "$backupDataDir/lvdisplay"
	lvdisplay > "$backupDataDir/lvdisplay"
	df -a > "$backupDataDir/df"
	findmnt -l > "$backupDataDir/findmnt"

	# this does not ignore /proc and network mounts so it's not that useful :(
	#find / | gzip > /root/full-file-list.txt.gz

	backup / sftp://backup/$HOSTNAME-backup/full-backup/ --exclude-filelist "$TMPDIR/exclude-list"
}

backup() {
	local src=$1
	local dest=$2
	shift 2
	local -a options=()

	if [[ $(date +%u ) == '1' ]]; then
		# try to only run full backups on monday (1)
		options+=(--full-if-older-than 2D)
	else
		# force a full backup once in a while
		options+=(--full-if-older-than 20D)
	fi

	#export PASSPHRASE
	duplicity \
		--log-file /var/log/backup.log \
		-v5 \
		--numeric-owner \
		--volsize 250 \
		--allow-source-mismatch \
		--asynchronous-upload \
		--no-encryption \
		"${options[@]}" "$@" "$src" "$dest"

	duplicity --log-file /var/log/backup.log --force remove-older-than 120D "$dest"
	#export PASSPHRASE=""
}

### support functions below ###

##
#  usage : in_array( $needle, $haystack )
# return : 0 - found
#          1 - not found
##
in_array() {
    local needle=$1; shift
    local item
    for item in "$@"; do
        [[ $item = "$needle" ]] && return 0 # Found
    done
    return 1 # Not Found
}

# same as in_array except 0 is returned if any item in haystack starts with needle
in_array_startswith() {
    local needle=$1; shift
    local item
    for item in "$@"; do
        [[ "$needle" == "$item"* ]] && return 0 # Found
    done
    return 1 # Not Found
}

exclude_mountpoints() {
	local error=0

	for fs in "${excludeMountpoints[@]}"; do
		if [[ $fs != */ ]]; then
			error=1
			echo "Error: excludeMountpoints entry doesn't end with /: $fs" >&2
		fi
		excludeList+="- $fs*"$'\n'
	done

	while read line; do
		local mountpoint=$(echo "$line" | cut -d\  -f2 | sed 's#\040# #g;')
		local type=$(echo "$line" | cut -d\  -f3)

		if ! in_array $type "${fsWhitelist[@]}"; then
			if ! in_array_startswith "$mountpoint/" "${excludeMountpoints[@]}"; then
				error=1
				echo "Warning: mountpoint not excluded: $mountpoint" >&2
			fi
		fi
	done </etc/mtab

	if ((error)); then
		exit 1
	fi
}

main "$@"