Last active
August 27, 2019 12:41
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
# Folders | |
BACKUPFOLDER="/tank/backup" | |
DESTINATION="/HDD/nextcloud-backup" | |
# Nextcloud volume | |
NEXTCLOUDVOLUME="tank/nextcloud" | |
# Files | |
DOCKERCOMPOSEFILE="/home/edu/nextcloud/docker-compose.yml" | |
DBENVFILE="/home/edu/nextcloud/db.env" | |
DBFILE="${BACKUPFOLDER}/nextcloud.sql" | |
EXCLUDEDFILES="/home/edu/nextcloud/excluded-files.txt" | |
# Others | |
HOST="xxx.example.com" | |
DESTUSER="edu" | |
SSHKEY="/home/edu/.ssh/id_rsa" | |
SSHPORT="8888" | |
BWLIMIT="2048" | |
# Get mysql credentials | |
source ${DBENVFILE} | |
DATE=$(date "+%Y-%m-%dT%H_%M_%S") | |
echo "Started at ${DATE}" | |
echo "Checking /HDD" | |
ssh ${HOST} -p ${SSHPORT} \ | |
"grep /HDD /proc/mounts -q || exit 2" | |
# Set nextcloud to maintenance mode | |
docker-compose -f ${DOCKERCOMPOSEFILE} exec -T --user www-data app \ | |
php occ maintenance:mode --on | |
# Dump mysqldatabase | |
docker-compose -f ${DOCKERCOMPOSEFILE} exec db \ | |
sh -c 'exec mysqldump --single-transaction -u${MYSQL_USER} -p"${MYSQL_PASSWORD}" "${MYSQL_DATABASE}"' > ${DBFILE} | |
# Create a zfs snapshot | |
sudo zfs snapshot ${NEXTCLOUDVOLUME}@backup | |
# Restart nextcloud | |
docker-compose -f ${DOCKERCOMPOSEFILE} exec -T --user www-data app \ | |
php occ maintenance:mode --off | |
# Create a symlink in the destination folder to the zfs snapshot (readonly) | |
ln -s /${NEXTCLOUDVOLUME}/.zfs/snapshot/backup/ ${BACKUPFOLDER}/nextcloud | |
cd ${BACKUPFOLDER} | |
# Perform the backup | |
# https://blog.interlinked.org/tutorials/rsync_time_machine.html | |
# Removed compression due to https://gist.github.com/KartikTalwar/4393116 | |
sudo rsync -aPL \ | |
-e "ssh -l ${DESTUSER} -i ${SSHKEY} -p ${SSHPORT} -o Compression=no" \ | |
--delete \ | |
--delete-excluded \ | |
--exclude-from ${EXCLUDEDFILES} \ | |
--bwlimit=${BWLIMIT} \ | |
--link-dest=${DESTINATION}/current \ | |
${BACKUPFOLDER}/ ${HOST}:${DESTINATION}/incomplete_back-${DATE} | |
# Organize the stuff in the backup host | |
ssh ${HOST} -p ${SSHPORT}\ | |
"mv ${DESTINATION}/incomplete_back-${DATE} ${DESTINATION}/back-${DATE} && \ | |
rm -f ${DESTINATION}/current && \ | |
ln -s ${DESTINATION}/back-${DATE} ${DESTINATION}/current" | |
# Clean up the symlink | |
rm -f ${BACKUPFOLDER}/nextcloud | |
# Clena up the database | |
rm -f ${DBFILE} | |
# Destroy the snapshot | |
sudo zfs destroy ${NEXTCLOUDVOLUME}@backup | |
DATEEND=$(date "+%Y-%m-%dT%H_%M_%S") | |
echo "Finished at ${DATEEND}" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment