MEGATOOLS – Synchronizing your backups to MEGA

Posted on Posted in linux, VPS

Hi and wellcome to another Sysadmin & DBA tips, in this post We’ll use the MEGATOOL utility to synchronize your server backups to the mega cloud!!

Megatools

Like the official webpage says:

Megatools is a collection of programs for accessing Mega service from a command line of your desktop or server.
Megatools allow you to copy individual files as well as entire directory trees to and from the cloud. You can also perform streaming downloads for example to preview videos and audio files, without needing to download the entire file.
You can register account using a ‘megareg’ tool, with the benefit of having true control of your encryption keys.
Megatools are robust and optimized for fast operation – as fast as Mega servers allow. Memory requirements and CPU utilization are kept at minimum.

The options that this tool offer:

Tools
=====

  megareg      Register and verify a new mega account
  megadf       Show your cloud storage space usage/quota
  megals       List all remote files
  megamkdir    Create remote directory
  megarm       Remove remote file or directory
  megamv       Move and rename remote files
  megaput      Upload individual files
  megaget      Download individual files
  megadl       Download file from a "public" Mega link
               (doesn't require login)
  megastream   Streaming download of a file
               (can be used to preview videos or music)
  megasync     Upload or download a directory tree
  megafs       Mount remote filesystem locally.

 Installation:

Differents installations are possible, in my case I’ll explain how realize a installation in a Ubuntu Server. But if you prefer, you can install from sources, obtain the latest source code from git, or extrac directly the binary files from:

PPA installation

  • Add Megattols PPA repositories to your /etc/apt/sources.list
deb http://ppa.launchpad.net/megous/ppa/ubuntu quantal main
deb-src http://ppa.launchpad.net/megous/ppa/ubuntu quantal main
  • Import Megatols PPA public Key
add-apt-repository ppa:megous/ppa
  • Update
aptitude update
  • Install megatools
aptitude install megatools

Configuration

The next step, if you don’t have an account yet, is create one following this steps:

# megareg -n  -e  -p  --agree 
Registration email was sent to 'email@albertolarripa.com'.
To complete registration, enter signup link bellow.

Enter signup link for email@albertolarripa.com, or press CTRL+C: https://mega.co.nz/#confirmLXHgeukasda82o4-mqz4mnALÑKSdjlasjd klasjlaNhegxSvGAJKLSDNASJKasdnjkaYUBhbGJlcnRvASdlñjkaiops2b20JdHdlZASLKASOPdas54

The account was registered successfully!

Now to simplificate the use of the tool, create a new file in the user account that will be used for megatools utility, or a new global configuration file in /etc that will use with –config=PATH option.

# cat /root/.megarc 
[Login]
Username = email@albertolarripa.com
Password = yourpassword

Finally install this packages:

aptitude install glib-networking gobject-introspection curl libcurl4-gnutls-dev

Now, check if your mega account is accesible from your server using the tool megals:

# megals
/Contacts
/Inbox
/Root
/Trash

Using the tools

Is the moment for see what offer the megatools utility

  • megadf: Show total available, used, or free space in the cloud
# megadf --mb
Total: 51200
Used: 6339
Free: 44860
  • megadl: download exported files and directories from Mega.co.nz

Download exported file:

$ megadl 'https://mega.co.nz/#!7YVWhCzZ!bauBlAkKKvv8hIm-8-qFmGOYS289ToQWN7rGFPzXB_w'

Download exported folder:

$ megadl 'https://mega.co.nz/#F!HIlFDajT!HLiVvQQkSe1d0ogxZuaOJg'
  • megadf: Mount remote filesystem locally via FUSE
$ mkdir -p mount
$ megafs mount
$ ls mount
Contacts Inbox Root Trash
  • megamkdir:  Create remote folder under your Mega.co.nz account

Create new folder:

$ megamkdir /Root/MyNewFolder
$ megals /Root
/Root
/Root/MyNewFolder

Add new contact to your contacts list:

$ megamkdir /Contacts/some@email.com
  • megamv: move or rename files or folders on your Mega.co.nz account

Rename a file:

$ megals /Root
/Root
/Root/README.TXT
$ megamv /Root/README.TXT /Root/README
$ megals /Root
/Root
/Root/README

Move file to a another folder:

$ megals -R /Root
/Root
/Root/README
/Root/MyFolder
$ megamv /Root/README /Root/MyFolder
$ megals -R /Root
/Root
/Root/MyFolder/README
  • megaput: upload files to your Mega.co.nz account

Upload file to the /Root:

$ megaput README
$ megals /Root
/Root
/Root/README

Upload file, while naming it differently:

$ megaput --path /Root/README.TXT README
$ megals /Root
/Root
/Root/README.TXT
  • megasync: Upload/download entire directories to/from your Mega.co.nz account

Upload directory.

$ megasync --local MyBackups --remote /Root/Backups

Download directory.

$ megasync --local MyBackups --remote /Root/Backups --download

MegaSync script

Mega don’t offer posibility to maintain a real-sync , for example, If I remove a file in my server and launch the megasync utility, MEGA not removed the file on the cloud, having a dirty cloud. To resolve it, I created a personal script that I share with you.

#!/bin/sh

MEGASYNC='/usr/local/bin/megasync'
MEGARM='/usr/local/bin/megarm'
LOCALDIR="/usr/local/data/backups"
REMOTEDIR="/Root/albertolarripa/backups"
SEDLOCALDIR="\/usr\/local\/data\/backups"
SEDREMOTEDIR="\/Root\/albertolarripa\/backups"
BACKUP_TIME=`date +%c`
LOG="/usr/local/data/backups/mega.log"
hostname=`hostname`

#Obtain the files that not exists in the local server

DELETE=`$MEGASYNC --dryrun --reload --download --local $LOCALDIR --remote $REMOTEDIR | sed 's/F '$SEDLOCALDIR'/'$SEDREMOTEDIR'/g'`

# And remove it

for i in $DELETE; do
        $MEGARM $i
done

# Run the synchronization to Mega

SYNC=`$MEGASYNC --no-progress --local $LOCALDIR --remote $REMOTEDIR`

echo "[$BACKUP_TIME][$(hostname)] synchronization to mega done!!" > $LOG
echo "Files removed $DELETE" >> $LOG
echo "Files synchronized" >> $LOG

4 thoughts on “MEGATOOLS – Synchronizing your backups to MEGA

  1. Hello,
    Thank you for this tool. It succesfully works on my ubuntu box, but it is not working it in my freebsd box (FreeBSD 8.3-RELEASE-p7 i386). I builded it from source megatools-1.9.91.tar.gz and when i run command, for example:
    # megadf
    I got error message:
    GLib-GIO-Message: Using the ‘memory’ GSettings backend. Your settings will not be saved or shared with other applications.
    ERROR: Can’t login to mega.co.nz: API ‘us’ failed: HTTP POST failed: Connection failed: Unacceptable TLS certificate

    Can you help to resolve this issue ?

  2. I tried to uploading a directory:

    $ megasync –local MyBackups –remote /Root/Backups

    Doesn’t work:

    Remote directory not found /Root/Backups

  3. Alberto, thanks a bunch for the script.
    According to https://github.com/megous/megatools/, megasync is now called megacopy.
    I’ve updated your script accordingly, and used a different delimiter for ‘sed’ to simplify configuration.


    #!/bin/sh

    LOCALDIR="/usr/share/webapps/owncloud/data/gcg/files/InstantUpload"
    REMOTEDIR="/Root/MyOwnCloud"
    LOG="/usr/share/webapps/owncloud/data/gcg/mega.log"
    hostname=`hostname`

    MEGACOPY='/usr/bin/megacopy'
    MEGARM='/usr/bin/megarm'
    BACKUP_TIME=`date +%c`

    #Obtain the files that not exists in the local server

    DELETE=`$MEGACOPY --dryrun --reload --download --local $LOCALDIR --remote $REMOTEDIR | sed 's|F '$LOCALDIR'|'$REMOTEDIR'|g'`

    # And remove it

    for i in $DELETE; do
    $MEGARM $i
    done

    # Run the synchronization to Mega

    SYNC=`$MEGACOPY --no-progress --local $LOCALDIR --remote $REMOTEDIR`

    echo "[$BACKUP_TIME][$(hostname)] synchronization to mega done!!" > $LOG
    echo "Files removed $DELETE" >> $LOG
    echo "Files synchronized" >> $LOG

Leave a Reply

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