Lucky Backup with SSH Keys and Crontab
Share on Facebook

So, you want an automatic back-up

What follows is a procedure for backing up files from a Linux-based webserver to a home computer also running Linux - in this case, Ubuntu.

 

Backing files up is a pain if you have to do everything manually ... logging on to your server with an FTP client such as Filezilla, downloading the files and remembering to do it every day. Wouldn't it be better if everything were automated and, every day, at whatever time you specified, your precious files were copied from your server to a local computer - with no need for your intervention at all?

 

Well, it can be. And it's simple.

 

The first thing to do is to set things up so that you can make an SSH connection to your server without having to bother about pesky things like usernames and passwords.

 

To do this, we use a certificate. All you need to know about certificates is that they come in two parts: a Public Key, which lives on your server and a Private Key, which lives on your home computer. They work together to validate that you're allowed in and, once you've set up a key pairing, you won't need to bother with usernames and passwords again. This is useful; there would be no point in a back-up routine running every day and pausing while it waits for your input.

 

Creating a Certificate

No, don't run away with your fingers in your ears, this is very straight-forward. Open a terminal on your computer - in Ubuntu using the Gnome desktop, go to Applications > Accessories > Terminal. Then type the following:

 

ssh-keygen -t rsa [press Enter]

 

You will receive a response like this: Generating public/private rsa key pair.

 

When you are prompted to enter the filename, just press Enter. When you are asked for a passphrase, press Enter again and then Enter again to confirm it. In effect, you've created a certificate with no password. This isn't a security risk provided that you keep your home computer secure and don't let people fool around with it.

 

The two files which make up your certificate have now been saved in the /home/[yourname]/.ssh folder. The Private Key is called id_rsa and the Public Key is called id_rsa_pub.

 

All you need to do now is install the Public Key on your server and, again, this is very easy.

 

Install the Public Key on your server

First of all, locate the Public Key file on your computer. It will be in the /home/[yourname]/.ssh folder. The dot before the name tells you that this is a hidden folder so, in your file manager (Nautilus in Ubuntu) select the View option Show Hidden Files.

 

Now you know where it is, FTP the file to your server. It needs to go in a folder called .ssh and this will be in your home folder, along with other folders such as public_html. If the .ssh folder does not exist, create it. Having copied the file to your server, we now need to open an SSH connection to the server and this is so scary that you might want to leave now.

 

In a Terminal session on your computer type the following:

 

ssh [username]@[servername] [press Enter]

 

Where [username] is your user name and [servername] is the domain name, or IP address (either will work) of your server. You will be prompted for your password. Type it in and press Enter. If you've done this right, you now have a Terminal session open on your server. Type the following:

 

cd .ssh [press Enter]

cat id_rsa_pub >> authorized_keys [press Enter]

 

This will create a file called autorized_keys which will contain your authorised key.

 

That's it! The first part is done. You will now be able to log-in to your server, via SSH, without giving a username and password. Try it. Exit the SSH session by typing:

 

exit [press Enter]

Then re-connect by typing:

ssh [username]@[servername] [press Enter]

 

If everything has worked, you will be logged in.

 

Rolling it all together to make an automated back-up routine

This part will be written in a few days time ...

 

 

 

 

Primer, sealer, dust proofing coating
Larkrise Sealcote is a multi-purpose, clear,
water-based coating - Dilute 2:1 and solve a number of problems