Hugo/backups/create_nextcloud_backup.sh

46 lines
1.5 KiB
Bash
Raw Normal View History

2023-01-14 23:59:00 +01:00
#!/bin/bash
2023-09-03 12:25:37 +02:00
# Backup script for Nextcloud in a Kubernetes cluster
PROGRAM_NAME='backup_nextcloud'
printlog () {
printf '%s: %s\n' "${PROGRAM_NAME}" "${1}"
}
2023-01-14 23:59:00 +01:00
BACKUP_DEST='/mnt/PRIVATE_DOCS/home/backup'
PASSFILE='./nextcloud_pass.txt'
# Create filename for database
2023-09-03 12:25:37 +02:00
database_backupfile="nextcloud-sqlbkp_$( date +'%Y%m%d' ).bak"
2023-01-14 23:59:00 +01:00
# Retrieve container names
base_container="$( docker ps --format '{{.Names}}' | grep hugocloud-nextcloud_hugocloud-nextcloud )"
database_container="$( docker ps --format '{{.Names}}' | grep hugocloud-postgresql_hugocloud-postgresql )"
2023-01-14 23:59:00 +01:00
# Abort entire script if any command fails
set -e
# Turn on maintenance mode
docker exec --user www-data "${base_container}" php occ maintenance:mode --on
2023-01-14 23:59:00 +01:00
# Database backup
2023-09-03 12:25:37 +02:00
printlog 'Backing up database'
internal_database_backupfile="/tmp/${database_backupfile}"
docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' -f "${internal_database_backupfile}"
docker cp "${database_container}":"${internal_database_backupfile}" "${BACKUP_DEST}"
# Files backup
for file in 'config' 'themes'; do
>&2 printf 'Copying %s\n' "${file}"
docker cp -a "${base_container}":"/var/www/html/${file}" "${BACKUP_DEST}"
done
2023-01-14 23:59:00 +01:00
# Turn off maintenance mode
docker exec --user www-data "${base_container}" php occ maintenance:mode --off
# Backup cleanup
# Only keep 30 days of backups
2023-09-03 12:25:37 +02:00
printlog 'Clean up old database backups'
2023-06-28 18:08:24 +02:00
find "${BACKUP_DEST}" -name '*sqlbkp*' -type f -mtime +30 -print -delete
2023-01-14 23:59:00 +01:00
2023-09-03 12:25:37 +02:00
printlog 'Done'