Tuesday, February 1, 2011

BASH : Backup Files

pico backupscripts.sh
#!/bin/bash
#Author
#Date
#Purpose - back up files and directories locally and store remotelly
BACKUPDIR=~/backup
SCRIPTDIR=~/temp2
BACKUPFILE=scripts.backup.`date +%F` # get the current date
tar -czvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR
#END
chmod u+x backupscripts.sh
mkdir ~/backup
./backupscripts.sh


#!/bin/bash
BACKUPDIR=~/backup
SCRIPTDIR=~/temp2
BACKUPFILE=scripts.backup.`date +%F` # get the current date
THRESHOLD=7
COUNT=`ls $BACKUPDIR/scripts.* | wc -l` #number of file from dir
if [ $COUNT -le $THRESHOULD]
then
tar -czvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR
fi
#END
./backupscripts.sh
#!/bin/bash
BACKUPDIR=~/backup
SCRIPTDIR=~/temp2
BACKUPFILE=scripts.backup.`date +%F`.tgz # get the current date
BACKUPHOST=192.168.1.10
THRESHOLD=7
if [ ! -e $BACKUPDIR ]
then
echo Create backup dir
mkdir ~/backup
COUNT=0
else
COUNT=`ls $BACKUPDIR/scripts.* | wc -l` #number of file from dir
fi


if [ $COUNT -le $THRESHOULD]
then
tar -czvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR > /dev/null # in order not to see the output
if [ $? != 0 ]; then echo Problem s creating backup files;fi
scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST:
if [ $? != 0 ]; then echo Problem copying backup file to backup host; fi
fi
#END
./backupscripts.sh


cat id_rsa.pub -> get the public key
ssh 192.168.1.10
cd .ssh/
nano authorized_keys -> paste the public key
exit
ssh 192.168.1.10 -> the password is not required any more
./backupscripts.sh -> in this way everything works smothly


#!/bin/bash
BACKUPDIR=~/backup
SCRIPTDIR=~/temp2
BACKUPFILE=scripts.backup.`date +%F`.tgz # get the current date
BACKUPHOST=192.168.1.10
THRESHOLD=7


function checkbackupdir()
{
if [ ! -e $BACKUPDIR ]
then
echo Create backup dir
mkdir ~/backup
COUNT=0
else
COUNT=`ls $BACKUPDIR/scripts.* | wc -l` #number of file from dir
fi
}


function backup(){
if [ $COUNT -le $THRESHOULD]
then
tar -czvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR > /dev/null # in order not to see the output
if [ $? != 0 ]; then echo Problem s creating backup files;fi
scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST:
if [ $? != 0 ]; then echo Problem copying backup file to backup host; fi
fi
}
checkbackupdir
backup
#END


#!/bin/bash 
BACKUPDIR=~/backup
SCRIPTDIR= "/temp2 /etc" #back up multiple folders
BACKUPFILE=scripts.backup.`date +%F`.tgz # get the current date
BACKUPHOST=192.168.1.10
THRESHOLD=7


function checkbackupdir()
{
if [ ! -e $BACKUPDIR ]
then
echo Create backup dir
mkdir ~/backup
COUNT=0
else
COUNT=`ls $BACKUPDIR/scripts.* | wc -l` #number of file from dir
fi
}


function backup(){
if [ $COUNT -le $THRESHOULD]
then
tar -czvf $BACKUPDIR/$BACKUPFILE $SCRIPTDIR # in order not to see the output
if [ $? != 0 ]; then echo Problem s creating backup files;fi
scp $BACKUPDIR/$BACKUPFILE $BACKUPHOST:
if [ $? != 0 ]; then echo Problem copying backup file to backup host; fi
fi
}
checkbackupdir
backup
#END


ls -lh ~/backup/ -> shows the dimension of the files from folder in MB

No comments:

Post a Comment