backup_volumes.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. # WARNING:
  38. # verify assigned values for these variables before proceeding:
  39. #
  40. # BACKUP_DIR - verify the location is accessible. The mount point is named by the login user so it may look different
  41. # VOLUMES - are all the volumes to backup include in the list?
  42. # <volume>_nc8_files: stores the core admin account and binaries, config of next cloud
  43. # <volume>_oo_data: document server
  44. # <volume>_es_index: elastic search index files
  45. # <volume>_db: mariadb - nextcloud db
  46. # <volume>_clamav: antivirus defintions
  47. #
  48. #
  49. # Variables
  50. # 2025-12-7
  51. #
  52. # comment out the below line - original location
  53. #BACKUP_DIR="/media/yazoo/qnap_vol3/backups/nextcloud/" # Change this to your desired backup directory
  54. BACKUP_DIR="/media/yazoo/qnap_vol3/backups/misc/" # Change this to your desired backup directory
  55. #BACKUP_DIR="/media/yazoo/Crucial-1T/restore_test_duplicati/backup_nc7" # backup nextcloud core files here for transfer to ryzen/rx7900 afterwards
  56. #
  57. #
  58. #BACKUP_DIR="/media/yazoo/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory
  59. TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
  60. # List of volumes to backup
  61. VOLUMES=("wg-easy_etc_wireguard" "jellyfin-b_cache" "jellyfin-b_config" "jellyfin_cache" "jellyfin_config" "nextcloud_duplicati_config" "duplicati_config") # Replace with your volume names
  62. #VOLUMES=("nc8_redis" "nc8_oo_data" "nc8_es_index" "nc8_clamav") # Replace with your volume names
  63. # Ensure backup directory exists
  64. mkdir -p "$BACKUP_DIR"
  65. # Function to check if a volume is empty
  66. is_volume_empty() {
  67. local volume_name=$1
  68. # Use a temporary container to check if the volume is empty
  69. if docker run --rm -v "$volume_name:/volume" alpine sh -c '[ -z "$(ls -A /volume)" ]'; then
  70. return 0 # Volume is empty
  71. else
  72. return 1 # Volume is not empty
  73. fi
  74. }
  75. # Loop through each volume and perform the backup
  76. for volume in "${VOLUMES[@]}"; do
  77. # Check if the volume is empty
  78. if is_volume_empty "$volume"; then
  79. echo "Skipping empty volume: $volume"
  80. continue
  81. fi
  82. # Define the backup file name
  83. BACKUP_FILE="$BACKUP_DIR/${volume}_backup-${TIMESTAMP}.tar.bz2"
  84. # Create a temporary container to backup the volume
  85. echo "Backing up volume: $volume to $BACKUP_FILE"
  86. # docker run --rm \
  87. # -v "$BACKUP_DIR:/backup" \
  88. # -v "$volume:/volume" \
  89. # alpine tar czf "/backup/${volume}_backup_${sequence}_${TIMESTAMP}.tar.bz2" -C / volume
  90. docker run -v "$volume:/volume" -v "$BACKUP_DIR:/backup" --rm loomchild/volume-backup backup -v ${volume}_backup-${TIMESTAMP}
  91. # Check if the backup was successful
  92. if [ $? -eq 0 ]; then
  93. echo "Backup completed successfully: $BACKUP_FILE"
  94. else
  95. echo "Backup failed for volume: $volume!"
  96. exit 1
  97. fi
  98. done
  99. echo "All backups completed successfully!"