Automatic database backups using cron, ssh and mysqldump

Here is a little bash script I wrote to do a daily backup of my databases.
This script will work unattended if you have ssh keys set up already so you won't be asked for credentials.

dbBackup.sh

#!/bin/bash

sshUser="ssh_username"
sshHost="ssh_host"

sqlUser="sql_username"    
sqlPass="msql_password"
sqlHost="database_host"

# Backup destination directory
DEST="/Users/ericpaige/db_backups"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# Store list of databases
DBS="mydatabase1 db2 other_database"

for db in $DBS
do

        FILE="$DEST/$db.$NOW.sql"
        ssh $sshUser@$sshHost mysqldump -u $sqlUser -h $sqlHost -p$sqlPass $db > $FILE

done

echo "Done"

 

The DBS variable is a list of database names separated by a space.
The FILE variable will look something like this: mydatabase1.03-03-2013.sql

You can set up a cron job to run every day at 12:30 am like this:

30 0 * * * /Users/ericpaige/db_backups/dbBackup.sh

Comments

I like this blog. thanks

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

Recent Posts

The talented Evan Coury created a module that allows you to specify alte

Here is a little bash script I wrote to do a daily backup of my database

This is my first attempt at using ZF2's new module system to use Zend's

Need Help?

Recent comments

Socialize with me

               


What am I doing now!






Google+ EricPaige on google+
Go to top