Author Topic: *NIX backup script  (Read 2971 times)

0 Members and 1 Guest are viewing this topic.

Offline chuck

  • Full Member
  • ***
  • Posts: 335
  • Canadian Biathlete
    • View Profile
    • Chucks Blog
*NIX backup script
« 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:
Code: [Select]
ssh-keygen -t rsa
scp .ssh/id_rsa.pub root@MailServer.local:.ssh/authorized_keys

Then, I run:
Code: [Select]
mkdir ~/baks

And after that, I save this script somewhere and add it to my crontab:
Code: [Select]
#!/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 :)
Chucks Blog
JavaOp2 Plugins

Quote
Error, keyboard not connected. Press F1 to continue.