#!/bin/bash # Docker Volume Restore Script # # This script is designed to restore Docker volumes from backup files. # It allows you to specify a backup directory, a timestamp for the backup files, # and a list of original and new volume names. The script ensures that the number of original volumes # matches the number of new volumes and provides an option for a dry-run to simulate the restore process without making any changes. # # Prerequisites # # # Docker: Ensure Docker is installed and running on your system. # Backup Files: The backup files should be in the specified backup directory and follow the naming convention: _backup-.tar.bz2. # # Script Variables # # BACKUP_DIR: The directory where the backup files are stored. Update this to your desired backup directory. # TIMESTAMP: The timestamp used in the backup file names. Replace this with your desired timestamp. # VOLUMES: An array of original volume names that were backed up. # NEW_VOLUMES: An array of new volume names to which the backups will be restored. # # Usage # # Set Variables: Update the BACKUP_DIR, TIMESTAMP, VOLUMES, and NEW_VOLUMES variables in the script to match your environment. # # Run the Script: # To perform a dry-run (simulate the restore process without making changes): # # ./restore_volumes.sh --dry-run # # To perform the actual restore: # # ./restore_volumes.sh # # # Variables BACKUP_DIR="/media/yaye/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory TIMESTAMP="20250131_174358" # Replace with your desired timestamp # List of volumes to restore VOLUME=nc4 NEW_VOLUME=nc4 VOLUMES=("${VOLUME}_files" "${VOLUME}_oo_data" "${VOLUME}_es_index" "${VOLUME}_db" "${VOLUME}_clamav") # Replace with your original volume names NEW_VOLUMES=(${NEW_VOLUME}_files "${NEW_VOLUME}_oo_data" "${NEW_VOLUME}_es_index" "${NEW_VOLUME}_db" "${NEW_VOLUME}_clamav") # Replace with your new volume names # Ensure backup directory exists mkdir -p "$BACKUP_DIR" # Check if the number of original volumes matches the number of new volumes if [ ${#VOLUMES[@]} -ne ${#NEW_VOLUMES[@]} ]; then echo "Error: Number of original volumes does not match the number of new volumes!" exit 1 fi # Parse command-line options while [ $# -gt 0 ]; do case $1 in --dry-run) DRY_RUN=1 ;; *) echo "Error: Unknown option: $1" exit 1 ;; esac shift done # Loop through each volume and perform the restore for ((i=0; i<${#VOLUMES[@]}; i++)); do # Define the backup file name BACKUP_FILE="$BACKUP_DIR/${VOLUMES[$i]}_backup-${TIMESTAMP}.tar.bz2" # Check if the backup file exists if [ ! -f "$BACKUP_FILE" ]; then echo "Backup file not found: $BACKUP_FILE" exit 1 fi # Create a new volume (only if not in dry-run mode) if [ -z "$DRY_RUN" ]; then docker volume create "${NEW_VOLUMES[$i]}" fi # Print a message indicating the restore operation (only if in dry-run mode) if [ -n "$DRY_RUN" ]; then echo "Would restore volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]} from $BACKUP_FILE" else # Create a temporary container to restore the volume echo "Restoring volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]} from $BACKUP_FILE" docker run -v "${NEW_VOLUMES[$i]}:/volume" -v "$BACKUP_DIR:/backup" --rm loomchild/volume-backup restore -v ${VOLUMES[$i]}_backup-${TIMESTAMP} # Check if the restore was successful if [ $? -eq 0 ]; then echo "Restore completed successfully: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]}" else echo "Restore failed for volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]}!" exit 1 fi fi done echo "All restores completed successfully!"