My Backup system at home:


Since getting more and more settings, setups and server/machines all over the house and other places, I decided to build me a BackupServer, and started out thinking about what to use to that. And ended up with a Linksys NSLU2.

Here's the little machine.
Install instructions can be find here at the danish linuxsite(on danish).

You can allso use it for ?? MythBackend-server - BUT that sounds a little funny to me. but my plan was having this little machine to make a backup ogf all of my servers and setups.

Configurationsfiles, MySQL dumps etc will afterwards be placed here on the NSLU2.

nslu2.jpg

But first of all - we need to make sure that some requirenments are fullfilled.Starting with a setup of the SSH-keys, which brings us down the level, where a specific machine are allowed to login without password. So let's start out with that problem:

$ ssh-keygen -t dsa

This prompts you for a password , if you uses a keyring or anything like that - you can add a password - BUT if you want automatic login (without prompt for passwd) you should just press ENTER twice(no password).
This command generate 2files id_dsa & id_dsa.pub in your .ssh/ dir. Now you sjould copy id_dsa.pub into a file on the remote machine with this command:

$ scp .ssh/id_dsa.pub IPADDRES:.ssh/authorized_keys

enter the password - and the copy are placed on the remote machine. Now try login to the same machine again, and you will not be prompted at loginpassword now. This will normally overwrite the file uthorized_keys (if any hosts sere added in there before this)

The BackupScript:

For getting the backup working I've made som changes, since I have my network through 3different subnet's without direct access through the subnets. The next problem is gaining root access for the backup user - for making the different backups and securering the data. - But ended up with this script:

#!/bin/bash
#
# This script is made by Per Jørgensen for backing up my
# Configuration,files and music from the different servers
# etc in my network setup

# Define the different variables
# Machines
FIREWALL="IP-address"
WEBSERVER="IP-address"
CORE="IP-address"
EXTRA="IP-address"

# Logfile definations
LOGFILE="/var/log/backup.log"
TMP="/tmp/dumplog"

# Commands used during Backup
RSYNC="rsync -avz -e ssh"

echo "`date +%R:%S `: Starting up the backup process." > $LOGFILE
echo "-------------------------------" >> $LOGFILE
echo "The following variables are used:" >> $LOGFILE
echo "Firewall: $FIREWALL" >> $LOGFILE
echo "Core: $CORE" >> $LOGFILE
echo "Atlantis: $ATLANTIS" >> $LOGFILE
echo "Zone: $ZONE" >> $LOGFILE
echo " " >> $LOGFILE
echo "The logfile: $LOGFILE" >> $LOGFILE
echo " " >> $LOGFILE
echo "This script is made by PBJ 2008" >> $LOGFILE
echo "The script is licensed under GNU" >> $LOGFILE
echo " " >> $LOGFILE
echo "Remember - OpenSource is not nessecary free" >> $LOGFILE

# Starting the Backup (Remember to use SSH-keys)
# Otherwise this won't work with auto login.

# Firewall / DNs / Nagios etc

