We’ve covered now how to setup your Raspberry Pi server, we’ve been over the amount of load that your new server can handle, now its time for the last step, redundancy. Having a working server is one thing, but making sure its backed up just in case something were to go wrong is pretty essential. In a perfect world, you wouldn’t need to worry about ever losing data but I know a couple of times my backups really saved me when I screwed up permissions or wiped the wrong database by accident, at the end of the day stuff happens. So planning for this by creating backups is a good thing to put in place. 

The End Goal

The end goal here is to create cron tasks that will automate this process for us. If you’re not super familiar with cron tasks, in general, that’s totally all right. You can sort of think of a cron task like an alarm clock. You might set your alarm to go off every day at 8 a.m. and when it goes off you’ll hopefully wake up. Cron tasks are the same deal, you tell a cron to run when you want it to, and it will do the tasks that you tell it to do. They can actually be pretty useful as we’ll come to see because it will take the burden off of us to run these backups. Before we can get into the cron tasks, we need to first create the scripts that cron will run.

Creating Our Scripts

We’ll need to create two scripts here that will run the commands to backup our servers files, as well as, our server’s database. Let’s start by making the script file with this command.

sudo nano www-backup.sh

You should now be in your nano editor, and you’ll want to copy this text into your file.


TIMESTAMP=`date +%b-%d-%y` 




Now, there might be a lot going on in the code above if you’re not super familiar with bash scripts, but basically, we’re just using the tar command to create a backup with a dynamically generated timestamp on the file. In order for this file to be executable though, you’ll need to modify the permission and add the executable attribute to this file with this command. 

sudo chmod u+x www-backup.sh

Next we’ll create our MySQL backup script

sudo nano mysql-backup.sh

And then put our code inside


TIMESTAMP=`date +%b-%d-%y` 




mysqldump --user=$USERNAME --password=$PASSWORD --all-databases | gzip > $DESTINATION

Again you need to modify the permissions on the script to add the executable attribute

sudo chmod u+x mysql-backup.sh

Setting up Cron

You’ll need to first make sure that your Raspberry Pi has the capability to run cron tasks, so start by installing the package with 

sudo apt-get install gnome-schedule

Next open the cron jobs file where we will create our tasks with

crontab -e

You’ll want to press enter to confirm use of default nano editor

You should now be in your nano editor looking at the configuration file of your cron tasks. We can now add two lines at the bottom and then we’re all set. So copy these lines and place them at the bottom.

30 0 * * 0 /path/to/script/www-backup.sh

30 1 * * 0 /path/to/script/mysql-backup.sh

To explain a little bit here, the first line the 30 is 30 minutes after 0 hours, or 12:30 a.m. then * * 0 will mean that we want this to run once a week every Sunday, or day 0. That’s all you need, use control x to save and close the file and you’re all set with making sure you’ll have consistent backups. 

Now you’ll want to most likely login with FTP every once in a while and copy over those backups to another redundant drive just in the off chance that your Raspberry Pi self-implodes, you should also clean up your Raspberry Pi every once in a while so you don’t have an entire month’s worth of backups on it. I hope you find this useful, and maybe you learned a thing or two about cron and the capabilities of the Raspberry Pi.

Categories: Hosting


Leave a Reply

Your email address will not be published. Required fields are marked *