Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just a little contribution: shell script to execute docker-autocompose and backup yaml #50

Open
phit42 opened this issue Sep 26, 2022 · 2 comments

Comments

@phit42
Copy link

phit42 commented Sep 26, 2022

Just my 2cents on a way to execute docker-autocompose:

#!/bin/bash

# https://github.com/Red5d/docker-autocompose

BACKUP_DIR="/volume1/docker/backupcontainersetup"
EXPORT_DATE="$(date +%Y-%m-%d_%H-%M)"
BACKUP_FILE="${BACKUP_DIR}/docker-autocompose_${EXPORT_DATE}.yaml"

# IncludedContainer=(diun sabnzbd watchtower)
IncludedContainer=($(docker ps -a --format "{{ .Names }}"))

echo "exporting container yaml settings to ${BACKUP_FILE}"
echo ${IncludedContainer[@]}

echo "# docker-compose file generated by Red5d/docker-autocompose" > "${BACKUP_FILE}"
echo "# included containers: ${IncludedContainer[@]}" >> "${BACKUP_FILE}"

# docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose:latest ${IncludedContainer[@]}  >> "${BACKUP_FILE}"

# export all containers
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/red5d/docker-autocompose:latest $(docker ps -aq)  >> "${BACKUP_FILE}"
@newadventure079
Copy link

Thanks for this. I'm using it and it works great

@reikolydia
Copy link

i found this project useful, as my watchtower containers sometimes fail to create containers after the update, so i have to backup the compose files to redeploy afterwards..

if you wanna use the python file itself, this is what i use for my daily cron job
the files are backed up into the same folder where the python file is, you can change this with:

  • autocomposedir : where the autocompose python file is
  • yamlbackupdir : what name you wish to give the folder

and the output captures all containers but each container is grouped individually into (in order):

  1. date
  2. container name
  3. container ID (in case you re deploy a container and it changes ID but retains the name)
  4. time

and at the end, it generates a timelog of when the cron job completes

#!/bin/bash

autocomposedir="/home/backuper/docker-backup"
yamlbackupdir="yaml-backups"

IFS=$'\n'
containers=$(docker ps -a --format "{{.ID}} {{.Names}}" | sort -k 2)
dockerupcounter=1
MAX=$(docker info --format "{{json .Containers}}")
SECONDS=0

echo ================================
echo "Started container config backup on: $(date +%d-%b-%Y\ %H:%M:%S\ %p\ %Z)"

for container in $containers
    do
    IFS=$' '
    containerID=($container)
    echo "$dockerupcounter/$MAX: $container"
    datefolder=$(date +%d-%m-%Y)
    mkdir -p "$autocomposedir/$yamlbackupdir/$datefolder/${containerID[1]}/${containerID[0]}"

    sudo python3 $autocomposedir/autocompose.py ${containerID[0]} > "$autocomposedir/$yamlbackupdir/$datefolder/${containerID[1]}/${containerID[0]}/$(date +%H-%M-%S\ %p\ %Z).yaml"

    dockerupcounter=$(expr $dockerupcounter + 1)
done

echo "Backup completed : $(date +%d-%b-%Y\ %H:%M:%S\ %p\ %Z)" >> $autocomposedir/$yamlbackupdir/backups.log
echo "Backup time took : $SECONDS seconds"
IFS=$' \t\n'

if you want a version that you manually start, and has a progress bar with rclone backup: check out my other fuller fledged file at: backup-container-configs.sh , just place the file where the python autocompose file is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants