Backing up folders with MegaCMD and bash

Automating the backup of crucial information can be done with MegaCMD.

For this demonstration, I installed MegaCMD on the server that I want to backup data from. Afterwards, I setup a Cron Job to automatically backup data to my Mega account.

First, I installed MegaCMD and logged in using the non-interactive command mega-login. I logged in so I do not need to include my credentials in the script.

mega-login email@domain.com password

NOTE: If you have 2 factor authentication on your account, append the --auth-code= to the end of mega-login command.

Then I choose what directory I wanted to backup to my Mega Account and added it to a script that will run at the specified time I set using a Cron Job.

The script is below. I added a curl command to notify me when the script was executed.  curl will attempt to GET response from my n8n instance, which will then execute a workflow that will notify my Discord and Mattermost server.  

  1. Create the script with nano, I made a directory called scripts to store all my scripts.

mkdir ~/scripts && cd ~/scripts

nano mega-backup.sh

#!/bin/bash
# Select items to backup
mega-put -c /home/angel/backup2mega/ /linux_backup/
# Optional - Use curl to notify me when the script is executed
curl https://n8n.mydomain.com/webhook-uuid
Contents of mega-backup.sh

2. Make the script executable:

chmod +x ~/scripts/mega-backup-script.sh

3. Add it to Cron and choose how often it is executed.

crontab -e

0 0 * * * sh /home/angel/scripts/mega-backup.sh

To know when it was executed, you can view the cron logs located at var/log/cron

sudo cat /var/log/cron | grep -e 'mega-backup' | less
In this command, I am using grep to filter out the lines that contain mega-backup and viewing the output with less