#!/bin/bash # # Docker Volume Backup Script # # This script is designed to back up Docker volumes to a specified directory. # It checks if each volume is empty before attempting to back it up and creates # compressed .tar.bz2 backup files for non-empty volumes. # # Prerequisites # # Docker: Ensure Docker is installed and running on your system. # Bash: This script is written in Bash and should be run in a Bash-compatible shell. # Backup Directory: Ensure the backup directory specified in the script exists or is writable. # #Script Overview # #The script performs the following tasks: # # Defines Variables: # BACKUP_DIR: The directory where backup files will be stored. # TIMESTAMP: A timestamp used to uniquely identify backup files. # VOLUMES: An array of Docker volume names to back up. # # Checks for Empty Volumes: # 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. # # Backs Up Volumes: # For each non-empty volume, the script creates a compressed .tar.bz2 backup file in the specified backup directory. # The backup is performed using the loomchild/volume-backup Docker image. # # Verifies Backup Success: # After each backup, the script checks if the backup was successful. If any backup fails, the script exits with an error. # # Outputs Status: # The script provides status messages for each step, including skipped volumes, successful backups, and any errors encountered. # # WARNING: # verify assigned values for these variables before proceeding: # # BACKUP_DIR - verify the location is accessible. The mount point is named by the login user so it may look different # VOLUMES - are all the volumes to backup include in the list? # _nc6_files: stores the core admin account and binaries, config of next cloud # _oo_data: document server # _es_index: elastic search index files # _db: mariadb - nextcloud db # _clamav: antivirus defintions # # # Variables BACKUP_DIR="/media/yazoo/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory #BACKUP_DIR="/media/yazoo/wdcrypt/2025-docker-data-migration/temp-nc6-backup" # Change this to your desired backup directory TIMESTAMP=$(date +"%Y%m%d_%H%M%S") # List of volumes to backup VOLUMES=("nc6_files" "nc6_oo_data" "nc6_es_index" "nc6_db" "nc6_clamav") # Replace with your volume names #VOLUMES=("nc6_redis" "nc6_oo_data" "nc6_es_index" "nc6_clamav") # Replace with your volume names # Ensure backup directory exists mkdir -p "$BACKUP_DIR" # Function to check if a volume is empty is_volume_empty() { local volume_name=$1 # Use a temporary container to check if the volume is empty if docker run --rm -v "$volume_name:/volume" alpine sh -c '[ -z "$(ls -A /volume)" ]'; then return 0 # Volume is empty else return 1 # Volume is not empty fi } # Loop through each volume and perform the backup for volume in "${VOLUMES[@]}"; do # Check if the volume is empty if is_volume_empty "$volume"; then echo "Skipping empty volume: $volume" continue fi # Define the backup file name BACKUP_FILE="$BACKUP_DIR/${volume}_backup-${TIMESTAMP}.tar.bz2" # Create a temporary container to backup the volume echo "Backing up volume: $volume to $BACKUP_FILE" # docker run --rm \ # -v "$BACKUP_DIR:/backup" \ # -v "$volume:/volume" \ # alpine tar czf "/backup/${volume}_backup_${sequence}_${TIMESTAMP}.tar.bz2" -C / volume docker run -v "$volume:/volume" -v "$BACKUP_DIR:/backup" --rm loomchild/volume-backup backup -v ${volume}_backup-${TIMESTAMP} # Check if the backup was successful if [ $? -eq 0 ]; then echo "Backup completed successfully: $BACKUP_FILE" else echo "Backup failed for volume: $volume!" exit 1 fi done echo "All backups completed successfully!"