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