Effortless Automatic Synchronization of MySQL/MariaDB Backup with MEGASync

Mega is a cloud storage and file-hosting service provided by Auckland-based Mega Limited. Users primarily access the service through web-based applications. Mega is renowned for its robust security, as all files are encrypted locally before being uploaded. This end-to-end encryption ensures that no unauthorized party, including Mega Limited employees, can access the files without the encryption passkey. The service is also notable for its generous 50 GB storage allowance for free accounts.

Here are some key features of the MEGA cloud service:

  • 50 GB of free cloud storage
  • End-to-end encryption
  • Cross-platform compatibility (Linux, Windows, macOS, Android, iOS)
  • Selective sync functionality
  • Web browser accessibility

Installing MEGA in Linux

Unlike Google Drive, MEGA provides both a substantial 50 GB of free storage and a dedicated desktop client for Linux. Binaries are readily available, simplifying the installation of MEGASync, the desktop sync client for MEGA, across all major Linux distributions. You can obtain the MEGASync binary package from the link below. Choose the version corresponding to your Linux distribution (or the one upon which your distribution is based). The MEGASync client functions similarly to the Dropbox client.

Download MEGAsync

  1. Create a MEGAsync account and sign in.

    [Image: MEGA login screen]

  2. Create a “MEGAsync” folder in your Linux home directory and a “full_MySQL” folder within your MEGA cloud storage. The “full_MySQL” folder will serve as the destination for MySQL/MariaDB backups. Files placed in the local “MEGAsync” folder will be synchronized to the “full_MySQL” folder in your MEGA cloud storage. (You can create additional folders for other purposes, such as Koha and Wordpress backups, as needed.)

    [Image: File structure showing local “MEGAsync” folder and MEGA cloud storage folder]

Configure Sync Permissions:

Grant sync permissions to the relevant folders within the MEGAsync settings.

[Image: MEGAsync settings screen showing sync permissions]

Automate MySQL/MariaDB Backups:

  1. Open Applications > Accessories > Terminal.

  2. Type crontab -e and press Enter.

  3. Select a text editor when prompted. Nano is a suitable choice.

  4. Navigate to the bottom of the cron file using the down arrow key.

  5. Copy and paste the following command to schedule a full MySQL/MariaDB backup at 4:30 PM daily:

    1
    
    30 16 * * * mysqldump -uroot -p'admin@lib' --all-databases --single-transaction --quick --lock-tables=false | xz > /home/mahesh/MEGAsync/full_MySQL/full-backup.sql.xz
    

    To back up specific databases, use the following commands, adjusting the database names and paths accordingly:

    • For Koha_library database backup:
    1
    
    30 16 * * * mysqldump -uroot -p'admin@lib' koha_library | xz > /home/mahesh/MEGAsync/koha_backup/koha_library.sql.xz
    
    • For Wordpress database backup:
    1
    
    30 16 * * * mysqldump -uroot -p'admin@lib' wordpress | xz > /home/mahesh/MEGAsync/wordpress/wordpress.sql.xz
    

    Important: Replace admin@lib with your actual MySQL/MariaDB password and modify the destination folder paths if necessary.

  6. Save the cron file by pressing Ctrl+O, followed by Enter.

  7. Exit the cron editor by pressing Ctrl+X.

Alternative Approach:

Create a bash script and schedule it using crontab.

  1. Create a bash script (e.g., “uploadtomega.sh”) using:

    1
    
    sudo nano /usr/local/bin/uploadtomega.sh
    
  2. Add the following content to the script, customizing the database names, passwords, and paths as needed:

    1
    2
    3
    4
    5
    
    #!/bin/bash
    
    mysqldump -uroot -p'admin@lib' --all-databases --single-transaction --quick --lock-tables=false | xz > /home/mahesh/MEGASync/full_MySQL/full-backup-$(date +%d-%m-%Y-%H.%M).sql.xz
    mysqldump -uroot -p'admin@lib' koha_library | xz > /home/mahesh/MEGASync/koha_backup/koha_library-$(date +%d-%m-%Y-%H.%M).sql.xz
    mysqldump -uroot -p'admin@lib' wordpress | xz > /home/mahesh/MEGASync/wordpress/wordpress-$(date +%d-%m-%Y-%H.%M).sql.xz
    
  3. Save the script and make it executable:

    1
    
    sudo chmod a+x /usr/local/bin/uploadtomega.sh
    
  4. Edit the root user’s crontab:

    1
    
    crontab -e
    
  5. Add the following line to schedule the script execution at 4:30 PM daily:

    1
    
    30 16 * * * /usr/local/bin/uploadtomega.sh
    
  6. Save and exit the cron editor.

After setting up the automated backups, you can find the backup files in your local “MEGAsync” folder after 4:30 PM each day. These backups will be synchronized to your MEGA cloud storage as well.

Reference: https://help.ubuntu.com/community/CronHowto#Crontab_Example

Licensed under CC BY-NC-SA 4.0
Last updated on Jan 04, 2023 02:52 +0100