Server Backups to MEGA

In an earlier post I looked at using MEGA Cloud Storage. As mentioned in that article, MEGA provides 50GB free to new accounts, combined with their native support for Linux and this seemed like a possible good solution for backing up my WordPress servers. In this article I’m going to dig into how to setup server backups to MEGA. In later articles we’ll combine this with other tools to make a very robust server management tool…all using free tools.

Requirements for Server Backups to MEGA

MEGA itself only provides a GUI client for Linux. It works great on my Linux Mint Laptop, but it’s not so useful on an Ubuntu Virtual Server. After doing some checking I came across a really good client fore MEGA called Megatools. They describe themselves as the Command Line Client for MEGA. The project is opensource and you can access the source code on Github. If you prefer you can download the source and compile on your server. The Megatools documentation is also available online here. I’m on Ubuntu and I’m just going to use apt-get to install it.

The next thing you’ll want to do is create a config file that contains your MEGA login credentials in your home directory. Create a new file and save it to ~/.megarc with the following contents:

[Login]
Username = your@email
Password = yourpassword

After you’ve saved that file, run the megadf command. Megadf will show your MEGA account storage usage and free space. If you are able to see results, then you are successfully connecting to your MEGA account.

Scripting Server Backups to MEGA

So my goal is to create a simple backup script that will take all of my MainWP backups and upload them to my MEGA account for safe storage. To do this I’m going to use 2 command from Megatools: megals and megaput.

Megals: This command lists directories and file details within MEGA

Megaput: This command uploads files to MEGA

I don’t want to upload a backup file if it already exists. So I’m going to check each backup file and see if it already is in my MEGA account. If its not found, I will upload it. If it is found, I will skip it. Because Megatools caches the contents of your MEGA account, the –reload parameter will force it to always use a fresh copy of your account. I’ll use the megals command to determine if it exist like so:

backupfile=full-diyidiot.com-July-28-2016-4-11-pm.tar.gz
if megals --reload | grep $backupfile
then
echo "Backup was already found in MEGA"
else
echo "Backup was not found in MEGA"
fi

To copy a file to MEGA, I’ll use the megaput command like this:

backupfile=archive-demo-7-11.tgz
localbackupdir=/tmp
BACKUP_FOLDER=/Root/Backups/Test
if megals --reload | grep $backupfile
then
echo "Backup was already found in MEGA"
else
echo "Backup was not found in MEGA"
megaput --path ${BACKUP_FOLDER}/${backupfile} ${localbackupdir}/${backupfile}
echo "Backed up ${backupfile} to MEGA"
fi

One thing that I found to be slightly confusing is that megaput takes parameters in what I would consider a reverse order. The syntax is: megaput –path MEGA_DESTINATON_PATH SOURCE_LOCAL_PATH

Final MEGA Backup Script

So now I want to setup my script that will handle server backups to MEGA. To bring this all together, I’m going to list the contents of my MainWP backups, uploading the new backups to MEGA. I’ll schedule this to run via a ChronTab entry.