Skip to content

Instantly share code, notes, and snippets.

@pourmand1376
Created December 25, 2024 12:56
Show Gist options
  • Save pourmand1376/16927234fcc9b1da821a66fce7988d3d to your computer and use it in GitHub Desktop.
Save pourmand1376/16927234fcc9b1da821a66fce7988d3d to your computer and use it in GitHub Desktop.
Daily Backup Script of Docker Volumes
#!/bin/bash
set -euo pipefail
# Directory containing Docker Compose file
DOCKER_DIR="/home/azureuser/services/mongodb"
# Backup storage directory
BACKUP_DIR="/home/azureuser/services/mongodb/backup"
# Number of backups to keep
KEEP_BACKUPS=5
# Change to the Docker Compose directory
cd "$DOCKER_DIR" || exit
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Stop Docker containers
docker compose down
# Create new backup
BACKUP_FILE="volumes-$(date +"%Y%m%d_%H%M%S").tgz"
sudo tar -cvf "$BACKUP_DIR/$BACKUP_FILE" volumes
# Start Docker containers
docker compose up -d
# Remove old backups
cd "$BACKUP_DIR" || exit
ls -t volumes-*.tgz | tail -n +$((KEEP_BACKUPS + 1)) | xargs -r rm
echo "Backup completed and old backups cleaned up."
@pourmand1376
Copy link
Author

Also, Use tar xvf yourfile.tgz to extract it into volumes folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment