Last active
March 17, 2024 14:01
-
-
Save Guiorgy/699361aea50d1126c636f6e880123af8 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
# NOTE: Deprecated in favor of https://github.com/Guiorgy/docker-vackup | |
# This is a wrapper for vackup: https://github.com/BretFisher/docker-vackup | |
# Installation: | |
# sudo curl -sSL https://raw.githubusercontent.com/BretFisher/docker-vackup/main/vackup -o /usr/local/bin/vackup && sudo chmod +x /usr/local/bin/vackup | |
# Based on PR #6: https://github.com/BretFisher/docker-vackup/pull/6 | |
# This script stops all running containers, backs up the volumes of all | |
# containers (including those that were not running), and restarts the | |
# containers that were stopped | |
vackup="/usr/local/bin/vackup" | |
date="/usr/bin/date" | |
docker="/usr/bin/docker" | |
mkdir="/usr/bin/mkdir" | |
ls="/usr/bin/ls" | |
grep="/usr/bin/grep" | |
tail="/usr/bin/tail" | |
xargs="/usr/bin/xargs" | |
backup_root_directory="$1" | |
if [ -z "$backup_root_directory" ]; then | |
echo 1>&2 'Must provide path to the backup destination' | |
exit 1 | |
fi | |
datetime=$($date +"%F_%H-%M-%S") | |
backup_directory="${backup_root_directory}/${datetime}" | |
running_containers=$($docker ps -q) | |
all_containers=$($docker ps -a -q) | |
all_volumes=$($docker volume ls -q) | |
# Get a list of containers that mount at least one volume | |
containers_with_volumes='' | |
for volume in $all_volumes; do | |
containers=$(docker ps -q --filter "volume=$volume" | tr '\n' ' ' | awk '{$1=$1;print}') | |
containers_with_volumes="$containers_with_volumes $containers" | |
done | |
containers_with_volumes=$(echo "$containers_with_volumes" | tr ' ' '\n' | sort -u | tr '\n' ' ') | |
# Stop running containers with volumes | |
if [ -n "$running_containers_with_volumes" ]; then | |
$docker stop $running_containers_with_volumes | |
fi | |
# Prepare the backup directory | |
$mkdir "${backup_directory}" | |
cd "${backup_directory}" # vackup dumps backups into the working directory and doesn't support full paths | |
# Backup all volumes | |
for volume in $all_volumes; do $vackup export $volume ${volume}.tar.gz ; done | |
# Backup containers as docker-compose.yaml | |
$docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose $all_containers > docker-compose.yaml | |
# Restart previously stopped containers | |
if [ -n "$running_containers_with_volumes" ]; then | |
$docker restart $running_containers_with_volumes | |
fi | |
# If the number of backups exceeds this number, remove the oldest backups | |
number_of_backups_to_keep=100 | |
# Remove old backups | |
cd "${backup_root_directory}" | |
$ls -tp | $grep '/$' | $tail -n +$((number_of_backups_to_keep + 1)) | $xargs -I {} rm -rf -- {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment