Created
December 25, 2024 12:56
-
-
Save pourmand1376/16927234fcc9b1da821a66fce7988d3d to your computer and use it in GitHub Desktop.
Daily Backup Script of Docker Volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, Use
tar xvf yourfile.tgz
to extract it into volumes folder.