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
on Mon, 03/04/2013 - 06:49 by
eric
Comments
cool stuff
I like this blog. thanks
Add new comment