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

# backup.sh
#
# This script is a template for a backup script which could be installed to
# /usr/local/bin as a per-machine system backup script. You will have to tweak
# it to your specific needs.
#
# The following dependencies have to be installed:
#
# - borg for the backup itself
# - mariabackup to get a consistent snapshot of all MariaDB databases
# - go-sendxmpp to notify in case of errors / success

HOST='org.example.host'
ADMIN='admin@example.org'
REPO='/srv/borg'
MARIADB_BACKUP_DIR='/var/local/mariadb_export'

# create full backup for mariadb to ensure consistent state (optional)
rm -rf ${MARIADB_BACKUP_DIR}
mkdir -p ${MARIADB_BACKUP_DIR}
chmod 700 ${MARIADB_BACKUP_DIR}
if ! mariabackup --backup --target-dir=${MARIADB_BACKUP_DIR}/ --user=root
then
	echo "❌ Backup: MariaDB backup failed on ${HOST}." | go-sendxmpp "${ADMIN}"
	exit 1
fi

# create a BorgBackup archive in the given repository
if ! borg create \
	--verbose \
	--stats \
	--compression zstd \
	--exclude 'dev/*' \
	--exclude 'lost+found/*' \
	--exclude 'media/*' \
	--exclude 'mnt/*' \
	--exclude 'proc/*' \
	--exclude 'run/*' \
	--exclude 'sys/*' \
	--exclude 'tmp/*' \
	--exclude 'srv/borg/*' \  # change / remove me based on REPO variable
	"${REPO}::${HOST}_{now:%Y-%m-%d_%H:%M:%S}" \
	/
then
	echo "❌ Backup: borg create failed on ${HOST}." | go-sendxmpp "${ADMIN}"
	exit 1
fi

# prune existing archives in the repository to reduce disk usage
if ! borg prune \
	--glob-archives "${HOST}*" \
	--keep-daily 7 \
	--keep-weekly 4 \
	--keep-monthly 6 \
	"${REPO}"
then
	echo "❌ Backup: borg prune failed on ${HOST}." | go-sendxmpp "${ADMIN}"
	exit 1
fi

# inform admin about success if not exited before
echo "✅ System backup succeeded on ${HOST}." | go-sendxmpp "${ADMIN}"