Knowledgebase: VPS
Automatic database backup generation for VPS Plesk
Posted by Jay Soni, Last modified by Jay Soni on 30 March 2015 12:41:57 PM

 Note! This article encompasses only MySQL database backup via Plesk.

Plesk in Linux does not provide the option to generate backups for MySQL databases via Backup Manager. The only way to get a backup is to use the export option via phpMyAdmin. Please click here for details.

The only way to automate the backup process is to configure a CRON for the following script. 

#!/bin/bash
# Set the datestamp, login credentials and backup directory
export date=$(date +\%Y\%m\%d)
export creds="-uadmin -p`cat /etc/psa/.psa.shadow`"
export backupdir="/mysqlbackup"
# delete week old files
find ${backupdir}/ -regex '.*.dump.gz' -mtime +4 -exec rm {} \;
# dump databases to the backupdir
echo "show databases;" | mysql ${creds} | egrep -v ^Database$ | \
awk '{print "mysqldump ${creds} "$1" | \
gzip > ${backupdir}/db-"$1"-${date}.dump.gz"}' | \
sh

As per the above script, backup will be created under the /mysqlbackup directory (create it manually on your server) and the name of the backup will be as db-dbName-yyyymmdd.dump.gz. Furthermore, upon execution the script will check for old backups and delete them automatically depending upon the value of mtime you want to specify.

(5 vote(s))
Helpful
Not helpful