summaryrefslogtreecommitdiff
path: root/bin/backup
blob: 396492788c9afe693a10c1837d541986e030cfd7 (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
#!/bin/sh

usage() {
	echo "Usage: $0 [-nv] [[[user@]host]:dir]

Incremental backup using rsync(1). If run as root, a full system
backup is performed. Otherwise, the user's home directory is backed.

Options:
-n	dry-run
-v	verbose

Files:
- $HOME/.backupignore	exclude files from user backup (non root)
- /etc/backupignore		exclude files from system backup (root)

Environment:
- BACKUP	backup directory"
}

while getopts :nv opt; do
	case $opt in
	(n|v) optv="$opt$optv" ;;
	(*) usage; exit 1 ;;
	esac
done
shift $((OPTIND - 1))

BACKUP=${1:-$BACKUP}
[ "$BACKUP" ] || { usage; exit 1; }

[ "$USER" = root ] &&
	ignore=/etc/backupignore volumes='/ /boot' ||
	ignore="$HOME/.backupignore" volumes="$HOME"
[ -f "$ignore" ] && ignore="--exclude-from=$ignore" || ignore=

last=$(rsync --list-only "$BACKUP/" 2>/dev/null | awk '{r=$NF} END {print r}')
case $last in
([12]*) opt_link=--link-dest=../$last;;
(*) opt_link=;;
esac

date=$(date +%Y-%m-%d-%H%M%S)

[ "$optv" ] && echo "# Backup $volumes to $BACKUP/$date"

exec rsync -HSxa$optv $ignore $opt_link $volumes "$BACKUP/$date"