From 2923de827e0f99b780d434ce5ee1dfd9c149b393 Mon Sep 17 00:00:00 2001 From: Eugene Amos Date: Tue, 5 Dec 2023 12:56:53 -0800 Subject: [PATCH] docker backups > Updating README > Adding bash script --- docker_backups/README.md | 56 +++++++++++++++++++ docker_backups/docker_backups.sh | 96 ++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100755 docker_backups/docker_backups.sh diff --git a/docker_backups/README.md b/docker_backups/README.md index e69de29..bb693b3 100644 --- a/docker_backups/README.md +++ b/docker_backups/README.md @@ -0,0 +1,56 @@ +

Docker Backups

+ +

+ A bash script that will stop all running containers. Create a .tar.gz files of those stopped containers then upload them to a specified save location. After the containers are successfully saved, it will proceed to restart them. +
+ The script will log each step of the process including the time it takes for the script to execute. +
+

+ +## Prerequisites + +You will need to change `[USERNAME]` to the one you use on your server. You will find this in **(3)** parts of the script. + +## Folder Structure + +If you have not done so already, create a new folder called `scripts` in your **~home** folder. This is where all your scripts will go. + +Inside your `scripts` folder create a new folder called `backups` + +When done your folders structure should look like this: `/home/[username]/scripts/backups` + +## Script + +Get the source code from this repo and save it as `docker_backups.sh` in the **backups** folder created in the previous step. + +Next we need to change the permissions of this file so that the server can read it. + +1. Right click on `docker_backups.sh` then Properties. + +2. Click on `Permissions` tab at top. + +3. Make sure Owner & Group reflect your `username` with `Read and Write` access to all. + +4. Check the `Execute` box to allow executing the file as a program. + +5. Close out the window. + +## Run + +Now we need to test and run the script. + +* Make sure your terminal is open to the location of the `docker_backups.sh` file. Should be `~/scripts/backups` + +* Run the script: + + ```bash + bash docker_backups.sh + ``` + +The script should be running and should be printing to the terminal what step its on. This process will take a few minutes or so depending on how many running containers you have. + +## Automation + +Now the script will need to be automated so that you will have current backups incase you need them. This can be automated using *Linux Mint's* built in
crontab
system to create a `cron` job. **Crontab** is managed with the terminal and ***does not*** have a GUI. + +I have not found a good GUI option for crontab as of yet however, I have found and use an application called
Cronicle.
Cronicle is task scheduler that functions like **crontab** but only with a GUI. diff --git a/docker_backups/docker_backups.sh b/docker_backups/docker_backups.sh new file mode 100755 index 0000000..ab9b685 --- /dev/null +++ b/docker_backups/docker_backups.sh @@ -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" + + + + + + + + + + + +