server-scripts/systembackup

98 lines
2.6 KiB
Plaintext
Raw Permalink Normal View History

2017-04-15 02:46:20 +00:00
#!/bin/bash
2017-06-12 00:45:18 +00:00
export PATH=/usr:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin
2017-04-15 02:46:20 +00:00
# ----------------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------------
cleanup() {
if [ "y" == "$MYSQL" ]; then
2017-06-12 00:45:18 +00:00
/bin/umount -f /var/lib/mysql-snapshot || true
/sbin/lvremove -f /dev/vg0/mysql-snapshot || true
2017-04-15 02:46:20 +00:00
fi
}
2017-04-15 02:58:55 +00:00
mount_snapshot() {
2017-04-15 02:46:20 +00:00
echo "mounting /var/lib/mysql-snapshot..."
2017-06-12 00:45:18 +00:00
/bin/mkdir -p /var/lib/mysql-snapshot
/bin/mount /dev/vg0/mysql-snapshot /var/lib/mysql-snapshot
2017-04-15 02:46:20 +00:00
echo "mounted /var/lib/mysql-snapshot."
}
2017-04-15 02:58:55 +00:00
create_snapshot() {
2017-04-15 02:46:20 +00:00
echo "locking database..."
2017-06-12 00:45:18 +00:00
/usr/bin/mysql --defaults-extra-file=/root/.my.cnf << EOF
2017-04-15 02:46:20 +00:00
FLUSH TABLES;
FLUSH TABLES WITH READ LOCK;
2017-06-12 00:45:18 +00:00
system /sbin/lvcreate --snapshot -n mysql-snapshot -L16G /dev/vg0/mysql
2017-04-15 02:46:20 +00:00
UNLOCK TABLES;
EOF
echo "database locked"
}
# ----------------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------------
if [ ! -f /etc/systembackup.conf ]; then
2017-06-12 00:45:18 +00:00
echo "systembackup not configured (/etc/systembackup.conf)"
2017-04-15 02:46:20 +00:00
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 EXIT
2017-04-15 02:46:20 +00:00
if [ "y" == "$MYSQL" ]; then
2017-04-15 02:58:55 +00:00
create_snapshot
mount_snapshot
2017-04-15 02:46:20 +00:00
fi
# ----------------------------------------------------------------------------
# BACKUP
# ----------------------------------------------------------------------------
2017-06-12 00:45:18 +00:00
BORG_PASSPHRASE=$PASSPHRASE /usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/bin/borg create --stats -v \
2017-04-15 02:46:20 +00:00
$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' \
2017-04-15 02:46:20 +00:00
--exclude '/var/lib/mysql'
# ----------------------------------------------------------------------------
# PRUNE
# ----------------------------------------------------------------------------
2017-06-12 00:45:18 +00:00
BORG_PASSPHRASE=$PASSPHRASE /usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /usr/bin/borg prune -v --list $REPOSITORY --prefix '{hostname}-' \
2017-04-15 02:46:20 +00:00
--keep-hourly=6 --keep-daily=7 --keep-weekly=4 --keep-monthly=6