Simple mysqldump backup script

**UPDATED 02/24/2012**

I finally got around to changing up my script and decided to change a few things that would essentially make more sense, the only main difference that I am doing is in the find command I am using better options to cycle the current SQL files.  My old version did not do what I planned.  Here is the updated script

#!/bin/bash
DATE=`date '+%Y-%m-%d_%I:%M%p'`
MYSQLUSER=backup
MYSQLPW=4RMDuUGgTSd9MvZS
MYSQLDB=ka_wordpress
DEST="/home/spellnight/backup"
FILES=$DEST/"$DATE"_$MYSQLDB.sql

## Check for files older than 10 days, if files are older than 10 days then delete$
find $DEST -mtime -10 -type f -exec rm {} \;

## Check to see if a file already exist
if [ -e "$FILES" ]
        then
                echo "$FILES already exists, exiting."
                exit
else
        echo "Creating backup database."
## If you want to back up all databases use --all-databases
        mysqldump -u $MYSQLUSER -p$MYSQLPW $MYSQLDB > $FILES
        echo "Database created!"
fi

**END UPDATE FROM 02/24/2012**

This is a simple mysqldump backup script that I made to automate the process to backup my database for WordPress.  It's pretty simple and does what it needs to do, for now I will leave it a this because I have more projects in the bucket list but I think this will help some out.

Where MYSQLUESR, MYSQLPW, and MYSQLDB is where you put your information in for accessing the database and DEST is where you want the databases to be stored, of course I created a user that just had enough privileges for mysqldump which is SELECT and LOCK TABLES on my database.


You could also put –all-databases inside MSQLDB to back up all databases.

#!/bin/bash
DATE=`date '+%Y-%m-%d_%I:%M%p'`
MYSQLUSER=username
MYSQLPW=password
MYSQLDB=database
DEST="/path/to/backup"
FILES=$DEST/"$DATE"_$MYSQLDB.sql
## Check for files older than 10 days and delete them.
find $DEST/*.sql -mtime +10 -exec rm {} \;

## Check to see if a file already exist
if [ -e "$FILES" ]
        then
                echo "$FILES already exists, exiting."
                exit
else
        echo "Creating backup database."
## If you want to back up all databases use --all-databases
        mysqldump -u $MYSQLUSER -p$MYSQLPW $MYSQLDB > $FILES
        echo "Database created!"
fi
Tagged , , , . Bookmark the permalink.

One Response to Simple mysqldump backup script

  1. Pingback: Simple mysqldump backup script revisited - Karl Ahlers

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.