How to Auto (timestamped) Archive Directories on Raspberry Pi
June 18, 2013 11:54 pm Leave your thoughtsOn 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:
- Our open atrium installation: /var/www/openatrium to be backed up to /media/dduckusb/backup/atriumbak-[backup_date].tar.gz
- Our dos games (wouldn’t want to lose our saved games): /home/pi/dos to be backed up to /media/dduckusb/backup/dosgamesbak-[backup_date].tar.gz
The Backup Script
Let’s first create our backup script. We call this script: dirbackup.sh
So using your favourite editor, create dirbackup.sh under your home directory (eg. /home/pi) and use these codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/bin/bash #pair of filename to create and the directory source backupdate=$(date +%Y%m%d) dirpairs=( "/media/dduckusb/backup/atriumdirbak-$backupdate.tar.gz /var/www/openatrium" "/media/dduckusb/backup/dosgamesbak-$backupdate.tar.gz /home/pi/dos" ) for dirpair in "${dirpairs[@]}" do tar -czf ${dirpair[@]} done |
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:
1 |
sudo crontab -e |
Then add another line:
1 |
30 23 * * 2 /home/pi/dirbackup.sh |
After that save the file. Of course, if you save the backup script anywhere else you’ll have to adjust the command above.
Done
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: backup, Raspberry Pi
Categorised in: Linux Tricks, Raspberry Pi