Blame
| 309fd4 | theophile | 2025-10-12 22:16:25 | 1 | # Nextcloud |
| 2 | ||||
| 3 | Le backup export en utilisant la commande Export du Snap vers le |
|||
| 4 | répertoire par défaut et non changeable |
|||
| 5 | `/var/snap/nextcloud/common/backups`. Celui si est "symlinké" avec un |
|||
| 6 | répertoire sur les disques grandes capacités `/mnt/data/backup`. |
|||
| 7 | ||||
| 8 | Le fichier obtenu est compressé et envoyé par rsync vers le NAS Synology |
|||
| 9 | FGM-ARBRE. Pour cela l'utilisateur rsync a été créé avec autorisation |
|||
| 10 | d'accès rsync, et répertoire de backup sur Arbre. |
|||
| 11 | ||||
| 12 | Le répertoire de données Nextcloud `/data` est directement synchronisé |
|||
| 13 | avec son propre répertoire sur le backup d'Arbre. |
|||
| 14 | ||||
| 15 | Les logs sont sauvegardés dans `/var/log/`. |
|||
| 16 | ||||
| 17 | Les répertoires intermédiaires sont vidés avant traitement, les logs |
|||
| 18 | sont limités ansi que le nombre de fichier compressé de Nexcloud |
|||
| 19 | (database, paramétrages...) |
|||
| 20 | ||||
| 21 | ``` bash |
|||
| 22 | #!/bin/bash |
|||
| 23 | ||||
| 24 | # Variables |
|||
| 25 | NEXTCLOUD_EXPORT_DIR="/var/snap/nextcloud/common/backups" # Default Nextcloud Snap export directory |
|||
| 26 | LOCAL_TEMP_DIR="/mnt/data/backup/temp" # Temporary local storage |
|||
| 27 | NAS_USER="rsync" # Synology NAS username |
|||
| 28 | NAS_HOST="192.168.1.41" # Synology NAS hostname or IP |
|||
| 29 | SRV_CERTIFICATE="~/.ssh/rsync_key" |
|||
| 30 | NAS_BACKUP_DIR="/volume1/NextcloudBackUp" # Destination path on Synology NAS |
|||
| 31 | DATE=$(date +'%Y-%m-%d_%H-%M-%S') # Timestamp for backup |
|||
| 32 | LOG_DIR="/var/log" # Log directory |
|||
| 33 | LOG_FILE="$LOG_DIR/nextcloud_backup_$DATE.log" # New log file with date-based name |
|||
| 34 | MAX_BACKUPS=5 # Maximum number of backups to keep on NAS |
|||
| 35 | CONFIG_DB_ARCHIVE="$LOCAL_TEMP_DIR/nextcloud_config_db_$DATE.tar.gz" # Compressed export for config and DB |
|||
| 36 | DATA_DIR="/mnt/ncdata/data" # Nextcloud data directory |
|||
| 37 | ||||
| 38 | # Ensure local temporary storage directory exists |
|||
| 39 | mkdir -p "$LOCAL_TEMP_DIR" |
|||
| 40 | ||||
| 41 | # Clean up old log files to keep only the latest 10 |
|||
| 42 | echo "[$(date)] Cleaning up old log files..." | tee -a "$LOG_FILE" |
|||
| 43 | ls -t $LOG_DIR/nextcloud_backup*.log | tail -n +11 | xargs rm -f |
|||
| 44 | ||||
| 45 | if [ $? -ne 0 ]; then |
|||
| 46 | echo "[$(date)] ERROR: Failed to clean up old log files." | tee -a "$LOG_FILE" |
|||
| 47 | exit 1 |
|||
| 48 | fi |
|||
| 49 | echo "[$(date)] Old log files cleaned successfully." | tee -a "$LOG_FILE" |
|||
| 50 | ||||
| 51 | # Clean up the Nextcloud export directory before starting |
|||
| 52 | echo "[$(date)] Cleaning up the Nextcloud export directory..." | tee -a "$LOG_FILE" |
|||
| 53 | sudo rm -rf -- "$NEXTCLOUD_EXPORT_DIR"/* |
|||
| 54 | if [ $? -ne 0 ]; then |
|||
| 55 | echo "[$(date)] ERROR: Failed to clean up the Nextcloud export directory." | tee -a "$LOG_FILE" |
|||
| 56 | exit 1 |
|||
| 57 | fi |
|||
| 58 | sudo rm -rf -- "$LOCAL_TEMP_DIR"/* |
|||
| 59 | if [ $? -ne 0 ]; then |
|||
| 60 | echo "[$(date)] ERROR: Failed to clean up the Temporary Compress export directory." | tee -a "$LOG_FILE" |
|||
| 61 | exit 1 |
|||
| 62 | fi |
|||
| 63 | echo "[$(date)] Nextcloud export directories cleaned successfully." | tee -a "$LOG_FILE" |
|||
| 64 | ||||
| 65 | # Perform full export of config and database using Snap export |
|||
| 66 | echo "[$(date)] Starting full export of Nextcloud configuration and database..." | tee -a "$LOG_FILE" |
|||
| 67 | sudo nextcloud.export -abc > /dev/null 2>&1 |
|||
| 68 | ||||
| 69 | if [ $? -ne 0 ]; then |
|||
| 70 | echo "[$(date)] ERROR: Full export of Nextcloud configuration and database failed." | tee -a "$LOG_FILE" |
|||
| 71 | exit 1 |
|||
| 72 | fi |
|||
| 73 | echo "[$(date)] Export completed successfully." | tee -a "$LOG_FILE" |
|||
| 74 | ||||
| 75 | # Compress the exported config and database |
|||
| 76 | echo "[$(date)] Compressing exported configuration and database..." | tee -a "$LOG_FILE" |
|||
| 77 | tar -czf "$CONFIG_DB_ARCHIVE" -C "$NEXTCLOUD_EXPORT_DIR" . |
|||
| 78 | ||||
| 79 | if [ $? -ne 0 ]; then |
|||
| 80 | echo "[$(date)] ERROR: Compression of configuration and database export failed." | tee -a "$LOG_FILE" |
|||
| 81 | exit 1 |
|||
| 82 | fi |
|||
| 83 | ||||
| 84 | # Transfer the compressed config and database archive to Synology NAS |
|||
| 85 | echo "[$(date)] Transferring compressed configuration and database to Synology NAS..." | tee -a "$LOG_FILE" |
|||
| 86 | rsync -a -e "ssh -i ~/.ssh/rsync_key" "$CONFIG_DB_ARCHIVE" "$NAS_USER@$NAS_HOST:$NAS_BACKUP_DIR/" |
|||
| 87 | ||||
| 88 | if [ $? -ne 0 ]; then |
|||
| 89 | echo "[$(date)] ERROR: Transfer of configuration and database to Synology NAS failed." | tee -a "$LOG_FILE" |
|||
| 90 | exit 1 |
|||
| 91 | fi |
|||
| 92 | ||||
| 93 | # Sync the data directory directly to Synology NAS |
|||
| 94 | echo "[$(date)] Syncing data directory to Synology NAS..." | tee -a "$LOG_FILE" |
|||
| 95 | rsync -a -e "ssh -i ~/.ssh/rsync_key" --delete "$DATA_DIR/" "$NAS_USER@$NAS_HOST:$NAS_BACKUP_DIR/data/" |
|||
| 96 | ||||
| 97 | if [ $? -ne 0 ]; then |
|||
| 98 | echo "[$(date)] ERROR: Sync of data directory to Synology NAS failed." | tee -a "$LOG_FILE" |
|||
| 99 | exit 1 |
|||
| 100 | fi |
|||
| 101 | ||||
| 102 | # Remove temporary local files to minimize space usage |
|||
| 103 | echo "[$(date)] Cleaning up local temporary files..." | tee -a "$LOG_FILE" |
|||
| 104 | rm -rf "$CONFIG_DB_ARCHIVE" "$LOCAL_TEMP_DIR"/* |
|||
| 105 | ||||
| 106 | if [ $? -ne 0 ]; then |
|||
| 107 | echo "[$(date)] ERROR: Failed to clean up local temporary files." | tee -a "$LOG_FILE" |
|||
| 108 | exit 1 |
|||
| 109 | fi |
|||
| 110 | ||||
| 111 | # Rotate backups on the Synology NAS to keep only the latest $MAX_BACKUPS |
|||
| 112 | echo "[$(date)] Rotating backups on Synology NAS..." | tee -a "$LOG_FILE" |
|||
| 113 | ssh "$NAS_USER@$NAS_HOST" "ls -t $NAS_BACKUP_DIR | tail -n +$(($MAX_BACKUPS + 1)) | xargs -I {} rm -rf "$NAS_BACKUP_DIR/{}"" |
|||
| 114 | ||||
| 115 | if [ $? -ne 0 ]; then |
|||
| 116 | echo "[$(date)] ERROR: Backup rotation on Synology NAS failed." | tee -a "$LOG_FILE" |
|||
| 117 | exit 1 |
|||
| 118 | fi |
|||
| 119 | ||||
| 120 | echo "[$(date)] Backup process completed successfully." | tee -a "$LOG_FILE" |
|||
| 121 | exit 0 |
|||
| 122 | ``` |