web analytics

Script to take mysql backup in plesk server and rsync to backup server

The following script will take the backup of all mysql databases and copy them to your backup server. This is a plesk server.

#!/bin/sh

TO=”abc@xyz.com”
CC=”def@xyz.com”
SUBJECT=”MySql backup – $HOSTNAME”
SUCCESS_MSG=” Mysql Backup Done”
ERROR_MSG=”URGENT – Mysql backup error ”
error_file=/root/scripts/mysql_error_file
rsync_progress=/root/scripts/rsync_mysql_progress

## Find files which are older than 2 days and delete them ###
find  /home/rsync_backups/mysql_backup/ -type f -mtime +2 -exec rm {} ;

mysql_user=admin
mysql_pass=`cat /etc/psa/.psa.shadow`
cd /home/rsync_backups/mysql_backup/

mysql_databases=`echo “show databases;” |mysql –user=$mysql_user –password=$mysql_pass | grep -v ^Database$`

for i in $mysql_databases
do
mysqldump –user=$mysql_user –password=$mysql_pass $i > $i.`date +%F`.sql  2>&1
done

if  [ $? -eq 0 ]
then
rsync -avzrp –delete /home/rsync_backups/mysql_backup user@xxx.xxx.xxx.xxx:/home/rsync_backups/ > $rsync_progress
if [ $? -eq 0 ]
then
cat $rsync_progress | /bin/mail -s “$SUCCESS_MSG” “$TO” “$CC”
else
cat $rsync_progress | /bin/mail -s “$ERROR_MSG” “$TO”  “$CC”
fi
else
echo “Mysql dump errored, please check” | /bin/mail -s “$ERROR_MSG” “$TO” “$CC”
fi

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>