I’ve been running a web server on my Raspberry Pi 2B for more than 3 years now and fortunately, no problem pertaining to data loss occurred. One does not know when something like that could happen though, therefore it is good to have some contingencies, in this case, having a backup plan.
When deciding what storage type and backup software to go for, I weighed several options. I wanted the backups be as low budget as possible, preferably free. Another thing to consider was the safety of the data, nobody wants a bad actor accessing their web application’s files, server configuration files and databases now, do they? I decided to go for Mega as it offers 15GB of storage for free, this being paired with the saved data being encrypted, it was the clear choice.
Starting with Mega and it’s command-line utilities
It should be pretty obvious that you’d need to register for a Mega account in order for this to work. After the registration, there are several ways to up your storage limit, though for a limited time (as far as I know).
With your account set-up and ready to go, it is time to get Mega’s own CLI — megacmd. It is available for Linux, Windows and MacOS, but in this article, I’m going to be focusing on the Linux side of things, more precisely Debian-based distros.
sudo dpkg -i megacmd-*
The binary’s filename to install ultimately depends on your distribution and it’s version.
After the installation is complete, you may try to login to mega through your terminal with the following command:
mega-login email password
If you’ve successfully logged in, you’re good to go to the scripting part.
Creating the backup script
As you most likely want to backup more than one folder at a time, it is best to put this all into one bash script. You could also write this in Python, Rust or any other scripting language, but for simplicity sake, I will build the script in Bash.
A quick overview of the script’s functionality:
- Login to Mega
- Check if backup exists for the current month (or week/day if you prefer more frequent backups)
- Backup directories where website files are saved
- Tar these directories into one archive
- Upload the files to Mega
- Upload previously created database dumps to Mega
- Remove temporary files and logout
There are lots of options how to build on top of the base script shown above. You may add more directories to backup, you may do daily or weekly backups. Maybe you want the script to be more verbose, you remove discarding mega-put and mega-logout messages to /dev/null. The possibilities are endless.
Needless to say, this script does it’s best when you run it as a cron job, after all, it is supposed to be an automated backup script. I run it monthly, so my cron looks like this:
0 12 1 * * /home/pi/scripts/backup_to_mega.sh
In my case, it runs on the first day of every month at the noon, simply enough. If you run it like this on default cron settings, you may get all the output of the script right into your email inbox every month though which can get pretty annoying pretty fast. I recommend decreasing the verbosity of the script by suppressing the echo commands or sending the cron job’s output to /dev/null altogether.
So that’s it, a simple, yet highly functional and dare I say powerful solution. If you’ve made it this far, thank you. And if you liked this article, make sure to drop a comment with some feedback, it is much appreciated.
Original script and more stuff like this can be found in my utils repo linked below and over at my GitHub.