Automate backups using SSH and Key Authentication on Bluehost

Millions of websites are hosted on Bluehost, probably the biggest provider of shared hosting. One issue is that they do not guarantee backups of your data and anyway my hosting accounts always get too big for them to backup anyway.

Since Sunday, I have had a real scare that the server was down for about 24 hours and I didn’t know if I would have any data to come back to. Luckily, last week I made a backup of everything including the databases, so I would have been OK, but it was the first time I had backed up in 3 months so I was lucky.

Time to set up an automatic backup program!

The issue is that every time you run a backup, you need to enter the servers password which means that it is difficult to automate. To get around this you need to use RSA key authentication. Unfortunately, that is easier said than done and it’s difficult to find out how to set this up on Bluehost. I spent hours learning how, so I thought I would show here how you can set up automatic backups in less than 5 mins…

Setting up your keys:
(First make sure SSH is activated on your server, you can do this through CPanel on Bluehost).

On your client machine (assuming you are backing up to a linux box):

Open a terminal and type…
ssh-keygen -t rsa

It then asks you to enter a file in which to save the key. Just type in the filepath you want to use, I would suggest this: “/home/YOURLINUXMACHINESNAME/.ssh/my_server
Press enter.

It then asks you for a password for that key. As you want to connect automatically, just press enter twice to accept no password.
You will get some feedback that the key was successfully generated.

Now, in the terminal again type:
sudo gedit /home/YOURLINUXMACHINESNAME/.ssh/my_server.pub

Enter your linuxbox password and you will see a text editor with the full key!

Now, go over to Bluehost and use file manager to go into the root of your account. Create a folder and call it “.ssh” (with the dot).

Now in that folder create a file called “authorized_keys”. Edit that file and paste in the full key that you created from the client. Copy in the full file including any comments etc. Save the file on Bluehost.

That should be you good to go!

Test

Do a test by opening a terminal on your client machine and type:
ssh username@domain.com

See the command prompt for your server without being asked for your password? Congrats!

Download your files and backup your databases!

All you need now is a good backup program that will even backup your databases. Even better if it only downloads the things that have changed on your website instead of downloading all 100Gb of junk you have on there every time!

Try this one for size! (save it as a .sh and enable it to be run as an executable)
Remember and add your client IP address to the MySQL whitelist in Bluehost in order to back up your databases.

#!/bin/sh

### Edit Me ###
yourlogin=”somebody”
yourpass=”secret”
yourdomain=”example.com”
backups=”/mnt/somewhere/”
databases=”drpl1 copp1″
### Edit Me ###

old_cd=”$(pwd)”

cd “${backups}”
rsync -aHvcz –delete –progress -e ssh ${yourlogin}@${yourdomain}:public_html . # <– note the trailing dot “.”

for db in $databases
do
mysqldump -h ${yourdomain} -u ${yourlogin} -p${yourpass} ${yourlogin}_${db} >${db}.sql
done

cd “${old_cd}”
exit 0
######

Thanks to Homer on the Bluehost forum for this script.

Now you just have to set up a cron task to run this script once a day, or once a week or however often you want.

This took me a while to work out, so I hope it helps some others get there quicker. Once it’s up and running, it really works great!

This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to Automate backups using SSH and Key Authentication on Bluehost

  1. neil says:

    Thanks, I’ll sleep better tonight.

  2. Gordon says:

    Dr Mac is there a hint of sarcasm in your reply?????

  3. Graeme says:

    Gary, I was with you up to ‘Millions…’

  4. neil says:

    Why no, Gordon. Moi? Sarcasm?
    Afraid I don’t know the meaning of the word.

    Graeme, does that include the article headline?, if so you got 7 words futher than me.

  5. neil says:

    P.S. Anyone noticed how close the minileague is getting, there are only 11277 positions between first and last:

    POS NAME POINTS
    426. stideon 49
    699. gudgeb 47
    1256. frazman78 45
    3065. rubbish_runner 40
    9011. craigyule 31
    11703. GaryfraetheDale 25

  6. neil says:

    Good news Gary,

    The BBC website is malfunctioning, and you have received a temporary boost – joint 0th position!

    westillhatedufc minileague
    POS NAME POINTS
    0. GaryfraetheDale 0
    0. stideon 0
    0. rubbish_runner 0
    0. gudgeb 0
    0. craigyule 0
    0. frazman78 0

Leave a Reply

Your email address will not be published. Required fields are marked *