Bluehost Backup Script for Linux or Mac

If you use Bluehost (or any remote server), you will want to make periodic local backups of your remote web files and emails. The CP backup function stopped working for me. Tech support gave me a few canned responses and informed me that their backups are NOT GUARANTEED TO WORK, and did little to help with the problem.

So with the help of this guy’s blog, published in 2009, I wrote my own. It works great and gets all the files I need. It will only backup changed files on subsequent backups, so it doesn’t waste time/bandwidth!

1) Make sure SSH is enabled on your Bluehost account

I had to call tech support and send them a picture of my license to do this (a few years ago). Your username and password are the same as your account. You can make sure it’s enabled in your Bluehost CP -> SSH/Shell Access.

2) Install mysql if you have databases to back up.

On Linux the package is “mysql-client”. Macs should have a separate installer.

3) Create an SSH key

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:

~/.ssh/my_server

Enter a password for the key.

There will now be two new files in that location, one called “my_server” and the other “my_server.pub”. Copy the .pub one to your Bluehost account at this location, creating the folders as necessary:

/.ssh/my_server.pub

Go into your Bluehost CP -> “SSH/Shell Access”. Click “Manage SSH Keys”. Under “Public Keys” you should see your new file name. Click “Manage Authorization” and click “Authorize”.

Test the key. You may have to enter a password the first time, but it should be automatic after that. Type Ctrl+D to log out after you test it:

ssh username@domain.com

3) Allow remote MySQL backup

Go to Bluehost CP -> Remote MySQL. Add your IP number as a remote access host. It’s pretty easy as it fills it in for you.

While you’re at it, go to “MySQL Databases” and take note of the database names. You’ll need these later.

4) Get the script

Save this somewhere in your home directory and edit the variables to fit your domain, passwords, and databases. Then make the permissions 755 (rwxr-xr-x) on the script file.

#!/bin/sh

# Ben Holt’s modified Bluehost/remote server backup script
# ben@theholtsite.com
# Posted 4/15

bhaccountname=”bluehost_account_name”
sqllogin=”bluehost_mysql_login_name”
sqlpass=”bluehost_mysql_account_password”
yourdomain=”yourdomain.com”

#local file path for backup (must exist)
backups=”/home/yourname/Backups”

#databases to backup, names found in control panel
#just the suffixes of each name are needed
databases=”wrdp1 wrdp2 con1 con2″

options=”–archive –progress –stats –delete-excluded –compress”

#change the account name and domain below

rsync $options \
–exclude “.Junk/” –exclude “.spam/” –exclude “.Trash/” \
–rsh=”ssh -l $bhaccountname” yourdomain.com:public_html :mail $backups

for db in $databases
do
mysqldump -h ${yourdomain} -u ${sqllogin} -p${sqlpass} ${bhaccountname}_${db} > $backups/${db}.sql
done

exit

This script gets all your email, but excludes the Junk, Spam, and Trash folders to save a bit of space. It also gets your public_html folder, but nothing else. You can always include more folders by adding them into the rsync command after “:mail”, using a : before the folder name.

Enjoy!

Leave a Reply

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