Hosting: Backing up your MediaTemple using rsync and a linux box

Follow these steps to setup a decent backup solution for your MT hosting (you can adapt this to any other hosting that offers SSH and RSYNC access).

First you'll want your linux box to be able to SSH into your hosting without a password using a SSH key instead.

Then, just clone the following repository somewhere in your linux box

https://github.com/PortNumber53/mt-utils

Customize the file:

mt.sh.sample

Next, make sure the variables in

0-variables.sh

match your enviroment.

Then you just need to run:

mt.sh.sample

For extra peace of mind, set it to run from a cron job.

 

 

Linux:: SSH configuration file

To save some typing you can create aliases to the servers you frequently access via ssh. This can be accomplished by either editing the global file ( /etc/ssh/ssh_config ), if you’re root and and to make changes for all the users, or your own config file ( $HOME/.ssh/config ).

Follow the example, and remember to customize to your needs

Host *
Compression yes
Port 22
ForwardAgent yes

Host sample
HostName host.sample_of_a_very_long_host_name.com
User joe
ForwardAgent yes
Port 22
Compression yes

Now you can ssh into that server using:

ssh sample

Linux:: settings shortcuts for your ssh connections to hosts

If you wish you could use friendly names to access your servers, here’s how to do it:

Edit your ~/.ssh/config and add the following:

Host myserver
    HostName 123.123.123.123
    Port 12345
    User bob
Host other_server
    HostName test.something.org
    User alice
    CheckHostIP no
    Cipher blowfish

Please, remember to customize "HostName" and "User" values to your needs. 🙂

Use an alias to SSH to a server

Create a file ~/.ssh/config and insert the following:

mauricio@rynex:~$ cat .ssh/config
Host SERVER_ALIAS
Port PORT
HostName SERVER_NAME
User USERNAME
Compression yes
TCPKeepAlive yes

Change according to your servername, username and port, of course.

Then you can simply use:

ssh SERVER_ALIAS

to connect to the server instead of using:

ssh username@hostname

Setting up Slackware 13 (64-bit) (part 3)

Make your server trust your laptop+user:

#su - "your username here"

$ssh-keygen -t rsa

$scp ~/.ssh/id_rsa.pub "remote_username"@"host_name":"target_filepath_for_key_on_the_server"

Now ssh into your server:

$ssh "remote_user"@"host_name"

$cd ~/.ssh

Insert your laptop’s public key in the authorized keys list:

$cat "target_filepath_for_key_on_the_server" >> authorized_keys

Try ssh-ing your server again (this time you should not need to type a password:

$ssh "remote_user"@"host_name"

Create an alias for your server:

#nano /etc/ssh/ssh_config

and add this  (customize it, please):

Host linode
HostName portnumber53.com
KeepAlive yes
User httpd
Compression yes

Now you should be able to ssh into your server like this:

$ssh linode

Now use this nice backup script:

#!/bin/bash
pid=/tmp/linode.pid
TODAY_FOLDER=/home/backup/`date +%Y-%m-%d --date "0 day ago"`/linode
YESTERDAY_FOLDER=/home/backup/`date +%Y-%m-%d --date "1 day ago"`/linode

if [ -e $pid ]
then
echo "Still running..."
else
touch $pid
if [ ! -d $TODAY_FOLDER ]; then
mkdir -p $TODAY_FOLDER
echo "copying..."
cp -alv $YESTERDAY_FOLDER/. $TODAY_FOLDER
fi

rsync --bwlimit=100 --force --no-p --progress --delete-before -aze ssh linode:~/ $TODAY_FOLDER
rm $pid
fi

and you will have full backups (while preserving spaces as the cp command creates hardlinks) daily

Making your server trust your laptop

To avoid copying and pasting my password (mostly in the wrong window), I thought it would be better to make my server trust my laptop by using public/private keys:
Here we go:

For Linux (or BSD, or OSX):

1. Open a terminal on your laptop and generate your keys:

ssh-keygen -t rsa

You may optionally choose a passphrase (which I didn’t since the I want to avoid typing).

2. Copy the public key to your server:

scp ~/.ssh/id_rsa.pub username@server_address:/your/home/folder/

3. SSH into your server and add your public key to the authorized_keys file:

cd ~/.ssh
echo "#Something to identify your key" >> authorized_keys
cat /your/home/folder/id_rsa.pub >> authorized_keys

4. Open another terminal and try logging to your server just providing your username:

ssh username@server_address

You should be granted with the shell command line from your server. If not, please review what you did. If still doesn’t work, drop me a line:

  • IGlr