Hugo/backups/create_nextcloud_backup.sh

41 lines
1.4 KiB
Bash
Raw Normal View History

2023-01-14 23:59:00 +01:00
#!/bin/bash
# Backup script for Nextcloud in a kubernetes cluster
BACKUP_DEST='/mnt/PRIVATE_DOCS/home/backup'
PASSFILE='./nextcloud_pass.txt'
# Create filename for database
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
>&2 echo '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
>&2 echo '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
>&2 echo 'Done'