Setting up an automatic backup for your Nearlyfreespeech site

After ditching my Squarespace photoblog and my photoblog and instead gathering everything at my hertze.com I started thinking about how to best backup the site contents. Hertze.com is running on Statamic, which means the contents is all text files. Before I usually wrote my content files on my laptop and synced them over to my web account at Nearlyfreespeech, but I’ve found myself using the Statamic admin panel more and more. Especially when posting photos. This means I don’t have a local backup of the contents on my laptop. Something had to be done.

Luckily Gina Trapani had already thought of this. I followed her advice and logged on to my server using SSH in Terminal.app and created a folder called backups on my server. In it I created the file backup.sh and a folder called files. Since I don’t use a database I simplified Trapanis script into this, which is the contents of backup.sh:

#!/bin/sh

THESITE="hertze.com"

THEDATE=date +%d%m%y%H%M

tar czf /home/public/backups/files/sitebackup_${THESITE}_${THEDATE}.tar -C / home/public/_content home/public/assets home/public/_themes home/public/_config

gzip /home/public/backups/files/sitebackup_${THESITE}_${THEDATE}.tar

find /home/public/backups/files/site* -mtime +3 -exec rm {} \;

You need make backup.sh executable, by typing chmod +x backup.sh.

You then go to Nearlyfreespeech.com and log in to your account. Under sites > Manage Scheduled Tasks you set a cron task to execute your backup script, in my case with the command /home/public/backups/backup.sh. After this, you’ll have three days worth of gziped backups of your _content folder, your asset folder, your _themes folder and your _config folder.

Now you’ll have to pull those backups from your server. Trapani has a clever solution where you sync your backups to another server. I don’t have another server, so I use a Hazel rule to run a shell script, which rsyncs the backups folder on my sever with a folder on my hard drive.

The Hazel rule is simple: If Current time is after 10:00 and Date Last Matched is not in the last 1 day, then Run shell script. This rule is attached to the folder on my laptop I want the backups pulled to. The shell script the rule executes looks like this:

#!/bin/sh

rsync -avz --delete -e ssh [email protected]:/home/public/backups/files /Users/Joakim/Backups

For this to work you need to setup password-less authentication by generating an SSH key on your computer and add that to your Nearlyfreespeech account under profile > Add SSH Key.

— August 17th, 2014 · Tagged code, backup, nearlyfreespeech, sync & statamic

comments powered by Disqus