Skip to content

Instantly share code, notes, and snippets.

@serverok
Created February 9, 2023 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serverok/5a247a5b8485a6a29764f12118a7f727 to your computer and use it in GitHub Desktop.
Save serverok/5a247a5b8485a6a29764f12118a7f727 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Author: admin@serverok.in
# Web: https://serverok.in/plesk-mysql-daily-backup-script
BACKUP_DATE="$(date +%Y%m%d-%H%M%S)"
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/${BACKUP_DATE}-${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' you@your-email.com
echo "Backup failed"
fi
done
find "/mysql-backup/" -maxdepth 1 -type f -name '*.sql' -mtime +3 -exec rm -f {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment