From 6454a01c2ef466ee31a67b5296c0ddc02bbbe2ca Mon Sep 17 00:00:00 2001 From: foobar Date: Wed, 2 Feb 2022 01:13:20 +0100 Subject: [PATCH] init --- backup.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..e44844e --- /dev/null +++ b/backup.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# check script curently running and exit or continue +me="$(basename "$0")"; +result=$(ps aux | grep -i "$me" | grep -v "grep" | wc -l) +echo "running: $result" +if [ "$result" -gt 3 ] + then + exit +fi + +# declarations +DIR_VOLUMES=/mnt/NASbarracuda1TB/keese/seafile-arm/volumes # path to seafile docker volumes dir +DIR_BACKUP=/mnt/NASbarracuda1TB/backup/keese/seafile # where to store the backup + +# mysql credentials to access databases inside seafile-mysql docker +MYSQL_USER=root +MYSQL_PASSWORD=mysqlrootpasswd + +# specify seafile-mysql docker name and temporary backup location inside the docker +DOCKER_SEAFILE_MYSQL=seafile-mysql + + +#------------------------------------------------------------ +# warning: only change below if you know what you are doing! + + +# build dependend dir paths +DIR_BACKUP_DB=${DIR_BACKUP}/db +TIMESTAMP=$(date +"%Y-%m-%d-%H-%M-%S") +DOCKER_BACKUP_DIR=/home/backup +DOCKER_BACKUP_DIR_TIMESTAMP="${DOCKER_BACKUP_DIR}/${TIMESTAMP}" +DIR_BACKUP_LOGS=${DIR_BACKUP}/logs +LOGFILE_STATUS="${TIMESTAMP}_rsync_status.log" +LOGFILE_PATH="${DIR_BACKUP_LOGS}/${LOGFILE_STATUS}" + + +# 1) backup seafile databases +docker exec -ti ${DOCKER_SEAFILE_MYSQL} sh -c "mkdir -p ${DOCKER_BACKUP_DIR_TIMESTAMP}" # create remporary backup dir inside docker +IFS=$'\n\r' # set the IFS to a newline character, so that a whole line can be assigned to an array element +DATABASES=( $(docker exec -ti ${DOCKER_SEAFILE_MYSQL} sh -c "mysql -e 'show databases' -s --skip-column-names -u ${MYSQL_USER} -p${MYSQL_PASSWORD}") ) # parse database names into array + +# loop databases and dump tables to sql files inside temporary docker backup folder +for i in "${DATABASES[@]:1}" +do + : + docker exec -ti ${DOCKER_SEAFILE_MYSQL} sh -c "mysqldump --single-transaction -h localhost -u ${MYSQL_USER} -p${MYSQL_PASSWORD} --opt ${i} > ${DOCKER_BACKUP_DIR_TIMESTAMP}/${i}.sql" +done + +# 2) copy backup database files to host and cleanup +docker cp ${DOCKER_SEAFILE_MYSQL}:${DOCKER_BACKUP_DIR_TIMESTAMP} ${DIR_BACKUP_DB} +docker exec -ti ${DOCKER_SEAFILE_MYSQL} sh -c "rm -r ${DOCKER_BACKUP_DIR}" + +# 3) backup seafile docker volumes folder +rsync -azh --log-file "${LOGFILE_PATH}" ${DIR_VOLUMES} ${DIR_BACKUP}