Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-07-2011, 02:09 AM   #1
LQ Newbie
Registered: Feb 2008
Posts: 7

Rep: Reputation: 0
Optimizing Mysql backup bash script


I have created a bash script that makes full mysql backups every day, set in crontab without compression as the databases sizes are small. At the end of the script backups are copied to cifs mount point (mounted in fstab). Script runs without problem.
The problem is that the script is not perfect. As you can seen, the code in bold are repetitive. Better would be to add new variables and add code with with show databases and fetch that output and add for and do loop etc, and avoid repetitive lines. Any suggestions to improve my script?? please help. Thx in advance

FECHA=`date +"%d%m%Y_%H%M"`
cd /var/backups/MySql/
#Borrar ficheros *.sql con 7 dias de antiguedad en la ruta local /var/backups/Mysql
find -name '*.sql' -type f -mtime +7 -exec rm {} \;
cd /mnt/BackupMySql/
#Borrar ficheros *.sql con 14 dias de antiguedad en la ruta cifs mountpoint /mnt/BackupMySql segun /etc/fstab
find -name '*.sql' -type f -mtime +14 -exec rm {} \;
#Inicio generacion backup con fecha actual
mysqldump -uroot -ppassword --opt bugtracker > /var/backups/MySql/bugtracker_$FECHA.sql
mysqldump -uroot -ppassword --opt mysql > /var/backups/MySql/mysql_$FECHA.sql
mysqldump -uroot -password --opt --single-transaction information_schema > /var/backups/MySql/information_schema_$FECHA.sql
mysqldump -uroot -password --opt qstock > /var/backups/MySql/qstock_$FECHA.sql
mysqldump -uroot -ppassword --opt wiqui > /var/backups/MySql/wiqui_$FECHA.sql
mysqldump -uroot -password --opt wordpress > /var/backups/MySql/wordpress_$FECHA.sql

cp -f /var/backups/MySql/*.sql /mnt/BackupMySql/
Old 10-07-2011, 05:08 AM   #2
Registered: Aug 2007
Distribution: openSUSE 12.1
Posts: 98

Rep: Reputation: 24
mysql -uroot -ppassword --batch -e "SHOW DATABASES" | while read f
mysqldump -uroot -password --opt $f > /var/backups/MySql/$f_$FECHA.sql

maybe you also should add:

'SHOW GRANTS FOR ''', user, '''@''', host, ''';'
) AS query FROM mysql.user" | \
mysql $@ | \
sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'

This dumps the GRANTS so it can be imported againg when the server need rebuilding after a crash....


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimizing a Bash Script zokken Programming 3 04-13-2011 12:29 AM
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
optimizing this bash script yah0m Programming 6 04-17-2009 07:42 PM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
bash backup script kristof_v Programming 4 07-24-2007 07:33 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration