Schedule Backups for MySQL and MariaDB Databases

11

4170-1-mysql-mariadb Have you ever felt a moment of panic when you realize that your data might be lost? Human errors, malicious attack, systems crash or disasters happen when least expected. So it is highly important to be always prepared to such situations by keeping up-to-date backups that can help with data recovery. Automated backups ensures in-time refreshment of the data copies and eliminates the need for manual tracking and setup. In this article, we’ll examine how to create a backup schedule, and offer hints for better data scheduling and restoring.

Jelastic PaaS provides a convenient and straightforward script for backing up stored data on MySQL and MariaDB database nodes. The process is as simple as providing the required parameters and can be accomplished in a matter of minutes. Herewith, no specialized knowledge is needed – just follow the setup steps to configure the appropriate cron expression.

Set Up Database Backups

For this tutorial, we’ll use the default backup script provided for MySQL and MariaDB database nodes at Jelastic PaaS.

1. The source code of the script can be viewed directly within the dashboard. Click the Config button next to your database to open the built-in file manager and locate the /var/lib/jelastic/bin/backup_script.sh file.

4170-1-database-config-backup-script

Tip: If you don’t have an environment with a MySQL or MariaDB database yet, follow the Database Hosting guide to create one.

2. To automate the script execution, we’ll use the cron scheduler available within the containers out-of-the-box. Open the /var/spool/cron/mysql file and provide a cron expression in the following format:

{frequency} {path-to-script} {script-parameters}

4170-1-schedule-backups-cron-job

You can use the commented examples as a template to define the required parameters:

  • {frequency} – sets the time of the script execution (see the guide linked above for details), e.g., */10 * * * *;
  • {path-to-script} – points to the required script (default backup script is available for MariaDB and MySQL at /var/lib/jelastic/bin/backup_script.sh);
  • {script-parameters} – provides custom parameters for the defined script; the following ones are available in this case:
    • -m {mode} – to backup whole or part of the database (“-m dumpall” or “-m dump” respectively);
    • -c {count} – to set a number of old backups to keep (e.g., “-c 3” to keep 3 old backups alongside the most recent one);
    • -u {user} and -p {password} – to provide access credentials for the database (access credentials can be found in the after-creation email);
      Note: If your {password} contains special characters, it should be included in the double quotation. Also, due to the cron specifics, you need to provide additional escaping for the % symbol by adding a backslash before it (e.g., -p “passw\%rd”).
    • -d {databases} and -t {tables} – to optionally specify a list of databases (e.g., “-d db1[,db2,db3…]”) or tables within a database (e.g., “-d db -t table1[,table2,table3…]”) to backup.

3. For example, if you want to backup a whole database every 10 minutes with the help of the default script, then the string of parameters should be as follows:

4170-1-schedule-backups-cron-job-example

*/10 * * * * /var/lib/jelastic/bin/backup_script.sh -m dumpall -c 3 -u root -p passw0rd

Save the settings to apply them.

4. To store data on some other node or remote server, you can configure the appropriate mount point for the database.

4170-1-schedule-backups-mount-points

Note: Any existing data in the mount point directory will be replaced with the files from the remote directory.

Alternatively, you can create a custom backup script, run-able from the remote server (database host, user, and password details will be required to establish a connection).

5. Wait for the script to be executed (depends on the configured cron frequency) and navigate to the /var/lib/jelastic/backup directory.

4170-1-schedule-backups-directory

If the backup operation was successful, you’ll see the appropriate .bz2 archive within the directory.

Download Database Backups

There are multiple options to download a (backup) file from a Jelastic PaaS container:

1. The most straightforward variant is to use the configuration file manager directly in the dashboard.

4170-1-schedule-backups-download-backup-file

Locate the required file, hover over it, and select Download from the context menu.

2. Another option is to use a SFTP/FISH protocol connection.

4170-1-schedule-backups-sftp-download

The required access details can be viewed within the dashboard.

3. Also, you can install the FTP add-on for your database to manage files via FTP.

Note: A public IP address is required for this option. If needed, it will be automatically attached to the node during the FTP add-on installation.

4170-1-schedule-backups-ftp-add-on

Based on your preferences, you can choose any of the proposed options.

Restore Database Backups

In case of a necessity, you can easily restore a database from the backups:

1. Access the phpMyAdmin admin panel with the credentials you received in the email sent after the creation of a database node.

4170-1-schedule-backups-mysql-node-created-email

2. Navigate to the Import tab, tick the Browse your computer option, and use the Choose File button to upload the required backup.

4170-1-schedule-backups-phpmyadmin-import

Click Go at the bottom of the page.

3. When the import is successfully finished, you’ll see the appropriate notification in the admin panel.

4170-1-schedule-backups-database-restored

That’s all! Now, you can be sure that all your database backups are made on a consistent regular basis and, in case of a necessity, can be restored. Keep your data secure with Jelastic PaaS. Try it out yourself for free at our Jelastic PaaS platform.