Backup files to Mega.nz from the command-line on a Linux Server

Cloud backups are the in-thing, and enterprises with self-hosted datacenters are moving quickly to take advantage of a live, redundant failover infrastructure running in the cloud that can be switched over to on a moment’s notice. While instant failover systems are crucial for critical infrastructure, they almost always come attached with a hefty price tag that is outside the reach of most small and medium businesses.

Most systems that can accommodate a little downtime on failure however are sufficiently serviced by a good old dump and archive backup policy at relatively low costs. They can also take advantage of the vast amounts of cloud storage being offered for next to nothing to store (encrypted) backup archives in the cloud and further reduce costs without increasing risk.

After a cursory review of current cloud storage providers, preferring those with a low price tag (ideally free), sufficient storage (20GB or more) and remote accessibility from the shell on a Linux system, only one provider rose above the clouds – Mega. Mega offers up to 50GB of free storage, and additional storage up to 8TB can be purchased at comparatively low prices. Additionally, Mega takes a unique approach to encrypting and storing files that simultaneously protect their customers’ files from being snooped on and Mega from lawsuits.

But what truly sets mega apart is megatools – their command line client application. megatools is a set of open source scripts that allow access to Mega from the command line on Linux based systems. This post gives a quick outline of installing megatools on an Ubuntu server and performing basic file operations. This approach, while ideally suited for remote servers, is in stark contrast to other online storage solutions like those Google or Dropbox, which rely on sync clients and are geared more towards end user systems.

Installing megatools

Megatools is available in the package repos, and can be installed with apt.

apt install megatools

Once installed, you will need to create a configuration file with the username and password of your Mega account. The config file looks like this, save it to your location of choice on the server with the name mega.cnf:

[Login]
Username = youremail@mega.com
Password = Your$password

Invoking the tools

To check if things are up and running, invoke the megadf command, which shows you available and used storage on your Mega account. You will have to pass it a path to the config file you saved earlier.

megadf --config=/path/to/mega.cnf

This should output something like:

Total: 53687091200
Used: 3963240858
Free: 49723850342

A list of all available scripts in the megatools package can be found from the megatools manpages, and are reproduced here:

megareg(1)
Register and verify a new mega account

megadf(1)
Show your cloud storage space usage/quota

megals(1)
List all remote files

megamkdir(1)
Create remote directory

megarm(1)
Remove remote file or directory

megaput(1)
Upload individual files

megaget(1)
Download individual files

megadl(1)
Download file from a “public” Mega link (doesn’t require login)

megacopy(1)
Upload or download a directory tree

megafs(1)
Mount remote filesystem locally.

It would be ideal to mount mega as a drive on the local system, but this tool has unfortunately not been fully implemented yet (according to the documentation). Using a combination of the other tools however, similar behaviour can be achieved.

For example, to copy a file named backup.zip.gpg located in /tmp/ use:

megaput --config=/path/to/mega.cnf --path=/Root/backups --disable-previews "/tmp/backup.zip.gpg"

This copies the file to Mega with a nice progress indicator to boot.

The other commands can be invoked similar manner, with the only required parameter being the path to the config file. To find a list of available parameters for a tool, try toolname -h.

For example,

megarm -h

Usage:
megarm [OPTION…] – remove files from mega.nz

Help Options:
-h, –help Show help options

Application Options:
-u, –username=USERNAME Account username (email)
-p, –password=PASSWORD Account password
–config=PATH Load configuration from a file
–ignore-config-file Disable loading .megarc
–no-ask-password Never ask interactively for a password
–disable-previews Never generate previews when uploading file
–reload Reload filesystem cache
–debug=OPTS Enable debugging output
–version Show version information

By integrating these tools into existing backup scripts, a comprehensive, resilient and cost effective backup solution to the cloud can be implemented easily.

Leave a Reply

Your email address will not be published. Required fields are marked *

18 + seven =