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.

Megatools-apt-getTo install Megatools, use the following command:

add-apt-repository ppa:megous/ppa
apt-get update && apt-get install -y megatools glib-networking fuse curl

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:

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:

if  megals --reload | grep $backupfile
    echo "Backup was already found in MEGA"
    echo "Backup was not found in MEGA"

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


if  megals --reload | grep $backupfile
    echo "Backup was already found in MEGA"
    echo "Backup was not found in MEGA"
    megaput --path ${BACKUP_FOLDER}/${backupfile} ${localbackupdir}/${backupfile}
    echo "Backed up ${backupfile} to MEGA"

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.

### Variables ###


#### Functions ####

backup ()
{ # Backup to Mega

  echo -e "Backing up $1"

  filename=$(basename "$1")

  if  megals --reload | grep $filename >/dev/null 2>&1
    		echo "Backup was already found in MEGA"
    		echo "Backup was not found in MEGA"
    		megaput --path ${BACKUP_TARGET}/${filename} ${1}
    		echo "Backed up ${filename} to MEGA"
  if [ $? -ne 0 ]; then
 		echo -e "ERROR: Couldn't upload $filename to Mega" "failure"
 	echo " "

echo -e "Starting MEGA Cloud Backup\n"

for f in $(find ${BACKUP_FOLDER} -name '*.gz'); do backup $f; done

echo -e "Ending MEGA Cloud Backup"
exit 0

Scheduling this to run at 1AM it will go through all my backup files and upload them to MEGA. In my next article I’ll talk more about how MainWP is setup, and also look at some logging options.

Update - 2016.08.09I posted an updated version of this script that logs its updates to Slack, read the Slack article to find the updated backup script.
I’m an avid Technologist, while I do work on architecting solutions that span from the desktop to the data center, my passion is Web Infrastructure.
We will be happy to hear your thoughts

      Leave a reply