Daily database backups on dreamhost
Linux, MySQL, Programming, Web Development, dreamhost December 13th. 2007, 1:25pmTo do daily database backups, I use cron jobs. If you aren’t familiar with cron jobs, think of the as scripts that run on a timer. Much like ’scheduled tasks’ on windows. To edit your cron jobs on dreamhost, locate the ‘cron jobs‘ menu item located under ‘goodies‘ on the main menu:

Before we add a cron job though, I want to familiarize you with the script and have you run a test to save you a headache later.
#! /bin/sh # Daily backups on your database with email notification # Tyler Mulligan # file dateVar=$(date +%m-%d) # Date variable to append to filename (default: month-day | 11-24) savePath="/home/tyler/backups/" # Backups are stored here fileName="my_db_backup" # File name minus the date # email subject="My Database Backup ${dateVar}" email="db_robot@mydomain.com" # database username="root" # username password="" # password hostname="localhost" # hostname database="database" # database mysqldump -u $username -p$password -h $hostname $database | gzip > $savePath$fileName-$dateVar.sql.gz uuencode $savePath$fileName-$dateVar.sql.gz $fileName-$dateVar.sql.gz | mail -s "$subject" $email
I’ve set it up so you fill out your information like any other config file, the last two lines do all the work.
I suggest you create a new text file called ‘dbbackup.txt’, and paste the above code in and saving it for future reference. It’s always good to have a clean slate to start from. Once you have that done, fill in your file, email and database variables and give it a test run.
Create a shell script through ssh and run that.
Copy your personalized database dump code, log into your server and type:
vi my_db_test.sh [press i] right click (to paste the copied code) [press esc][type ":wq"] [press enter]
Your script is now written to a file called ‘my_db_test.sh’.
chmod +x the file and run it:
chmod +x my_db_test.sh ./my_db_test.sh
Once you receive the email, check your backup directory and verify that the database was properly dumped. If you don’t receive and email, you did something wrong.
If everything worked fine, return to the dreamhost cron job page, add a cron job and paste your working code into your newly created cron job.
Save, wait a day and you should receive your email. It’s a wise idea to grab a copy of your backup every week or so to store locally.
Dreamhost is actually offering a special for their 10th anniversary right now, 500gb disk space, 5TB transfer for only $5.95 a month. I’ve been with them 2 years and I’ve been pleased.


















