summaryrefslogtreecommitdiffstats
path: root/zfs-backup.sh
blob: 733c570c1190cbb44767e887baa5639be9e73fb4 (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
#!/bin/bash

## example host config
# set_vars root karif
#
# run_ssh $src /root/backup.pl start
#
# run_ssh backup@$host "cd /home/backup/mysql-dumps; /home/backup/create-mysql-dumps"
# run_ssh backup@$host "cd /home/backup/psql-dumps; /home/backup/create-psql-dumps"
# backup_rsync "$src:/home/backup/mysql-dumps/" "$dst/mysql-dumps/"
# backup_rsync "$src:/home/backup/psql-dumps/" "$dst/psql-dumps/"
# run_ssh backup@$host "rm -f /home/backup/mysql-dumps/*.sql.gz"
# run_ssh backup@$host "rm -f /home/backup/psql-dumps/*.dump"
#
# backup_rsync --filter=". /mnt/backup/filters/karif_root_filter" "$src:/mnt/snapshots/vg0-root/" "$dst/root/"
# backup_rsync --filter=". /mnt/backup/filters/karif_home_filter" "$src:/mnt/snapshots/vg0-home/" "$dst/home/"
# backup_rsync "$src:/boot/" "$dst/boot/"
#
# run_ssh $src /root/backup.sh stop
#
# create_and_clean_snapshots "$dst_fs"



set -e

backup_rsync() {
	local dir="${@: -1}"
	mkdir -p "$dir"
	rsync --rsh="/usr/bin/ssh -i /root/.ssh/id_rsa-backup" --delete --numeric-ids --delete-excluded --max-delete=100000 -axEAXHv "$@"
}

run_ssh() {
	ssh -i /root/.ssh/id_rsa-backup "$@"
}

set_vars() {
	user="$1"
	host="$2"
	src="$user@$host"
	dst_fs="$backup_fs_base/$host"
	dst="$(zfs get mountpoint -o value -H "$dst_fs")"
}

create_snapshots() {
	local dst_fs=$1

	if [[ "$(date +%d)" = "01" ]]; then
		/root/zfs_create_dated_snapshot.sh "$dst_fs" monthly
	fi
	if [[ "$(date +%u)" = "7" ]]; then
		/root/zfs_create_dated_snapshot.sh "$dst_fs" weekly
	fi
	/root/zfs_create_dated_snapshot.sh "$dst_fs" daily
}

clean_snapshots() {
	local dst_fs=$1

	/root/zfs_snapshot_cleanup.sh "$dst_fs" daily 10
	/root/zfs_snapshot_cleanup.sh "$dst_fs" weekly 4
	/root/zfs_snapshot_cleanup.sh "$dst_fs" monthly 3
}

create_and_clean_snapshots() {
	local dst_fs=$1
	create_snapshots "$dst_fs"
	clean_snapshots "$dst_fs"
}

backup_fs_base="data/backup_zfs/backups"

if (($#<1)); then
	echo "usage: ${0##*/} <hostname>"
	exit 1
fi

source "${0%/*}/host-configs/$1"