Linux.com

Home Linux Community Forums Linux Distributions Ubuntu [useful] MySQL datbase backup script

[useful] MySQL datbase backup script

Link to this post 14 May 09

Here is a script i use for backing my website up. Its a simple shell scipt that runs the mysql dump command.

mysqldump -udatabaseusername 
-pdatabasepassword
databasename >$(date).sql


so a typical usage of this might be:


mysqldump -uroot -pajellydoghnut
database1 >$(date).sql
]/code]

Where root is the username, ajellydoughnut is
the password and database1 is the database i
want to backup.

The advantage to doing it this way is that i can
go ahead and add this script as a cron joob to be
executed say every Tuesday at 11am meaning
that my database is backed up automatically for
me and a backup of that database is downloaded
to wherever this script is stored in .sql format
(typically i put it in a folder of its own in my
home directory)

Another handy thing is that because
of >$(date).sql on the end the script automatically
sets the current date and time as the filename
meaning no more messing around looking
through the creation time, its just there in the
filename :)


Hope this helps someone.

-Lenswipe

Link to this post 14 May 09

#!/bin/sh

USER=root
PSW=PassWord
FN='/bin/date +%Y-%m-%d'
BK_DIR="/root/MySQL_BACKUP"
PARAM="--quote-names --add-drop-table -u$USER -p$PSW"

cd $BK_DIR
mysqldump ${PARAM} trac > trac.sql
mysqldump ${PARAM} zabbix > zabbix.sql

tar -cvf mysql_$(date +%d.%m.%y).tar *.sql
gzip mysql_$(date +%d.%m.%y).tar
chmod 600 ${BK_DIR}/mysql_$(date +%d.%m.%y).tar.gz

Link to this post 14 May 09

is ther an adantage to doing it that way?

Link to this post 20 May 09

yes. I guess portability is one big advantage with this last one. You can use it in another server just changing the variables. Also, its a complete script (#!/bin/sh....)

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board