Home

Website Backups Part 2 - The Site

Here in Part 2, we'll cover syncing your web server directory to an Ubuntu computer.

Variables

I will use the following variables in my code snippets.  These are to be replaced with your specific information.

mysql_server - This is the server that your MySQL database is hosted, sometimes this is just localhost
mysql_username - This is the user name that connects to the MySQL server
mysql_password - This is the password for the MySQL user
mysql_database - This is the specific MySQL database, some sites may have more than one
webserver - This is the server that your website is hosted on
webserver_username - This is the user name required to connect to the web server
webserver_password - This is the password for the web user
local_username - This is the local computer user name

Server Login without Passwords

Since we want to automate our backups, we will not be there to enter in the password to gain access to our web server.  You could put your password into the scripts, but I would rather do a public-private key signing instead.

Start off by opening terminal on your computer and typing in the following command, and hit return until it's completed.

{codecitation}ssh-keygen -d{/codecitation}

Now, ssh into your web server, still providing your password.

{codecitation}ssh webserver_username@webserver{/codecitation}

Create a .ssh directory, set the permissions, and exit back to the local machine.

{codecitation}mkdir .ssh
chmod 700 .ssh
exit{/codecitation}

Now transfer the public key to the server.

{codecitation}scp /home/local_username/.ssh/id_dsa.pub webserver_username@webserver:.ssh/authorized_keys{/codecitation}

Once transferred, login again to the web server, set the key permissions and logout

{codecitation}ssh webserver_username@webserver{/codecitation}

{codecitation}chmod 600 .ssh/authorized_keys
exit{/codecitation}

Now verify you can connect to the web server without needing a password.

{codecitation}ssh webserver_username@webserver{/codecitation}

Backing up the Web Server

Let's create a place to put our website backup and start editing the script.  In terminal run the following

{codecitation}mkdir /home/local_username/backups
/home/local_username/backups/webserver
pico /home/local_username/backups/backup.sh{/codecitation}

Our script is very simple, and will be this

{codecitation}#!/bin/bash
rsync -avz -e ssh webserver_username@webserver:/home/webserver_username/ /home/local_username/backup/webserver{/codecitation}

And ctrl+o, enter and ctrl+x saves and exits pico.  Now we want the script to be executable, so we

{codecitation}chmod +x /home/local_username/backups/backup.sh{/codecitation}

Archiving the Backups

Now, we need to make sure we have some archives of our backup, since the above script will overwrite any file that's newer.  So, if a hacker messes up our index.php file and we don't catch it before the backup, we just lost our only good copy.  So, create an archives folder and launch pico

{codecitation}mkdir /home/local_username/archives
pico /home/local_username/archives/archive.sh{/codecitation}

Our script is very simple, and will be this

{codecitation}#!/bin/bash
cd /home/local_username/
suffix=$(date +%y%m%d)
tar -cf archives/archive.$suffix.tar backups/*{/codecitation}

And ctrl+o, enter and ctrl+x saves and exits pico.  Now we want the script to be executable, so we

{codecitation}chmod +x /home/local_username/archives/archive.sh{/codecitation}

And by running {codecitation}/home/local_username/archives/archive.sh
ls /home/local_username/archives/{/codecitation} we'll see an archive file with today's date.

Automating the Backups and Archives

I don't like terminal for everything, and cron jobs are one of those things.  So, I used Schedule Tasks gui.  You can find it in the Ubuntu Software center or install it via terminal by running {codecitation}sudo apt-get install gnome-schedule{/codecitation}

You'll find it under System Tools once it's installed, then it's a matter of creating a new recurrent task.  Giving it a description, telling it to run /home/local_username/backups/backup.sh or /home/local_username/archives/archive.sh and telling it how often to run.  The preview text at the bottom of the window will give you insight on when your script will run, then click add when you are finished.

Wrap Up

Now you can rest easier knowing that your websites are backed up.  You'll want to visit your website folder occasionally to clean up old MySQL backups as well as your Archives folder on your computer to clean out old archives as space is needed.