server-scripts/systembackup

103 lines
2.6 KiB
Plaintext
Raw Normal View History

2017-04-15 02:46:20 +00:00
#!/bin/bash
# ----------------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------------
cleanup() {
if [ "y" == "$MYSQL" ]; then
umount -f /var/lib/mysql-snapshot || true
lvremove -f /dev/vg0/mysql-snapshot || true
fi
}
mount() {
echo "mounting /var/lib/mysql-snapshot..."
mkdir -p /var/lib/mysql-snapshot
/bin/mount /dev/vg0/mysql-snapshot /var/lib/mysql-snapshot
echo "mounted /var/lib/mysql-snapshot."
}
lock() {
echo "locking database..."
mysql << EOF
FLUSH TABLES;
FLUSH TABLES WITH READ LOCK;
system lvcreate --snapshot -n mysql-snapshot -L16G /dev/vg0/mysql
UNLOCK TABLES;
EOF
echo "database locked"
}
# ----------------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------------
if [ ! -f /etc/systembackup.conf ]; then
echo "systembackup not configured"
exit 1
fi
source /etc/systembackup.conf
if [ -z "$REPOSITORY" ]; then
echo "REPOSITORY not set"
exit 1
fi
if [ -z "$PASSPHRASE" ]; then
echo "PASSPHRASE not set"
exit 1
fi
# ----------------------------------------------------------------------------
# SETUP
# ----------------------------------------------------------------------------
set -e
trap cleanup SIGHUP SIGINT SIGTERM
if [ "y" == "$MYSQL" ]; then
lock
mount
fi
# ----------------------------------------------------------------------------
# BACKUP
# ----------------------------------------------------------------------------
BORG_PASSPHRASE=$PASSPHRASE /usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 borg create --stats -v \
$REPOSITORY::'{hostname}-{now:%Y-%m-%d-%H}' \
/ \
--compression lzma \
--exclude '/dev' \
--exclude '/proc' \
--exclude '/sys' \
--exclude '/tmp' \
--exclude '/run' \
--exclude '/var/run' \
--exclude '/mnt' \
--exclude '/media' \
--exclude 'lost+found' \
--exclude '/var/cache/apt/archives/' \
--exclude '/var/lib/lxcfs'
--exclude '/var/lib/mysql'
# ----------------------------------------------------------------------------
# PRUNE
# ----------------------------------------------------------------------------
BORG_PASSPHRASE=$PASSPHRASE /usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 borg prune -v --list $REPOSITORY --prefix '{hostname}-' \
--keep-hourly=6 --keep-daily=7 --keep-weekly=4 --keep-monthly=6
# ----------------------------------------------------------------------------
# CLEANUP
# ----------------------------------------------------------------------------
cleanup