Script: mega_db_save_upload.sh

In this document:

Description

Configuration

Parameters

Possible codes returned by script

Setting up Cron

Example

Related

Description

This script preforms an encrypted backup of a MySql database dumped to bz2 for a given user and sends that backup to Mega.nz account specified and logs the results.

This script by default deletes any backup of the current user database from Mega.nz that is more then 60 days old.

This script by default deletes the backup on the current server once the Mega.nz backup file has been confirmed.


This script was written to work with Virtualmin by default but can easily be configured to work any Linux system running MySql databases.


If there is an error in the backup the script can be configured to send out an email using sendmail. If your using a default install of Virtualmin then sendmail will already be installed.


Script can easily be set to work as a cron job for automated backup.


Configuration

There are configuration settings that can go into .mega_scriptsrc for section [MEGA_DB_SAVE_UPLOAD]

See Configuration file.

See Configuration file - Example for an example of setting up to send email.


Parameters

-u

Required: The Unix User to do the database backup for.

-d

Required: the name of the database to be backed up to Mega.nz.

-a

Optional: The number of days as a positive integer before todays date to delete files older then from Mega.nz.
Default is 60 days.
Setting -a will override DAYS_TO_KEEP_BACKUP set in Configuration file.

-b

Optional: Specify -b for location of backup directory on the local server.

-i

Optional: The configuration file to use that contain the credentials for the Mega.nz account you want to access.
See Set up .megarc.

-g

Optional: -g the gpg owner of the public key to use for encryption.
If ENCRYPT_OUTPUT is set to true and -g option is not set then Configuration file must have the value set for GPG_OWNER.
Setting -g option overrides GPG_OWNER set in Configuration file.

-o

Optional: The Log file to log to results of running mega_db_save_upload.sh.
Can be value of "t" in which case results are outputted to the terminal window
Can be value of "s" in which case no results are written to a log file or terminal window.
Defaults to /home/${USER}/logs/mega_db.log
Setting -o option overrides the LOG option [MEGA_DB_SAVE_UPLOAD] of Configuration file.

-s

Optional: The System Log file to log to results user errors running mega_db_save_upload.sh.
Can be value of "t" in which case results are outputted to the terminal window
Can be value of "s" in which case no results are written to a log file or terminal window.
Defaults to /var/log/mega_db.log (requires script be run as root)
Setting -s option overrides the SYS_LOG option [MEGA_DB_SAVE_UPLOAD] of Configuration file.
-s can be set to the same value as -o.

-m

Optional: send email on error. y for send on error and n for no send email on error.
Email options of  [MEGA_DB_SAVE_UPLOAD] of Configuration file must be configured.
Setting -m will override SEND_EMAIL_ON_ERROR in Configuration file.

-n

Optional: -n the name of the server used in logs both locally and in path name on Mega.nz.
If this flag is not set then SERVER_NAME must be set in Configuration file.
Setting -n will override SERVER_NAME in Configuration file.

-r

Optional: Specify -r the directory on the Mega.nz server to save the backup in.

-f

Optional -f is a special option that can forget or ignore testing for certain conditions.

-f 'c' skips testing for mysql configuration. If you know that your mysql configuration file is set up properly for mysqldump and your are getting an error 50 then you can use -f 'c' to skip the check for  mysql configuration. See also MYSQL_TEST_CNF in Configuration file.

-f 'd' skips testing for mysql database. If for any reason you know that your database exist and you are getting an error 52 then you can use -f 'd' to skip the check if the database exist. See also MYSQL_TEST_DB in Configuration file.

-f 'u' skips testing for unix user. Must override ( LOG or use -o), BAK_DIR and MEGA_BACKUP_DIR in Configuration file. See also TEST_USER in Configuration file.

-f 'g' skips testing if gpg public key exist. If for any reason your are getting an error of 117 and you know your public key is imported then you can use -f 'g' to skip the check if public key exist. See also TEST_GPG in Configuration file.

Options for -f can be combined. For example: -f 'dc' would skip checking of mysql configuration and database.

-h

Optional: Display Script help

-v

Optional: Display script version



Possible codes returned by script

Code

Definition

0

Normal Exit script found no issues

3

No write privileges to create log file.

4

Log file exist but no write privileges.

20

No argument for user supplied for user

21

No argument for user supplied for database

30

File not found on mega.nz

40

If no argument is give for parameter one, the path to check.

41

If megamkdir had error creating directory

42

If a file was found with the same name as part of the path

50

