Simple Linux Backup with SCP and TAR

Added on by Ray Insalaco.
I ran into a problem this week when I tried to pull a file off of a backup and found out that I could not read any of the backup tapes that I had. I was using Dump to create the backups and when I tried to access them with Restore I got the error message that the tape was not a dump tape. I had tested this before I put the server into place, but something has changed in the last two months. I decided to change to a different method of creating backups. I am now going to tar the server and then use scp to move the tar file to a second server that is located off site.

Lets start with setting the machines up so that we can transfer files between the two servers without being prompted for a password. For this posting server1 will be the server that is being backed up and server2 will be the server that we are sending the backup to.

Configure the Client (server1)
Create the client key without a password:
[root@server1 /]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key [/root/.ssh/id_dsa]:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa
Your public key has been saved in /root/.ssh/
The key fingerprint is 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:root@server1

Lets take a look at the key:
[root@server1 /]# cd ~/.shh
[root@server1 /]# ls
id_dsa known_hosts

Copy the key to the server that we are going to copy the backup to:
[root@server1 /]# scp -r

Configure the server (server2)
Lets make a .ssh directory if the directory does not exist:
[root@server2:~#] mkdir .ssh
[root@server2:~#] chmod 700 .ssh
[root@server2:~#] cd .ssh

Add the key for the client to the authorized keys:
[root@server2:~#] cat ~/ >> authorized_keys
[root@server2:~#] rm ~/

You should now be able to use ssh and scp from serve1 to server2 without being prompted for a password.

From here I had to create the script for creating the tar file of server1 and then copying it to server2. I created a directory called backup to hold my script and the MySQL backups. So on server1 we are going to create the directory and the script:
[root@server1: /#] cd /
[root@server1: /#] mkdir /backup
[root@server1: /#] cd /backup
[root@server1: /#] nano

Now the script:

# Define the variables for later use:
BACKUPFILE=backup-$(date +%Y%m%d)
OLDFILE=backup-$(date --date='7 day ago' +%Y%m%d) # We are keeping 7 days of databases dumps.
EXCLUDES="--exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/media --exclude=$BACKUPFILE.tgz"

# Backup the MySQL Databases:
mysqldump -uroot --opt databasename1 > /backup/databasename1-$BACKUPFILE.sql
mysqldump -uroot --opt databasename2 > /backup/databasename2-$BACKUPFILE.sql

# Create the tar file:

# Upload the tar file:

# Remove the backup file:
rm /backup/databasename1-$OLDFILE.sql
rm /backup/databasename2-$OLDFILE.sql

You should now be able to backup server1 with the command:
[root@server1: /#] sh /backup/

Just want to say thanks to David for his demo at the last ECLUG meeting, because it gave me the point to jump off at.