backup_volumes.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. #
  3. # Docker Volume Backup Script
  4. #
  5. # This script is designed to back up Docker volumes to a specified directory.
  6. # It checks if each volume is empty before attempting to back it up and creates
  7. # compressed .tar.bz2 backup files for non-empty volumes.
  8. #
  9. # Prerequisites
  10. #
  11. # Docker: Ensure Docker is installed and running on your system.
  12. # Bash: This script is written in Bash and should be run in a Bash-compatible shell.
  13. # Backup Directory: Ensure the backup directory specified in the script exists or is writable.
  14. #
  15. #Script Overview
  16. #
  17. #The script performs the following tasks:
  18. #
  19. # Defines Variables:
  20. # BACKUP_DIR: The directory where backup files will be stored.
  21. # TIMESTAMP: A timestamp used to uniquely identify backup files.
  22. # VOLUMES: An array of Docker volume names to back up.
  23. #
  24. # Checks for Empty Volumes:
  25. # The script uses a temporary Docker container to check if a volume is empty. If a volume is empty, it skips the backup for that volume.
  26. #
  27. # Backs Up Volumes:
  28. # For each non-empty volume, the script creates a compressed .tar.bz2 backup file in the specified backup directory.
  29. # The backup is performed using the loomchild/volume-backup Docker image.
  30. #
  31. # Verifies Backup Success:
  32. # After each backup, the script checks if the backup was successful. If any backup fails, the script exits with an error.
  33. #
  34. # Outputs Status:
  35. # The script provides status messages for each step, including skipped volumes, successful backups, and any errors encountered.
  36. #
  37. # Variables
  38. BACKUP_DIR="/media/yaye/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory
  39. TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
  40. # List of volumes to backup
  41. VOLUMES=("nc4_files" "nc4_oo_data" "nc4_es_index" "nc4_db" "nc4_clamav") # Replace with your volume names
  42. #VOLUMES=("nc4_redis" "nc4_oo_data" "nc4_es_index" "nc4_clamav") # Replace with your volume names
  43. # Ensure backup directory exists
  44. mkdir -p "$BACKUP_DIR"
  45. # Function to check if a volume is empty
  46. is_volume_empty() {
  47. local volume_name=$1
  48. # Use a temporary container to check if the volume is empty
  49. if docker run --rm -v "$volume_name:/volume" alpine sh -c '[ -z "$(ls -A /volume)" ]'; then
  50. return 0 # Volume is empty
  51. else
  52. return 1 # Volume is not empty
  53. fi
  54. }
  55. # Loop through each volume and perform the backup
  56. for volume in "${VOLUMES[@]}"; do
  57. # Check if the volume is empty
  58. if is_volume_empty "$volume"; then
  59. echo "Skipping empty volume: $volume"
  60. continue
  61. fi
  62. # Define the backup file name
  63. BACKUP_FILE="$BACKUP_DIR/${volume}_backup-${TIMESTAMP}.tar.bz2"
  64. # Create a temporary container to backup the volume
  65. echo "Backing up volume: $volume to $BACKUP_FILE"
  66. # docker run --rm \
  67. # -v "$BACKUP_DIR:/backup" \
  68. # -v "$volume:/volume" \
  69. # alpine tar czf "/backup/${volume}_backup_${sequence}_${TIMESTAMP}.tar.bz2" -C / volume
  70. docker run -v "$volume:/volume" -v "$BACKUP_DIR:/backup" --rm loomchild/volume-backup backup -v ${volume}_backup-${TIMESTAMP}
  71. # Check if the backup was successful
  72. if [ $? -eq 0 ]; then
  73. echo "Backup completed successfully: $BACKUP_FILE"
  74. else
  75. echo "Backup failed for volume: $volume!"
  76. exit 1
  77. fi
  78. done
  79. echo "All backups completed successfully!"