diff --git a/backup.sh b/backup.sh index 6841dc1..9e7b1b3 100755 --- a/backup.sh +++ b/backup.sh @@ -1,12 +1,14 @@ #!/bin/bash +# project name (by default directory name of the compose project) +PROJECT_NAME=nextcloud # backup directory SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" backupDir=${SCRIPT_DIR}/backup # create directories -mkdir -p ${backupDir}/app +mkdir -p ${backupDir}/config mkdir -p ${backupDir}/docker-conf mkdir -p ${backupDir}/data mkdir -p ${backupDir}/db @@ -14,21 +16,22 @@ # current date currentDate=$(date +"%Y-%m-%d_%H-%M-%S") -# backup mysql +# backup database dbFile="${backupDir}/db/${currentDate}.sql" -docker exec $(docker container ls -qf name=nextcloud_database) sh -c 'mysqldump --single-transaction=TRUE --lock-tables --default-character-set=utf8mb4 -uroot -p"${MYSQL_ROOT_PASSWORD}" ${MYSQL_DATABASE}' > ${dbFile} +docker exec $(docker container ls -qf name=${PROJECT_NAME}_database) sh -c 'mysqldump --single-transaction=TRUE --lock-tables --default-character-set=utf8mb4 -uroot -p"${MYSQL_ROOT_PASSWORD}" ${MYSQL_DATABASE}' > ${dbFile} sed -i "/^mysqldump: \\[Warning\\]/d" ${dbFile} tar -cj ${dbFile} -f "${dbFile}.tbz2" rm ${dbFile} -# backup app data as root because of permission problems -sudo tar -C data -I pbzip2 -pc app -f "${backupDir}/app/${currentDate}.tbz2" +# backup nextcloud conf directory as root because of permission problems +docker run --rm -v ${PROJECT_NAME}_app-vol-1:/volume -v "${backupDir}/config":/backup debian:buster-slim bash -c "cd /volume/config && tar -czf /backup/${currentDate}.tar ." &> /dev/null # backup data as root because of permission problems -#sudo tar -C data -I pbzip2 -pc data -f "${backupDir}/data/${currentDate}.tbz2" +docker run --rm -v ${PROJECT_NAME}_data-vol-1:/volume -v "${backupDir}/data":/backup debian:buster-slim bash -c "cd /volume && tar -czf /backup/${currentDate}.tar ." &> /dev/null -# backup config files -tar -pcj nextcloud.conf -f "${backupDir}/docker-conf/${currentDate}.tbz2" +# backup docker config files +tar -pcj nextcloud.conf db.env cloud.env -f "${backupDir}/docker-conf/${currentDate}.tbz2" # delete all files older 30 days find ${backupDir} -iname "*.tbz2" -type f -mtime +10 -exec rm {} \; > /dev/null +