| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #!/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.
- #
- # Variables
- BACKUP_DIR="/media/orbitzs/nextcloud_files/backup/nc2/docker_volumes" # Change this to your desired backup directory
- TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
- # List of volumes to backup
- VOLUMES=("nc3_files" "nc3_oo_data" "nc3_es_index" "nc3_db" "nc3_clamav") # Replace with your volume names
- #VOLUMES=("nc3_redis" "nc3_oo_data" "nc3_es_index" "nc3_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!"
|