~/.my.cnf for mysqldump is not found or script does not have read permissions.

51

~/.megarc for mega.nz is not found or script does not have read permissions.

52

The database passed in to the script does not exist

53

The user passed in to the script does not exist

60

bzip2 not found

70

Configuration file for mega scripts does not exist

71

No read permissions for configuration file

72

It seems that no values have been set in the configuration file for section [MEGA_DB_SAVE_UPLOAD]

73

Invalid value in configuration

100

There is another mega process running. Can not continue.

101

megarm not found. Megtools requires installing or properly configure MT_MEGA_RM option of .mega_scriptsrc to point to the megarm location

102

megals not found. Megtools requires installing or properly configure MT_MEGA_LS option of .mega_scriptsrc to point to the megals location

103

megaput not found. Megtools requires installing or properly configure MT_MEGA_PUT option of .mega_scriptsrc to point to the megaput location

104

No argument supplied for Mega Server Path

105

megadf can not be found. Megtools requires installing or properly configure MT_MEGA_DF option of .mega_scriptsrc to point to the megadf location

110

No mega server path specified

111

Config can not be found or we do not have read permissions. See Set up .megarc

112

The file to upload does not exist or can not gain read access.

115

megamkdir not found. Megtools requires installing or properly configure .mega_scriptsrc to point to the megamkdir location

116

Null value. Config file GPG_OWNER of .mega_scriptrc or -g option must be set.

117

Config file GPG_OWNER of .mega_scriptrc or -g option must be set and must be a valid GPG Public Key.
See: How to Encrypt Files on Linux using GnuPG

118

Unable to create directory to place backup file in. Make sure you have the proper permissions create the directory for specified directory location.

119

Unable to write in directory for backup file. There are no write permissions for the backup directory even though the directory exist.


Setting up Cron

You can set this script to run as a cron job. One entry for each database you want to back up to Mega.nz on a schedule.


To add an entry into cron as root login to the terminal as root user.


# crontab -e


The first time you use cron you may have to choose an editor. I would recommend nano as it is the simplest.

Add your entry at the end of any current cron jobs that exist.

Example Cron entry

The following entry would run once a day at 9:40 PM and upload to the default account set in .megarc.

This entry would backup database "sammy_wordpress" for the user "sammy".

40 21 * * * /bin/bash /usr/local/bin/mega_db_save_upload.sh -u 'sammy' -d 'sammy_wordpress' >/dev/null 2>&1

By Default you would find the log entries in /home/users/sammy/logs/mega_db.log

The file path on Mega.nz would look something like this
"/Root/myserver/backups/sammy/database/2018-06-07-21-40-01_sammy_wordpress.sql.bz2.gpg"




The following entry will run once a day at 1:00 AM and use a second mega account that the credentials stored in the /.megarc_bob

This entry would backup database "bugtracker_bob_db" for the user "bob".

0 1 * * * /bin/bash /usr/local/bin/mega_db_save_upload.sh -u 'bob' -d 'bugtracker_bob_db' -i ~/.megarc_bob >/dev/null 2>&1

By Default you would find the log entries in /home/users/bob/logs/mega_db.log

The file path on Mega.nz would look something like this
"/Root/myserver/backups/bob/database/2018-06-07-01-00-01_bugtracker_bob_db.sql.bz2.gpg"


See Crontab Generator for an online tool to help generate cron entries.


Example


The following would backup database "sammy_wordpress" for the user "sammy". By Default you would find the log entries in /home/users/sammy/logs/mega_db.log

The file path on Mega.nz would look something like this
"/Root/myserver/backups/sammy/database/2018-06-07-21-40-01_sammy_wordpress.sql.bz2.gpg"


# /bin/bash /usr/local/bin/mega_db_save_upload.sh -u 'sammy' -d 'sammy_wordpress'



The following entry would backup database "bugtracker_bob_db" for the user "bob". By Default you would find the log entries in /home/users/bob/logs/mega_db.log

The file path on Mega.nz would look something like this
"/Root/myserver/backups/bob/database/2018-06-07-01-00-01_bugtracker_bob_db.sql.bz2.gpg"


# /bin/bash /usr/local/bin/mega_db_save_upload.sh -u 'bob' -d 'bugtracker_bob_db' -i "$HOME/.megarc_bob"



Related

Getting Started

Setting up .megarc

Configuration File

GnuPG Encrypt and Decrypt

Set up Server for MySqlDump

Crontab Generator

mega_db_save_upload.sh Latest Source