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).
-
Log on to your MySQL or MariaDB database and run the query below to create a database user
db_user_backupsto 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
PROCESSprivilege. 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; -
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/.envAfter
chmod -R 775, every user on the server can read the.envfile with the password. If several users work on the server, restrict it withsudo chmod 600 /backups/.envand run the script from root's cron (sudo crontab -e). -
Open db-backup.sh:
nano /backups/db-backup.shand 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]} doneThe code above loops through the array
DB_NAMESwith 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).
-
Open crontab:
crontab -e -
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