"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Monday, 2 October 2017

Backing-up the Raspberry Pi 3

Recently I had to repeat installation of my Raspberry Pi 3 server. Probably because of some SD card corruption problem I started experiencing unexpected loss of active services, first GitLab then MiniDLNA, a and failures while trying reinstalling or reconfiguring them. I so downloaded latest Raspbian image and went with a complete installation. Not a big deal, since I’ve been following my own instructions on this blog but still a time consuming process.
Once the Raspberry server was operative again I started looking for a simple backup solution in the case something broke again by itself or, not unlikely, I broke something by myself.
I quickly found in the Internet this discussion page where, among other solutions, it was proposed a handy shell script to completely backup Raspberry SD Card. Following discussion links I landed on this GitHub page where the same script is available in its latest version.
Installing the script is just matter of downloading and unzipping it or, as alternative, cloning it with git command. I did choose the latter since I’m going to need git in future.
sudo apt-get install gitmkdir scriptcd script/git clone https://github.com/aweijnitz/pi_backup.git
configuring the script
The PI backup script is a fine example of shell programming, it needs only a couple of arrangements to fit the system where is installed.
First functions stopServices() and startServices must be edited by uncommenting commands to stop and start services running on the Raspberry. I added command to stop and start MiniDLNA service:
...sudo service minidlna stop...sudo service minidlna start...
Then I edited the backup path. In the same scrip section it’s possible to set the number of old backups to keep and if backed-up images must be compressed.
...# Setting up directoriesSUBDIR=backupMOUNTPOINT=/media/usbdiskDIR=$MOUNTPOINT/$SUBDIRRETENTIONPERIOD=1 # days to keep old backupsPOSTPROCESS=0 # 1 to use a postProcessSucess function after successfull backupGZIP=0 # whether to gzip the backup or not...

Scheduling Pi backup
The backup script can, of course, be executed manually
But the real strength in having a backup script is not to have to remember to call it. So, last but not least, I configured the cron service in order to automatically execute the script:
crontab -e
Since I mostly work on Raspberry during week-and I configured a weekly backup on Monday morning
...0 5 * * 1 /home/pi/script/pi_backup/backup.sh...

Performances can vary widely depending on SD card size and destination drive speed, it takes about half an hour to complete on my system with the compression option disabled. On a home machine, mostly used as media server, I can afford half a hour of downtime every week.