echo "Backing up the needed configurations at Firewall." >> $LOGFILE
$RSYNC $FIREWALL:/home/peque/backup/* /storage/firewall/ >> $LOGFILE
echo "Finished with Firewall Machine" >> $LOGFILE
echo "Deleting the tempfiles on Firewall" >> $LOGFILE
ssh $FIREWALL rm -rf /home/peque/backup >>$LOGFILE

echo " " >> $LOGFILE
echo " " >> $LOGFILE

# Web / mail / Wiki

echo "Backing up the needed configurations on Webserver." >> $LOGFILE
$RSYNC $WEBSERVER:/home/pbj/backup/* /storage/webserver/ >> $LOGFILE
echo "Finished with Webserver Machine" >> $LOGFILE
echo "Deleting the tempfiles on Webserver" >> $LOGFILE
ssh $FIREWALL rm -rf /home/pbj/backup >>$LOGFILE
echo " " >> $LOGFILE
echo " " >> $LOGFILE

# MythTV - MasterBackend Server

echo "Backing up CORE - MythTV server." >> $LOGFILE
echo "Starting with backing up the MySQL database." >> $LOGFILE
$RSYNC $CORE:/tmp/mysql_backup.sql /storage/core >> $LOGFILE
echo "Backing up the Music directory:" >> $LOGFILE
$RSYNC $CORE:/storage/music /storage/core >> $LOGFILE
echo "Backing up Open-Sasc-NG keys: /home/myth/keys " >> $LOGFILE
$RSYNC $CORE:/home/myth/keys /storage/core >> $LOGFILE
echo "Finished with CORE MythTV server." >> $LOGFILE
echo " " >> $LOGFILE
echo " " >> $LOGFILE

# EXTRA

echo "Backing up Extra server." >> $LOGFILE
"ADD YOUR OWN SCRIPTS HERE "
echo "Finished with Extra server." >> $LOGFILE
echo " " >> $LOGFILE
echo " " >> $LOGFILE

# Sending the details in a mail!

echo "Ended backup procedure..." >> $LOGFILE
echo "Sending the mail with the info from the backup process." >> $LOGFILE
echo "`date +%R:%S `: Backup process ended."
echo "Backup Process at Backy:" >> $TMP
cat /var/log/backup.log > $TMP

mail -s 'Daily Backup Backy' "Your emailaddress < $TMP

rm -rf /var/log/backup.log
rm -rf $TMP

# EOF

This script is placed on the NSLU2(called Backy) and run each nigth at 2AM !

Firewall:

So as you can see - we need som different requirements for this script, so starting with Firewall, so start the backupscript with the requirements needed for running the main backupscript. The preparebackupscript is runned each nigth at 01AM on Aladin (Firewall).

#!/bin/bash
#
# Backupscript for Aladin Firewall,DNS and DHCP server
# Move all important data to /home/peque/backup and
# Another server taking over from there.

# Copying the motd
cp /etc/motd /home/peque/backup

# Copying the Firewall script
cp /usr/local/sbin/pbj-iptables.sh /home/peque/backup

# Copying the DHCP configuration
cp /etc/dhcpd.conf /home/peque/backup

# Copying the DNS directory
rsync -rvz /etc/bind /home/peque/backup

# Copying Nagios directory
rsync -rvz /usr/local/nagios /home/peque/backup

# Ending the backup by changing the owner
chown -R peque.users /home/peque/backup

So all in all - the backupscript is making the directory, which placed everything for backup - in the dir /home/peque/backup. Afterwards the backupscript on the NSLU2 rsyncs the dir to it self, and afterwards deletes the dir, so it's ready for the next nigth.

Webserver:

The same procedure is runned on the Webserver - at little script on the local server prepares the data for backup. So the script on Atlantis looks like this:

#!/bin/bash
# This script is made for backing up the different
# things on this computer - like websites, mail &
# databases.
# The script is made by Per Jørgensen 20080905
#
# Variables
BACKDIR="/home/pbj/backup"
CONFDIR="/home/pbj/backup/config"
DBDIR="/home/pbj/backup/database"

# Starting scripting
mkdir -p $CONFDIR
mkdir -p $DBDIR
chmod -R 777 $DBDIR

# Starting with copying the motd
cp /etc/motd $BACKDIR

# Rsyncing /var/www
rsync -avz /var/www $BACKDIR
# Rsyncing apacheconf
rsync -avz /etc/apache $CONFDIR

# Rsyncing maildir from postfix
rsync -avz /usr/local/virtual $BACKDIR
# Rsyncing postfix configuration
rsync -avz /etc/postfix $CONFDIR
# Rsyncing squirrelmail
rsync -avz /etc/squirrelmail $CONFDIR

# Backing up the PostgreSQL database
su postgres -c "pg_dumpall > $DBDIR/postgresql.sql"

# Backing up the MySQL database
mysqldump -h localhost -p -A > /home/pbj/backup/database/mysql.sql

# Ending the script by changing the ownership
chown -R pbj.users $BACKDIR

This script is starting at 01AM, and afterwards just waiting for the Backy server to connect and syncronize before deleting the files again!

Those 2 servers where the diffucult ones, since I could not hit those machines directky carse of the network setup.

CORE MythTV Backendserver:

Well - there isn't much to say about this one - carse they're on the same subnet, and direct contact is possible, so just syncronize whatever you want from the CORE server on to the NSLU2.

Afterwards you have your own Backup-system, running automaticly each nigth and even confirms it to you in a mail from the system aboput how it went!

 

Good luck building your own Backkup-system!