Skip to content

Instantly share code, notes, and snippets.

@bruvv
Created December 12, 2019 12:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bruvv/e4a6803b2d18960517fa94c8cc0e44cc to your computer and use it in GitHub Desktop.
Save bruvv/e4a6803b2d18960517fa94c8cc0e44cc to your computer and use it in GitHub Desktop.
#!/bin/bash
location=<your backup location>
date=`date "+%Y%m%d"`
for name in $(docker ps --format '{{.Names}}'); do
#Backup location per container
backup_location=$location"/"$name
#Make the directory when it doesn't exist
mkdir -p $backup_location
#Find previous backup by its LATEST label
previous_backup=`find $backup_location -name *LATEST*`
if [ -z $previous_backup ]
then
#No backup made before, so just make one
echo "No previous backup available. Lets make one and move along."
docker inspect $name > $backup_location"/"$name".syno.LATEST.json"
else
#Determine if something changed in the configuration of the container
#First make a new temp backup
temp_backup=$backup_location"/"$name".syno.TEMP.json"
docker inspect $name > $temp_backup
#Load files
prev_file=$(< $previous_backup)
temp_file=$(< $temp_backup)
if cmp -s "$prev_file" "$temp_file" ;
then
#No change, so we can keep the previous as LATEST
rm $temp_backup
else
#Get last modification date of previous backup
date_previous_backup=$(sudo stat --printf=%y $previous_backup | cut -c 1-10 -n)
#Rename previous LATEST to last modification date
new_previous_backup=$backup_location"/"$name".syno.v"$date_previous_backup".json"
mv ${previous_backup} ${new_previous_backup}
#Rename TEMP to LATEST
latest_backup=$backup_location"/"$name".syno.LATEST.json"
mv ${temp_backup} ${latest_backup}
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment