If you invested time (and you probably did) crafting fragments it would be terrible to loose them. Out of the box, you can backup the database by clicking on the upper right icon. The only problem is, like everything you have to remember, is not to forgot to do it regularly. Let’s try to make the raspberry do the work for us.
I’ll spread the article in three parts :
- The clean backup : Simple and clean.
- Put the clean method in a script and schedule it.
- The hacky way : For the curious ones.
A Clean backup
Every fragments or files is stored in /var/lib/jetty/piggydb. So simply, we’ll back them up. To be sure that we don’t corrupt the database, we are stopping jetty before compressing files. Nice thing about tar command here is that it will store the permissions set on each folders and files and restore them when you’ll unpack them.
sudo /etc/init.d/jetty stop && cd /usr/share/jetty && tar -zcvf /tmp/piggydb.tar.gz piggydb/ && sudo /etc/init.d/jetty start
Et voilà ! We now have a backup in /tmp directory.
Simply the line above does:
- Stopping jetty daemon
- Changing current directory to /usr/share/jetty directory
- Creating /tmp/piggydb.tar.gz archive of piggydb directory
- Starting again jetty daemon
To restore our backup, we are changing only one (hint: it’s an x) argument.
sudo /etc/init.d/jetty stop && cd /usr/share/jetty/ && sudo tar -xzvf /tmp/piggydb.tar.gz && sudo /etc/init.d/jetty start
If you change the war filename and restore the backup, it will try to create a new directory the first time jetty’s run. Make sure that you keep the same name.
Script the backup and schedule it
Now we know how to do it, let’s wrap things together.
Step 1 : Create the backup script
From a terminal, go to your home folder.
You should be in /home/pi.
Open nano with backup-piggydb.sh file. (saving it will create it later)
Inside nano type (or copy/paste) the following:
echo "$(date -R) : Starting Piggy DB Backup"
sudo /etc/init.d/jetty stop
cd /usr/share/jetty && tar -zcvf /tmp/piggydb.tar.gz piggydb/
sudo /etc/init.d/jetty start
echo “$(date -R) : Piggy DB Backup complete”
Press now ctrl + X to exit, press Y to save the changes and enter to confirm file name.
Step 2: Schedule it with Crontab
Crontab will allow us to plan when the backup should be done. So let’s edit our current crontab.
This will launch nano with one line. We will type (copy/paste) below this line the following:
0 7 * * * sh /home/pi/backup-piggydb.sh > /home/pi/backup-piggydb.log
Press ctrl + X to exit, press Y to save the changes and enter to confirm file name.
Little explanation, if you don’t know what you just typed, we’ve scheduled our backup script to :
- Run at 0 minute, 7 hours, every day, every months and every day of a week (The 3 stars). This means at 07h00 or 7AM every day.
- Run the following command : sh /home/pi/backup-piggydb.sh > /home/pi/backup-piggydb.log (I’m redirecting script output to file in case you’ve to know if something went wrong.)
Step 3 (optional) : Email the results, copy the backup off the raspberry SD,…
We’re bit beyond this tutorial, but it could be interesting to send by email the log file, so you know that a backup happened (or not !). This can be added at the end of the crontab line.
;cat /home/pi/backup-piggydb.log | sendemail -f “email@example.com” -t “youremail@address” -u “Backup complete” -s “smtp.yourisp.com”
Moving the backup file outside the SD card could be interesting using one of those command:
- Copy backup to a USB Drive attached to the PI : cp /tmp/piggydb.tar.gz /mount/USBKEYPluggedOnYourPI
- Uploading backup on a FTP server : wput /tmp/piggydb.tar.gz ftp://someftpserver/ --reupload
- Coping file to windows share : smbclient “//yourNAS/backups/” -N -c “put /tmp/piggydb.tar.gz piggydb.tar.gz”
The hacky way
I’m adding an extra paragraph for those who want more than just stopping/starting jetty to backup files.
Out of the box, you can backup your database/files. By clicking on a button on the backup page, you’ll be able to download a .pig file (it’s a zip file) with everything in it. And you’ll be able to restore it on another Piggy without caring how it is installed.
Other benefits from this button is that you can backup more frequently your data because if a script does it, it won’t have to stop jetty to do it.
Assuming this button only appear for the user owner, I’ve looked for ways to login on Piggy using curl, then trying to recover the generated archived. Simple as that.
cd /tmp && curl —data “form_name=loginForm&original=&userName=owner&password=YOUROWNERPASSWORD&ok= OK “ —dump-header headers http://127.0.0.1:8080/piggydb/login.htm
cd /tmp && curl -o PiggyDB-Backup.pig -b headers http://127.0.0.1:8080/piggydb/command/export-database.htm && rm headers
Doing this is the closest to what you could do by yourself, but it’s not future proof. If PiggyDB developer change something in the export pages code, these lines will be broken.