Plesk MySQL Daily Backup Script

Create file

mkdir /usr/serverok/
vi /usr/serverok/plesk-mysql-backup

With the following content

#!/bin/bash
# Author: [email protected]
# Web: https://serverok.in/plesk-mysql-daily-backup-script

if [ ! -d "/mysql-backup/" ]
then
    mkdir /mysql-backup/
fi

mysql -uadmin -p`cat /etc/psa/.psa.shadow` -e "show databases" | grep -v "+-------------" | grep -v "Database" | grep -v "information_schema" | grep -v "performance_schema" > /tmp/sok-dbs.txt

for db in `cat /tmp/sok-dbs.txt`
do
    /usr/bin/mysqldump -uadmin -p`cat /etc/psa/.psa.shadow` --opt --triggers --routines --events $db > /mysql-backup/$db.sql
    if [ $? -ne 0 ]; then
        # if you need email alert when backup fail, uncomment following line, add your email address
        # echo "database backup $db failed - `date`" | mail -s 'MySQL Backup failed' [email protected]
        echo "Backup failed"
    fi
done

If you need multiple copies of MySQL database kept, use

https://gist.github.com/serverok/5a247a5b8485a6a29764f12118a7f727

Make it executable

chmod 755 /usr/serverok/plesk-mysql-backup

Set it to run as cronjob

1 1 * * * /usr/serverok/plesk-mysql-backup > /var/log/sok-mysqldb-backup.log 2>&1

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *