Číst česky →

How to backup multiple MySQL or MariaDB databases automatically (MySQL 5.7, MariaDB 10.1)

2 min read Hukot net Linux Basics Backups VPS MySQL

We'll show you how to back up MySQL or MariaDB, which are on the same server, using your own bash script and cron.

Database Backup(s)

We'll assume we have three MySQL databases on our server named db-hukot-site, db-hukot-blog, db-hukot-app (don't worry about creating these databases, you'll substitute their names for yours where they are used in the script in step 3 below).

  1. Log on to your MySQL or MariaDB database and run the query below to create a database user db_user_backups to handle backups:

    GRANT LOCK TABLES, SELECT, SHOW VIEW, REPLICATION CLIENT, PROCESS ON *.* TO 'db_user_backups'@'%' IDENTIFIED BY '{COMPLEX-PASSWORD}';
    

    mysqldump in MySQL 5.7.31 and later needs the PROCESS privilege. Without it, it prints "Access denied; you need (at least one of) the PROCESS privilege(s)".

    Also run the following to make sure MySQL is configured to properly restore stored procedures:

    SET GLOBAL log_bin_trust_function_creators = 1;
    
  2. Set up the necessary directory structure and files:

    # create backup directory with environment and log file
    sudo mkdir /backups && cd /backups
    sudo touch .env db-backup.sh db-backup.log
    sudo chmod -R 775 /backups
    sudo chmod -R g+s /backups
    sudo chmod +x db-backup.sh
    
    # add mysql backup user credentials into environment file
    echo "export MYSQL_USER=db_user_backups" > /backups/.env
    echo "export MYSQL_PASS={COMPLEX-PASSWORD}" >> /backups/.env
    

    After chmod -R 775, every user on the server can read the .env file with the password. If several users work on the server, restrict it with sudo chmod 600 /backups/.env and run the script from root's cron (sudo crontab -e).

  3. Open db-backup.sh:

    nano /backups/db-backup.sh
    

    and paste the code below inside it, then save the file (Ctrl+X -> Y -> hit Enter).

    DB_NAMES=( 'db-hukot-site' 'db-hukot-blog' 'db-hukot-app' ) # replace with your own database name(s)
    BKUP_NAMES=()
    BKUP_DIR="/backups"
    
    # get total number of databases
    total_dbs=${#DB_NAMES[@]}
    
    # create backup file names
    for (( i=0; i<${total_dbs}; i++ )); do
      BKUP_NAMES[$i]="$(date +%Y%m%d%H%M)-backup-${DB_NAMES[$i]}.sql.gz"
    done
    
    # get backup users credentials
    source $BKUP_DIR/.env
    
    # create backups
    for (( i=0; i<${total_dbs}; i++ )); do
      # NOTE: --routines flag makes sure stored procedures are also backed up
      mysqldump --routines -u ${MYSQL_USER} -p${MYSQL_PASS} ${DB_NAMES[$i]} | gzip > ${BKUP_DIR}/${BKUP_NAMES[$i]}
    done
    

    The code above loops through the array DB_NAMES with the name(s) of the database(s) you want to back up.

Cronjob Setup

Set up a cronjob to run every midnight that runs the backup script and saves the result/output to the backup log. Cron runs commands with /bin/sh, so redirect the output with >> file 2>&1 (the &>> shortcut works only in bash).

  1. Open crontab:

    crontab -e
    
  2. Add the entry below to crontab:

    0 0 * * * /usr/bin/env bash /backups/db-backup.sh >> /backups/db-backup.log 2>&1
    

Note: While testing you can set the cronjob to run every minute instead, like below:

* * * * * /usr/bin/env bash /backups/db-backup.sh >> /backups/db-backup.log 2>&1

Or every 5 minutes (replace 5 with the number of minutes you want):

*/5 * * * * /usr/bin/env bash /backups/db-backup.sh >> /backups/db-backup.log 2>&1