restore_volumes.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. # Docker Volume Restore Script
  3. #
  4. # This script is designed to restore Docker volumes from backup files.
  5. # It allows you to specify a backup directory, a timestamp for the backup files,
  6. # and a list of original and new volume names. The script ensures that the number of original volumes
  7. # matches the number of new volumes and provides an option for a dry-run to simulate the restore process without making any changes.
  8. #
  9. # Prerequisites
  10. #
  11. #
  12. # Docker: Ensure Docker is installed and running on your system.
  13. # Backup Files: The backup files should be in the specified backup directory and follow the naming convention: <volume_name>_backup-<timestamp>.tar.bz2.
  14. #
  15. # Script Variables
  16. #
  17. # BACKUP_DIR: The directory where the backup files are stored. Update this to your desired backup directory.
  18. # TIMESTAMP: The timestamp used in the backup file names. Replace this with your desired timestamp.
  19. # VOLUMES: An array of original volume names that were backed up.
  20. # NEW_VOLUMES: An array of new volume names to which the backups will be restored.
  21. #
  22. # Usage
  23. #
  24. # Set Variables: Update the BACKUP_DIR, TIMESTAMP, VOLUMES, and NEW_VOLUMES variables in the script to match your environment.
  25. #
  26. # Run the Script:
  27. # To perform a dry-run (simulate the restore process without making changes):
  28. #
  29. # ./restore_volumes.sh --dry-run
  30. #
  31. # To perform the actual restore:
  32. #
  33. # ./restore_volumes.sh
  34. #
  35. #
  36. # Variables
  37. #BACKUP_DIR="/media/yazoo/wdcrypt/2025-docker-data-migration/temp-nc5-backup" # Change this to your desired backup directory
  38. #BACKUP_DIR="/media/yazoo/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory
  39. BACKUP_DIR="/media/yazoo/Crucial-1T/restore_test_duplicati/backup_nc7" # Change this to your desired backup directory
  40. TIMESTAMP="20260102_230607" # Replace with your desired timestamp
  41. # List of volumes to restore
  42. VOLUME=nc7
  43. NEW_VOLUME=nc8
  44. #VOLUMES=("${VOLUME}_files" "${VOLUME}_oo_data" "${VOLUME}_es_index" "${VOLUME}_db" "${VOLUME}_clamav") # Replace with your original volume names
  45. VOLUMES=("${VOLUME}_files" "${VOLUME}_oo_data" "${VOLUME}_es_index" "${VOLUME}_db" ) # Replace with your original volume names
  46. #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
  47. NEW_VOLUMES=(${NEW_VOLUME}_files "${NEW_VOLUME}_oo_data" "${NEW_VOLUME}_es_index" "${NEW_VOLUME}_db" ) # Replace with your new volume names
  48. # Ensure backup directory exists
  49. mkdir -p "$BACKUP_DIR"
  50. # Check if the number of original volumes matches the number of new volumes
  51. if [ ${#VOLUMES[@]} -ne ${#NEW_VOLUMES[@]} ]; then
  52. echo "Error: Number of original volumes does not match the number of new volumes!"
  53. exit 1
  54. fi
  55. # Parse command-line options
  56. while [ $# -gt 0 ]; do
  57. case $1 in
  58. --dry-run)
  59. DRY_RUN=1
  60. ;;
  61. *)
  62. echo "Error: Unknown option: $1"
  63. exit 1
  64. ;;
  65. esac
  66. shift
  67. done
  68. # Loop through each volume and perform the restore
  69. for ((i=0; i<${#VOLUMES[@]}; i++)); do
  70. # Define the backup file name
  71. BACKUP_FILE="$BACKUP_DIR/${VOLUMES[$i]}_backup-${TIMESTAMP}.tar.bz2"
  72. # Check if the backup file exists
  73. if [ ! -f "$BACKUP_FILE" ]; then
  74. echo "Backup file not found: $BACKUP_FILE"
  75. exit 1
  76. fi
  77. # Create a new volume (only if not in dry-run mode)
  78. if [ -z "$DRY_RUN" ]; then
  79. docker volume create "${NEW_VOLUMES[$i]}"
  80. fi
  81. # Print a message indicating the restore operation (only if in dry-run mode)
  82. if [ -n "$DRY_RUN" ]; then
  83. echo "Would restore volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]} from $BACKUP_FILE"
  84. else
  85. # Create a temporary container to restore the volume
  86. echo "Restoring volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]} from $BACKUP_FILE"
  87. docker run -v "${NEW_VOLUMES[$i]}:/volume" -v "$BACKUP_DIR:/backup" --rm loomchild/volume-backup restore -v ${VOLUMES[$i]}_backup-${TIMESTAMP}
  88. # Check if the restore was successful
  89. if [ $? -eq 0 ]; then
  90. echo "Restore completed successfully: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]}"
  91. else
  92. echo "Restore failed for volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]}!"
  93. exit 1
  94. fi
  95. fi
  96. done
  97. echo "All restores completed successfully!"