Skip to content

Instantly share code, notes, and snippets.

@lucatk
Last active August 6, 2020 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucatk/21e222075e6a611567ddb328c7c893a4 to your computer and use it in GitHub Desktop.
Save lucatk/21e222075e6a611567ddb328c7c893a4 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x #echo on
S3_BUCKET="ENTER_YOUR_BUCKET_HERE" # configure s3cmd beforehand
TEMP_DIR=$(mktemp -d)
CONTAINER_ID=$(docker ps -aqf "name=root_rancher_1")
IMAGE=$(docker inspect --format='{{.Config.Image}}' $CONTAINER_ID)
RANCHER_VERSION=$(echo $(docker inspect --format '{{ .Config.Env }}' $CONTAINER_ID) | tr ' ' '\n' | grep CATTLE_SERVER_VERSION | sed 's/^.*=//')
TIMESTAMP=$(date +"%Y-%m-%d-%H-%M")
BACKUP_CONTAINER=rancher-data-$TIMESTAMP
BACKUP_FILE=rancher-data-backup-$RANCHER_VERSION-$TIMESTAMP.tar.gz
cd $TEMP_DIR
docker stop $CONTAINER_ID
docker create --volumes-from $CONTAINER_ID --name $BACKUP_CONTAINER $IMAGE
docker run --volumes-from $BACKUP_CONTAINER -v $PWD:/backup:z busybox tar pzcf /backup/$BACKUP_FILE /var/lib/rancher
s3cmd put --guess-mime-type $BACKUP_FILE s3://$S3_BUCKET/$BACKUP_FILE
rm -rf $TEMP_DIR
docker start $CONTAINER_ID

This is just a wee little script for having automated backups of a single-node Rancher instance on docker. The script stops the container, backs its data up and simply uploads the backup file to S3 using s3cmd.

Made for and tested on Ubuntu 18.04.

You can then for example setup a simple cron job for it like this: (every day at 5a.m.)

0 5 * * * /usr/bin/bash /root/backup.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment