docker backups
> Updating README > Adding bash script
This commit is contained in:
96
docker_backups/docker_backups.sh
Executable file
96
docker_backups/docker_backups.sh
Executable file
@@ -0,0 +1,96 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
#
|
||||
# Make sure to set username to your own.
|
||||
# You will need to change it in (3) places.
|
||||
#
|
||||
# [USERNAME]
|
||||
#
|
||||
#
|
||||
# Location of the log file
|
||||
LOGFILE="/home/[USERNAME]/scripts/backups/docker_backups_logs/docker_backups.log"
|
||||
|
||||
# Location of backup files. This can be a mapped NAS location if needed.
|
||||
BACKUPLOCATION="/home/[USERNAME]/server_backups/docker_zips/"
|
||||
|
||||
# Generate a timestamp
|
||||
timestamp=$(date +"%Y-%m-%d")
|
||||
|
||||
# Capture the start time
|
||||
start_time=$(date +%s)
|
||||
|
||||
# Log the start of the backup
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Starting backup" | tee -a "$LOGFILE"
|
||||
|
||||
|
||||
|
||||
# Step 0: Delete backups older than two weeks
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Deleting backups older than two weeks..." | tee -a "$LOGFILE"
|
||||
sudo find "$BACKUPLOCATION" -name "docker_backups_*.tar.gz" -mtime +14 -exec rm {} \; 2>&1 | tee -a "$LOGFILE"
|
||||
|
||||
# Capture the list of running Docker containers
|
||||
running_containers=$(sudo docker ps --format "{{.Names}}")
|
||||
|
||||
|
||||
|
||||
# Check if there are any running containers
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
|
||||
if [ -z "$running_containers" ]; then
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] No Docker containers are running." | tee -a "$LOGFILE"
|
||||
else
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Currently running Docker containers:" | tee -a "$LOGFILE"
|
||||
echo "$running_containers" | tee -a "$LOGFILE"
|
||||
|
||||
# Step 1: Stop all running Docker containers
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Stopping all running Docker containers..." | tee -a "$LOGFILE"
|
||||
sudo docker stop $running_containers 2>&1 | tee -a "$LOGFILE"
|
||||
fi
|
||||
|
||||
|
||||
|
||||
# Step 2: Create a tarball (zip) of your custom Docker folder with a timestamp
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Creating tarball backup of custom Docker directory..." | tee -a "$LOGFILE"
|
||||
|
||||
# Create backup in backup location defined above
|
||||
sudo tar czvf "${BACKUPLOCATION}docker_backups_${timestamp}.tar.gz" /home/[USERNAME]/docker 2>&1 | tee -a "$LOGFILE"
|
||||
|
||||
|
||||
|
||||
# Step 3: Restart all previously running Docker containers if any were running
|
||||
if [ -n "$running_containers" ]; then
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Restarting all previously running Docker containers..." | tee -a "$LOGFILE"
|
||||
sudo docker start $running_containers 2>&1 | tee -a "$LOGFILE"
|
||||
fi
|
||||
|
||||
# Capture the end time
|
||||
end_time=$(date +%s)
|
||||
|
||||
# Calculate the duration in minutes and remaining seconds
|
||||
duration=$((end_time - start_time))
|
||||
minutes=$((duration / 60))
|
||||
seconds=$((duration % 60))
|
||||
|
||||
# Log and display the duration
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo " " | tee -a "$LOGFILE"
|
||||
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Backup process completed in ${minutes} minutes, ${seconds} seconds." | tee -a "$LOGFILE"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user