Published in Work

Backing up site files and mySQL databases on Media Temple (gs) Grid Service

Not only have I moved blogs recently, I've also moved hosting companies. It's pretty straightforward to ensure you never lose any data from your Media Temple Grid Service...

I’m sure we’ve all been bitten by the lack of backups - or backups going wrong. I’m now paranoid about this so upon moving to Media Temple, I set about ensuring I never lost any of my data or files. It turned out to be pretty simple.

Let’s do step one first!

You need to ensure that SSH is enabled on your Media Temple account. You can do this in your Account Centre.

Windows or Mac?

Windows users will need to download a bit of software called “Putty” - it’s needed to do SSH from a Windows machine. Mac users can perform SSH “out of the box”.

Download Putty if you need to and read the Media Temple guide to using it with your account.

Backing up your web files

Once logged in via SSH, navigate to your /data directory. I’ve used ##### instead of my site number so replace the hashes with the number of your account (can be found in the Server Guide).


cd /home/#####/data

Now, create some directories:


mkdir sites-backup
chmod sites-backup

Create a file and make it executable:


touch sites-backup.sh
chmod +x sites-backup.sh

Open the file and paste in the following, replacing your domains and server number as before:


#!/bin/bash
today=$(date '+%d_%m_%y')
echo "* Performing domain backup…"
tar czf /home/#####/data/sites-backup/DOMAIN_"$today".tar.gz -C / home/#####/domains/DOMAIN
# Remove backups older than 7 days:
MaxFileAge=7
find /home/#####/data/sites-backup/ -name '*.gz' -type f -mtime +$MaxFileAge -exec rm -f {} \;
echo "* Backed up…"

This simple bit of script creates a tar.gz backup of the domain directory, stamps it with the date the script runs on and places it in the folder just created. It also runs through the directory and deletes any files it finds that are older than 7 days, meaning the backup folder is kept nice and clean and with a week’s worth of backups at any one time.

Run the script via SSH to test it - ./sites-backup.sh

Check that a backup file has been created. If it has, you can now setup the script as a cron job within the Media Temple Account Centre. There is a knowledge base article here.

So, what about mySQL files then?

Oddly enough, it’s a very similar process so I’m not going to go through it blow by blow, except to say that you can create a folder and a file again (mine are called “db-backups” and “db-backup.sh” respectively). The backup file needs to look like this:


#!/bin/sh
#############################
SQLHOST="internal-db.s#####.gridserver.com"
SQLDB="db#####_NAME-OF-DATABASE"
SQLUSER="USERNAME"
SQLPASS="PASSWORD"
SQLFILE="DATABASENAME_$(date '+%d_%m_%y').sql"
LOCALBACKUPDIR="/home/#####/data/db-backup"
#############################
echo "* Performing SQL dump…"
cd $LOCALBACKUPDIR
mysqldump -h $SQLHOST --add-drop-table --user="$SQLUSER" --password="$SQLPASS" $SQLDB > $SQLFILE
# Remove backups older than 7 days:
MaxFileAge=7
find /home/90929/data/db-backup/ -name '*.sql' -type f -mtime +$MaxFileAge -exec rm -f {} \;
echo "* Backed up…"
exit

This creates a mySQL backup file and also removes any that are older than 7 days.

Simple eh?

FTP the files back to your local server

I have set up an FTP job which downloads the files daily to my server and then and puts them into a folder which is then backed up on my back up server - as well as backing them up online using my online backup service. Paranoid? Maybe!

There are a number of ways of doing this - I’m using Windows7 at the moment so I’ve written an FTP batch file which is then set up as a scheduled task. Dead easy.

If anybody wants to see the file, just drop me an email and I’ll send it along.

Equally if anybody has any questions, drop me a mail and I’ll help you where I can - or if there is a better way then please let me know too! Hope this helps someone…