Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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