Tag Archives: backup

Having fun with backups

Update 2016-11-16: Not sure if this works anymore, because I have moved away from Openshift. So if you have found this through web search – please use caution.
You know how they say: “There are two kinds of people, those who don’t backup and those who now backup”.

I have experienced some catastrophic data loss in the past, so I am more from second kind. That’s why today I have decided, despite the fact that there are more interesting stuff, to do something useful. Yeah, set up backups!

This blog is running on Openshift, a Red Hat PaaS. I’ve chosen them because there is a free plan and Git deploys, among other things.

So the story goes like this.

Openshift has it’s own console tool – ‘rhc’, which controls every aspect of your cloud instance (or ‘gear’ as they call it). You can create, destroy, deploy, install software etc. And, yes, you can make ‘snapshots’ – basically a gzip, which includes your DB, your Git repository and other application data. This is just what I need. The only problem with snapshot command is that you have to save the file somewhere and it should be somewhere outside the ‘gear’ (like, your local machine). At first I was eager to put down simple cron script and run it on my laptop. Dirty, but it may work. But what if the laptop is not on the network, or I am on limited traffic (not that this happens very often, but still)? So there should be a way to teach my ‘gear’ do backups by itself.

Let’s see how ‘rhc snapshot’ command works. First of all ‘rhc’ is just a gem, so we can read it’s source code. Turns out ‘snapshot’ is some sort of command on the ‘gear’ which is called through ‘ssh’:
ssh #{app_uuid}@#{app_name}-#{namespace}.#{rhc_domain} 'snapshot' > #{filename}

Ok, i would like to see it’s guts too. Ssh-ing to server and…
bash: snapshot: command not found

But why? Frankly – I don’t know. But, luckily, I’ve found ‘gear’ command in alias list, which has it’s own ‘snapshot’ option. Bingo! Now all I need is just run ‘gear snapshot’ every day and copy it’s output somewhere. Let’s use Dropbox for that.

Apparently there is official Dropbox command line client, which requires python(?). So I went the other way and used Dropbox Uploader by Andrea Fabrizi, which is a bash script. Yeah, i don’t want to install python, there is only 1GB of disk space on the box.

My backup script is far more humble than Dropbox Uploader:

#!/usr/bin/env bash
# Prepare and save snapshot locally
SUFFIX=$(date +'-%A')              # save one backup for a day of week
gear snapshot > $FILENAME

# Upload to dropbox (obviously)
RUN_DIR=$(dirname $0)
UPLOADER="$RUN_DIR/dropbox_uploader.sh -f $RUN_DIR/dropbox_uploader_config"


# Cleanup

‘dropbox_uploader_config’ is generated by uploader when you run it for the first time. Just follow the instructions on screen.

What’s left is to tell cron to run daily. Putting executable script into ‘.openshift/cron/daily/’ folder would do the trick. There is also five minutes timeout on crons, so I used ‘nohup’.

Well, that came out longer than i expected. I spent maybe 4 hours on all that, including doing laundry and making some tea and writing this post. Still, that’s a crucial feature (too bad Openshift doesn’t do backups by default). Oh, forgot to mention that ‘snapshot’ will stop the app, so be aware.

That’s it for today.