restore_db.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory
  38. #TIMESTAMP="20250201_015158" # Replace with your desired timestamp
  39. TIMESTAMP="20250219_194346" # Replace with your desired timestamp
  40. # List of volumes to restore
  41. VOLUME=nc5
  42. NEW_VOLUME=nc6
  43. VOLUMES=("${VOLUME}_db" ) # Replace with your original volume names
  44. NEW_VOLUMES=("${NEW_VOLUME}_db") # Replace with your new volume names
  45. # Ensure backup directory exists
  46. mkdir -p "$BACKUP_DIR"
  47. # Check if the number of original volumes matches the number of new volumes
  48. if [ ${#VOLUMES[@]} -ne ${#NEW_VOLUMES[@]} ]; then
  49. echo "Error: Number of original volumes does not match the number of new volumes!"
  50. exit 1
  51. fi
  52. # Parse command-line options
  53. while [ $# -gt 0 ]; do
  54. case $1 in
  55. --dry-run)
  56. DRY_RUN=1
  57. ;;
  58. *)
  59. echo "Error: Unknown option: $1"
  60. exit 1
  61. ;;
  62. esac
  63. shift
  64. done
  65. # Loop through each volume and perform the restore
  66. for ((i=0; i<${#VOLUMES[@]}; i++)); do
  67. # Define the backup file name
  68. BACKUP_FILE="$BACKUP_DIR/${VOLUMES[$i]}_backup-${TIMESTAMP}.tar.bz2"
  69. # Check if the backup file exists
  70. if [ ! -f "$BACKUP_FILE" ]; then
  71. echo "Backup file not found: $BACKUP_FILE"
  72. exit 1
  73. fi
  74. # Create a new volume (only if not in dry-run mode)
  75. if [ -z "$DRY_RUN" ]; then
  76. docker volume create "${NEW_VOLUMES[$i]}"
  77. fi
  78. # Print a message indicating the restore operation (only if in dry-run mode)
  79. if [ -n "$DRY_RUN" ]; then
  80. echo "Would restore volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]} from $BACKUP_FILE"
  81. else
  82. # Create a temporary container to restore the volume
  83. echo "Restoring volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]} from $BACKUP_FILE"
  84. docker run -v "${NEW_VOLUMES[$i]}:/volume" -v "$BACKUP_DIR:/backup" --rm loomchild/volume-backup restore -v ${VOLUMES[$i]}_backup-${TIMESTAMP}
  85. # Check if the restore was successful
  86. if [ $? -eq 0 ]; then
  87. echo "Restore completed successfully: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]}"
  88. else
  89. echo "Restore failed for volume: ${VOLUMES[$i]} to ${NEW_VOLUMES[$i]}!"
  90. exit 1
  91. fi
  92. fi
  93. done
  94. echo "All restores completed successfully!"