Red5d / docker-autocompose

Generate a docker-compose yaml definition from a running container
1.76k stars 197 forks source link

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

Open phit42 opened 1 year ago

phit42 commented 1 year ago

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 commented 1 year ago

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

reikolydia commented 9 months ago

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:

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