h1

mysql databases backup script on cpanel servers

July 23, 2008

Daily mysql backup with 7 days retention period.

root@server1 [~]# cat /scripts/mbak.sh
#!/bin/bash
Time=`date “+%Y.%m.%d-%A”`
baksrc=/var/lib/mysql
bakdst=/backup/mysqlbackup
dumpdb=/usr/bin/mysqldump

yum -y dialog nmap ncurses

mkdir -p $bakdst/$Time

{ for I in 10 20 30 40 50 60 70 80 90 100 ; do
echo $I
sleep 1
done
echo; } | dialog –gauge ” Starting to backup all databases ” 6 70 0

ls -lhd  $baksrc/*_* | awk {‘print $9}’ | cut -d/ -f5 | grep -v ib_* > /root/mysqldd-list

for db in `cat /root/mysqldd-list` ;
do

{ for I in 10  50  100 ; do
echo $I
sleep 1
done
echo; } | dialog –gauge ” Creating backup of $db ” 6 70 0

$dumpdb $db > $bakdst/$Time/$db.sql  2> $bakdst/$Time/error.log

{ for I in 10  50  100 ; do
echo $I
sleep 1
done
echo; } | dialog –gauge ” Database : $db backup completed  ” 6 70 0

echo ” ……………..  $db backed up on `date “+%Y.%m.%d.%T-%A”`  …………………………

.. ”
sleep 3
echo ” …………….. working on the next db backup ………………………”
sleep 3
echo ” ……………… Taking some rest before that ……………………….”
sleep 3
done

$dumpdb  mysql > $bakdst/$Time/mysql.sql 2> $bakdst/$Time/error.log

echo ” All Databases backup up successfully to folder $bakdst/$Time/ ”
echo ” Check for any errors at : $bakdst/$Time/error.log ”
wall ” All Databases backup up successfully to folder $bakdst/$Time/ ”
wall ” Check for any errors at : $bakdst/$Time/error.log or the database backup ”
echo ” removing older backups”
sleep 2
echo ” hold on, let me work for 3 to 10 mins”
/usr/bin/find $bakdst -type d -maxdepth 1 -mtime +7 -exec rm -fr {} \;
echo ” …………….. Finishing up the entire process ”
sleep 3
echo ” …………….. Successfully Done ……………………….”
wall ” ***** Note Again **** Check for any errors at : $bakdst/$Time/error.log or the database backup ”
exit 0

root@server1 [~]#

root@server1 [~]# chmod +x /scripts/mbak.sh ( make the script executable )
root@server1 [~]# /bin/sh /scripts/mbak.sh (run script to make database backups )

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: