How to Auto (timestamped) Archive Directories on Raspberry Pi

June 18, 2013 11:54 pm Leave your thoughts

Pi Dir BackupOn my last post I talked about backing up your latest files automatically on a Raspberry Pi, while it’s well and good, it doesn’t keep historical data at all. So what happen if you corrupted current data and the corrupted data happened to get synced? No too good isn’t it, we’ll just end up with broken sets of data.

So today we are going to write a simple archiving script for our Raspberry Pi that can be used to archive multiple directories to multiple files unique to the backup date.

In this example, we will archive two directories to two different files:

The Backup Script

Let’s first create our backup script. We call this script:

So using your favourite editor, create under your home directory (eg. /home/pi) and use these codes:

Basically in the code we list the pairs of backup file destination and the directory sources as a list in the script. We then iterate through the pairs and execute the tar command, creating an archive file of the directory source. The above script is quite flexible, should you ever want to backup other directories, simply add more lines to the dirpairs list.

The Schedule

Now that we have our backup script, we are almost done. The last thing that we want to do is schedule the script to run every one day of the week using cron. In this example we will schedule this to run every 2nd day of the week, which is Tuesday at 23:30.

As usual it’s pretty simple to do this, we simply use crontab:

Then add another line:

After that save the file. Of course, if you save the backup script anywhere else you’ll have to adjust the command above.


That’s it, we’re more covered now. Every Tuesday your Raspberry Pi will archive both Open Atrium and the Dos games. If anything bad should happen to your SDCard at least you’ll have an archive of the last Tuesday’s sets.


Tags: ,

Categorised in: ,