MySQL – Daily database dumps, all nicely sorted.

Contributor Icon Contributed by bofh468 Date Icon November 11, 2003  
Tag Icon Tagged: MySQL

A nice shell script that can be run as a cron job to produce nice MySQL dumps for backup.


I run several boxes with MySQL installed. My backup strategy for MySQL includes the following:

– Each database needs to be dumped nightly to it’s own file.
– I keep all miscellaneous files (eg DB Dumps) under /backup
– Since I retain the files for 4 or 5 days, I need to tag the files
with the date they were dumped.

Under /etc, create a subdirectory “db-backup”:
mkdir /etc/db-backup

Create/edit the file /etc/db-backup/db-list.txt and put each database to be dumped in that file, one per line. The script, as posted here, can’t deal with blank lines.

A quick example would be (without the dashes):

—-
mysql
pdns
mimerdesk
phpbb
—-

Then here’s the backup script. I keep it in /etc/db-backup/db-backup.sh

#!/bin/bash
# Set a value that we can use for a datestamp
DATE=`date +%Y-%m-%d`
# Our Base backup directory
BASEBACKUP="/home/backups/mysql"

for DATABASE in `cat /etc/db-backup/db-list.txt`
do
# This is where we throw our backups.
FILEDIR="$BASEBACKUP/$DATABASE"

# Test to see if our backup directory exists.
# If not, create it.
if [ ! -d $FILEDIR ]
then
mkdir -p $FILEDIR
fi

echo -n "Exporting database: $DATABASE"
/usr/local/bin/mysqldump --user=root --opt $DATABASE | gzip -c -9 > $FILEDIR/$DATABASE-$DATE.sql.gz
echo " ......[ Done ] "
done

# AutoPrune our backups. This will find all files
# that are "MaxFileAge" days old and delete them.
MaxFileAge=4
find $BASEBACKUP -name '*.gz' -type f -mtime +$MaxFileAge -exec rm -f {} \;

The above assumes that you don’t have a password set for the root user. If you do, just add the -p switch into mysqldump. If you supply the password after “-p” you won’t be prompted for it.

Do be sure to chmod u+rwx on the script, and chmod u+rw on db-list.txt. If you’re including the root password in the file, chmod oa-rwx both files (actually, do that anyway).

Previous recipe | Next recipe |
 
  • bofh468
    I know that replying to oneself is bad karma... but I noticed a typo above. The backup files will be located under /home/backups/mysql/dbname-YYYY-MM-DD.tar.gz
  • Anonymous
    This is exactly what I've been looking for. Will try it soon.

    Ellen
  • Anonymous
    If you want to back up *every* database on the server, then replace this line

    for DATABASE in `cat /etc/db-backup/db-list.txt`


    with the following

    for DATABASE in `echo 'show databases' | mysql`
  • Anonymous
    <ul id="quote">/usr/local/bin/mysqldump --user=root --opt $DATABASE | gzip </ul>

    this is for user root with no password, if the user root has a password add '--password=' followed by your password INSTEAD of just putting '-p'

    I've tried using '-p' but then I got the password prompt.
  • Anonymous
    <ul id="quote"><h6>Anonymous wrote:</h6></ul><ul id="quote">/usr/local/bin/mysqldump --user=root --opt $DATABASE | gzip </ul>

    this is for user root with no password, if the user root has a password add '--password=' followed by your password INSTEAD of just putting '-p'

    I've tried using '-p' but then I got the password prompt.
    try using -ppassword
    instead of -p password.
  • Esquire
    Just on the code
    for DATABASE in `echo 'show databases' | mysql -p`


    --column-names=false should be added so that "Database", which is the column name, will not be included in the list.

    Thus...
    for DATABASE in `echo 'show databases' | mysql --column-names=false -p`
    <ul id="quote"><h6>Anonymous wrote:</h6>If you want to back up *every* database on the server, then replace this line

    for DATABASE in `cat /etc/db-backup/db-list.txt`


    with the following

    for DATABASE in `echo 'show databases' | mysql`
    </ul>
  • Anonymous
    Hi,

    I am also looking for a reliable and dependable solution to backups my databases on the server and want to keep each backup for atleast 15-30 days.

    I guess the solution mentioned here is more reliable than the ones available with php scripts.

    Can you tell me the command to be executed on cron tab to take daily backups at a specified time?

    Thanks.
  • Anonymous
    I am little new to coding stuff and am bit confused to paths and user with or without password.

    Can you please tell me exact script with following parameters:

    1. Type of hosting: shared
    2. Objective: to backup every database and keep them for 15 days
    3. Backup folder: /home/myid/db-backups
    4. etc folder is located at /home/myid/etc
    5. UserID: hello, password: iamok

    Please also tell me the command to be executed on cron tab to run your sh script, thanks.
  • thanks very much for this explanation
  • N GowriSankar
    This looks great. I like the idea of gzipping it.

    For our requirement, we were taking two level backups = Daily backups and Monthly backups. The daily backups are recycled every week and monthly backups are stored for much longer duration [As a matter of fact, we have not yet deleted them for past 3 years]. Of course, our backups are only 200+ MB now.

    So we have ended up with:
    01-2008.dmp, 02-2008.dmp, 03-2008.dmp, ... 05-2009, 06-2009.dmp, etc and seven daily dump files = Mon.dmp, Tue.dmp, Wed.dmp, ... Sat.dmp, Sun.dmp

    We used `date +%e` to get the day of today. We used `date +%0m` to get month and `date +%0y` to get year.
blog comments powered by Disqus