Shell Script To Backup MySql Database Server

A working mysql backup script - explains how to backup MySQL Database Server using a cron job under UNIX or Linux like operating systems

Create file backscript.sh,

Add ,
<--
echo "Back up started"
# take the current date and take back up
MyUSER=" " # USERNAME
MyPASS=" " # PASSWORD
MydatabaseName=" " # databse

datum="$(date +"%d-%m-%Y")"
echo ${datum}
mysqldump -u${MyUSER} -p${MyPASS} ${MydatabaseName} > /var/lib/sqlback/$MydatabaseName-${datum}.sql

echo "back up end"
# take the 7 days before the current date and delete the back up file
datumDel=`date +"%d-%m-%Y" --date="7 days ago"` # you can change the days here

echo ${datumDel}
rm /var/lib/sqlback/$MydatabaseName-${datumDel}.sql

-->

Add line in the linux crontab something like below

59 22 * * /var/lib/sqlback/backscript.sh

Comments