How to create scheduled  cloud backups of your database using mega with webmin
Tags: webmin, linux, database, mega
Published: 2021-11-04 15:32:16

Hi! The procedure to create a scheduled backup in the cloud with webmin is divided into the following steps:

1. **Configuration and installation of the mega command line tool (megatools):** To install megatools, configure it with your mega.co.nz account, and know how to use 'megaput' and 'megarm', I recommend you to follow the instructions in this [post](http://blog.jeshurun.ca/technology/backup-files-mega-nz-from-command-line-linux-server). Once you have finish the installation and configuration of mega tools, I recommend doing a couple of tests with megaput and megarm of any file they have on their server to test if their configuration was well done.

2. **Configuring webmin to accept dates in the file name:** We need another configuration to make webmin agree to run this type of override, otherwise it will think that the name that is placed in our file will be 'static'. For this case we will use the following unix date format:% Y-% m-% d. This should respond to the year, month and day respectively. First we go to Webmin -> Servers -> MySQL Database Server. Then at the top left we find a module configuration button and select it to see more advanced options. Here we look for the option Dostrftimesubstitution of backup destinations? Yes, and we save.

3. **Configuring scheduled database backups:** In this case, the first thing we will do is go to the management panel of our database server, which for this case will be MySQL. We then go to Webmin-> Servers -> MySQL Database Server. Once here, we select the database of our interest and we click Backup Database. Here we will configure the following parameters.

* **Backup destination:** We select 'path on server' and here we must make sure to indicate a route with parameters based on date format. Therefore our absolute path to place would be:

`/ any-directory/%Y-%m-%d-mi-data-base.sql.gz`




* **Other backup options:**  Here I recommend selecting compression by gzip. In addition to this, the commands to debug and upload each backup file to the mega cloud were executed here.

* **Command to run before backup:**

```
rm "/ any-directory /" $ (date -d "$ date -3 days" + "% Y-% m-% d") - my-database.sql.gz && megarm --config = /root/.secrets/mega.cnf "/ Root / my-mega-folder /" $ (date -d "$ date -14 days" + "% Y-% m-% d") - my-base-of -data.sql.gz
```

Now we will break this command down into 2 parts:

```
rm "/ any-directory /" $ (date -d "$ date -3 days" + "% Y-% m-% d") - my-database.sql.gz
```

This command basically removes the backup from the server that is 3 days old, this in order to optimize resources and retain fewer backups.

```
megarm --config = / root / .secrets / mega.cnf "/ Root / my-mega-folder /" $ (date -d "$ date -14 days" + "% Y-% m-% d") - my-database-sql.gz
```

This other command removes from the mega directory all those backups that are from 14 days ago, this because generally you will want to have older backups in the cloud.

* **Command to run after backup:**

```
megaput --config = / root / .secrets / mega.cnf --path = / Root / Yaku.app / DEV / backups --disable-previews "/ backups /" $ (date +% Y-% m-% d ) -my-database-sql.gz
```
This command is executed after the backup is created on the server in the indicated folder, and its purpose is to send the generated backup to the MEGA cloud.

**Backup schedule:**

* **Send backup status email to:** Here we place an email to which we would like the backup confirmation to reach us
* **Send email for:** Here we will mark 'Only on failure' so that they do not reach us all the time, only in case the backup fails.
* **Scheduled backup enabled?** Yes, at times chosen below ..Simple schedule ..Daily (at midnight)

Once these options are selected, we click 'Save' and save our configuration. 

If everything has gone well, the backups should start to be generated and you can view them directly in your Mega account as well as on your webmin server.