Skip to content

Instantly share code, notes, and snippets.

@mammuth
Created October 7, 2016 12:43
Show Gist options
  • Save mammuth/36c933e53e3fb2126a357c7f9f65174a to your computer and use it in GitHub Desktop.
Save mammuth/36c933e53e3fb2126a357c7f9f65174a to your computer and use it in GitHub Desktop.
This script backups a Docker container instance, by creating a new docker image and exports this to a .tar
#!/bin/sh
log_file=${BACKUP_LOG_FILE}
backup_script_name="docker-teamspeak-snapshot"
date=$(/bin/date +"%Y-%m-%d_%H-%M-%S")
echo "Started $backup_script_name at $date" >> $log_file
echo "Create a new snapshot_teamspeak docker image.." >> $log_file
docker commit -p teamspeak snapshot_teamspeak >> $log_file
echo "Export the snapshot_teamspeak image to a tar.." >> $log_file
docker save -o /var/backup/docker-container/snapshot_teamspeak_$date.tar snapshot_teamspeak >> $log_file;
echo "Remove untagged / old images.." >> $log_file
docker rmi $(docker images | grep "^<none>" | awk '{print $3}') >> $log_file
# We only want to store backups 14 days, so let's remove old ones
echo "Look for old backups and remove them..." >> $log_file
for file in "$( /usr/bin/find /var/backup/docker-container -type f -mtime +14 )"
do
/bin/rm -f $file >> $log_file
done
echo -e "Finished $backup_script_name at $(date +"%Y-%m-%d_%H-%M-%S") \n" >> $log_file
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment