Clan x86

Technical (Development, Security, etc.) => Unix / Linux Discussion => Topic started by: chuck on December 31, 2006, 01:13:33 PM

Title: *NIX backup script
Post by: chuck on December 31, 2006, 01:13:33 PM
I wrote this yesterday and I use it from a remote machine to backup my mail and stuff.

First, to make the ssh login quick and easy:

ssh-keygen -t rsa
scp .ssh/id_rsa.pub root@MailServer.local:.ssh/authorized_keys


Then, I run:

mkdir ~/baks


And after that, I save this script somewhere and add it to my crontab:

#!/bin/sh
# Machine to login to. This script will do ssh $USER@$MACHINE
USER="root"
MACHINE="MailServer.local"

# Remote dir's to backup
LIST="/home /etc /var/lib/dpkg"

BACKDIR="$HOME/baks"

###############################
# No more configuration options
YEAR=`date "+%Y"`
MONTH=`date "+%B"`
DAY=`date "+%d"`

BACKDIR="$BACKDIR/$MACHINE/"

for d in $LIST; do
        if ! [ -d $BACKDIR ]; then
                mkdir $BACKDIR
        fi

        if ! [ -d $BACKDIR/$YEAR ]; then
                mkdir $BACKDIR/$YEAR
        fi

        if ! [ -d $BACKDIR/$YEAR/$MONTH ]; then
                mkdir $BACKDIR/$YEAR/$MONTH
        fi

        if ! [ -d $BACKDIR/$YEAR/$MONTH/$DAY ]; then
                mkdir $BACKDIR/$YEAR/$MONTH/$DAY
        fi

        FILENAME=`echo $d | sed 's/\///'`
        FILENAME=`echo $FILENAME | sed 's/\//./g'`

        ssh $USER@$MACHINE "tar zcpf - $d" | cat > $BACKDIR/$YEAR/$MONTH/$DAY/$FILENAME.tgz
done


When the script runs, it creates the directory's for the machine, year, month, and day. It then runs tar on the remote machine and cat's it to a tgz file.

Just posting this here so it may help someone :)