backup-automation/nextcloud/nextcloud-backup.sh

63 lines
2.1 KiB
Bash
Raw Normal View History

2024-08-04 23:10:36 +02:00
#!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
# shellcheck disable=SC1090
source "$SCRIPT_ABS_LOCATION/nextcloud-backup.env"
# shellcheck disable=SC1090
source "$SCRIPT_ABS_LOCATION/../logger.sh"
2024-08-04 23:10:36 +02:00
# Container Names
APP_CONTAINER="nextcloud-app"
DB_CONTAINER="nextcloud-db"
BACKUP_CONTAINER="duplicati"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/nextcloud-backup.log"
2024-08-04 23:10:36 +02:00
# Database Settings
DB_USER="nextcloud"
SOURCE_DIR="/mnt/data/nextcloud"
# Put Nextcloud in Maintenance Mode
log "Putting Nextcloud in maintenance mode..."
docker exec -u www-data "$APP_CONTAINER" php occ maintenance:mode --on || { log "Error: Failed to put Nextcloud in maintenance mode."; exit 1; }
2024-08-04 23:10:36 +02:00
# Dump Database
log "Dumping Nextcloud database..."
DB_TMP_BAK_NAME="nextcloud-db_$(date +"%Y%m%d").bak"
docker exec "$DB_CONTAINER" /usr/bin/mariadb-dump \
2024-08-04 23:10:36 +02:00
--single-transaction \
-h localhost \
-u "$DB_USER" \
2024-08-04 23:10:36 +02:00
-p"$DB_PASSWORD" \
nextcloud > "$SOURCE_DIR/$DB_TMP_BAK_NAME" || { log "Error: Failed to dump the Nextcloud database."; exit 1; }
2024-08-04 23:10:36 +02:00
# Backup all files to target destination
log "Backing up Nextcloud files and database..."
docker exec "$BACKUP_CONTAINER" \
2024-08-04 23:10:36 +02:00
duplicati-cli backup \
"ssh://$BACKUP_DESTINATION" \
2024-08-04 23:10:36 +02:00
"$SOURCE_DIR/html/data" \
"$SOURCE_DIR/html/config" \
"$SOURCE_DIR/html/themes" \
"$SOURCE_DIR/$DB_TMP_BAK_NAME" \
--backup-name="nextcloud backup" \
--keep-versions=7 \
--auth-username="$SFTP_USERNAME" \
--auth-password="$SFTP_PASSWORD" \
--passphrase="$BACKUP_ENCR_PASSPHRASE" \
2024-08-04 23:10:36 +02:00
--ssh-fingerprint="$SFTP_FINGERPRINT" \
--prefix="nextcloud" || { log "Error: Failed to backup Nextcloud files and database."; exit 1; }
# Turn off Maintenance Mode
log "Turning off Nextcloud maintenance mode..."
docker exec -u www-data "$APP_CONTAINER" php occ maintenance:mode --off || { log "Error: Failed to turn off Nextcloud maintenance mode."; exit 1; }
2024-08-04 23:10:36 +02:00
# Delete temporary backup files
log "Deleting temporary backup files..."
rm "$SOURCE_DIR/$DB_TMP_BAK_NAME" || { log "Error: Failed to delete temporary databse file"; exit 1; }
2024-08-04 23:10:36 +02:00
log "Nextcloud backup completed successfully."