In part one of this tutorial, Automating Your Server Backups On Media Temple’s Grid-Server, I explained how to automate backups of your files on your grid-server. Unfortunately, not all your data was covered in those backups because it did not include your database. Now I will describe how you can utilize cron and mysqldump to automate database backups on the grid-server side.

The Process

Like the last tutorial, we will go about this in a similar fashion by creating a script which we will utilize as a cron job.

  1. Open up your favorite text-editor and copy and paste the following snippet:
    #!/bin/bash
    mysqldump –add-drop-table -h internal-db.s99999.gridserver.com -udb99999 -pPASSWORD database_name > database_name.sql

    We will need to replace a few things here though:

    • Replace all the instances of ‘99999′ with your gridserver account number.
    • Replace ‘PASSWORD’ with your SSH password
    • Replace ‘database_name’ with the name of the database you’d like to backup

    If you’ve ever setup Wordpress or some other CMS which needs a database, you’ve likely come across this info before.

    You could test if your command works by first SSHing in and then entering in the command. If an sql file is created, it worked.

    If you have more than one database you want to backup, just create another mysqldump command.

  2. Now save this file as something like ‘db-backup.sh’
  3. Upload ‘db-backup.sh’ to your /data folder
    I chose to put the script in this folder because when it runs, the .sql files are also dumped into this folder. This folder is nice because it is only accessible by you and you wouldn’t want the public to accidentally stumble upon an entire dump of your database.
  4. Now log into your Media Temple Account Center
  5. Go to the admin section of your primary domain and click the Cron Jobs link
    Cron Jobs Link
  6. Click on the ‘Add a new cron job’ button
    Add new cron job
  7. Feel free to fill in your email address in the output email field if you’d like. This will send you emails whenever the cron job runs and will let you know if it was successful or not. However, the important field we need to fill out is Command to Run. In this field, enter:
    /home/99999/data/db-backup.sh

    Again, replace the ‘99999′ with your grid-server account number (it’s actually listed below ‘Command to Run’)

  8. In the bottom half of the page, you can specify how often you’d like your script to run. This entire section is up to you. I’ve set mine to run daily at 2AM when traffic on my site is slower
  9. Click Save and you’re all done!

Now what about saving these mysql dumps to my local disk?

If you’ve went through part one of this tutorial and successfully set up rsync, these dumps are now part of that synchronization. :)

Again, if something didn’t make sense or you know of some way to improve these backup processes, please feel free to leave a comment!


Possibly Related


15 Comments so far... perhaps you would like to leave one?

Thanks for this article, it is very useful!

Comment by Billybob — May 29, 2008 @ 9:06 pm

Thankyou so much for this post, EXACTLY what I’ve been looking for!

Comment by Ben — June 9, 2008 @ 6:33 pm

Glad that you guys found it useful :)

Comment by Sam Lu — June 10, 2008 @ 11:29 am

Can’t possibly thank you enough for this script. Searched for hours compiling bits from here and there, and this was by far the most useful.

I believe the problem with the other scripts is that I needed to define the password and username in the manner that you use in order to call the mysqldump function from a php file.

If anyone else has the (dv) dedicated virtual server, and you’re attempting to run a nightly data backup through a cron job executing a php script, here is what works for me (make sure you set the 3 constants DB_USER, DB_PASSWORD, & DB_NAME, and change the $backupfile path:

// Creates a nightly backup of the db,
// One week of backups are maintained
function nightlyBackup(){
$backupfile = ‘../backups/’.date(’l').’.sql’;

// Since the mysqldump script doesn’t overwrite, we must delete the old backup
if(file_exists($backupfile)){
unlink($backupfile);
}

$command = “mysqldump -u”.DB_USER.” -p”.DB_PASSWORD.” “.DB_NAME.” > $backupfile”;
system($command);
}
nightlyBackup();

Thanks again!
Tison

Comment by Tison — June 12, 2008 @ 2:06 pm

Confirmation email states:
/home/xxxxx/data/db-backup.sh: line 2: dbxxxxx_db_country_areas.sql: Permission denied
any idea? Worked fine with ssh as it created .sql in my user directory.

Comment by Daniel — July 30, 2008 @ 6:05 am

Daniel: I never got that before. Have you SSH’d again and tried doing the dump with the file existing already? I’d try to see if the “Permission denied” error really means that the file can’t be overwritten.

Comment by Sam Lu — July 30, 2008 @ 9:49 am

Sam, If I ssh and run the file it creates the .sql files fine, and if I do it again is does overwrites them. But as a cron job it gives permission denied. Is the file run as a different user by the cron job as opposed to me via ssh?

Comment by Daniel — August 7, 2008 @ 5:36 am

I was also thinking along those lines Daniel. I can’t help out any further but I’m sure the folks at Media Temple (that is, if they are your host) can help you look into this as it most likely is a user permission issue.

Comment by Sam Lu — August 7, 2008 @ 11:04 am

Hey! Ihavn’t tried this, but can you explain me if there is a way to do this for multiple databases? Like, run the command several times to produce several backups? I have 13 DB’s on my grid-server and it’s a pain to manually backup everyone, everytime!
Thank for the help! ;)

Comment by Hugo Baeta — August 13, 2008 @ 4:34 am

Hugo, if you follow the steps above but just insert additional commands of the following in your script for each database, it will create dumps of all your databases when it runs!

mysqldump –add-drop-table -h internal-db.s99999.gridserver.com -udb99999 -pPASSWORD database_name > database_name.sql

Be sure to review step one to see which parts of this command you’ll need to modify.

Comment by Sam Lu — August 17, 2008 @ 1:02 pm

Sam,

Does this script overwrite the files everytime it is run?

mysqldump –add-drop-table -h internal-db.s99999.gridserver.com -udb99999 -pPASSWORD database_name > database_name.sql

Thanks!
Michael

Comment by Michael York — August 23, 2008 @ 11:14 pm

Michael, I did find that the script did overwrite the existing sql file when it was run. If it doesn’t, you might experience something like what Daniel experienced where report emails would be sent saying that there was a permission issue.

Comment by Sam Lu — August 24, 2008 @ 5:37 pm

for the permission denied, make sure you are using absolute paths…

Comment by Luca F. — October 12, 2008 @ 2:02 am

Sam Lu…Thanks for the tip. I had the same issue and the abs path fixed it!

Comment by EtherBunny — November 11, 2008 @ 12:42 pm

@EtherBunny I can’t take credit for the tip, Luca F was the one who actually said it ;)

Comment by Sam Lu — November 11, 2008 @ 9:26 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>