Sunday, November 19, 2006

Dump database MySQL tiap hari

To backup MySQL everyday with simple shell script.
I'm using Ubuntu 5, MySQL 5.

Create a file at /etc/cron.daily .
Named, e.g. "backup-mysql" .
The content of the file is (between dashes) :
-------------------------------------------
#!/bin/sh

test -x /usr/bin/mysqldump || exit 0
/usr/bin/mysqldump --all-databases -u root --password=a | gzip - > /backup/mysql/mysql-`date +%F`.sql.gz
cd /backup/mysql
find . -atime +14 -delete

-------------------------------------------

#!/bin/sh
Tell the shell wich shell to run this script. Ubuntu default is bash shell.

test -x /usr/bin/mysqldump || exit 0
Check the /usr/bin/mysqldump is it there ? If not just exit.

/usr/bin/mysqldump --all-databases -u root --password=a | gzip - > /backup/mysql/mysql-`date +%F`.sql.gz
Dump all the databases from server with user 'root' and password 'a' (for example). Zipped with gzip and store at /backup/mysql. The file name generated differently every day prefixed with 'mysql-' follow by the date (from `date +%F`, it's a back quote pair).

cd /backup/mysql
find . -atime +14 -delete

Go to the folder and check if there are some file older than 14 days, delete it.

Tags: , , , , ,


No comments: