LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-03-2003, 07:00 PM   #1
turnip
Member
 
Registered: Jul 2002
Posts: 143

Rep: Reputation: 15
bash shell backup script


So, I wrote this backup script. and kept adding things as I went got into it.

I guess I'm looking for some constructive criticism, thoughts, comments, or whatever from the programmers here.

My goal now is to make it usable on Linux and FreeBSD. I run FreeBSD, so it works just fine there.

A quick run down..

The script runs dump, on 3 partitions.
It starts with a level 0 dump and and one a day increments up one. Until 9, then it goes back to 0.

It also appends the current date to the file name. And moves it to a NAS server.

It then checks the files on the NAS server by date and starts removing them if they get too old.

It also does a mysql dump. And tar for web directories (I want to keep these longer).

I also need to make some of the commands easier to change for anyone who ends up using it. like the path to dump.

I was gonna run on about some things that pissed me off while I wrote this, but the list is too big so here it is.

[code

#!/usr/local/bin/bash
#
# Copy-write John Zakhar, all rights reserved.
# This script is free for use, all I ask is you give me the credit for it.
# md5sum of this file is 9cda2aeaa0b27c3944145a53bbafe5f5
# jzakhar@newnewmedia.com ; www.newnewmedia.com ; TheNewNewMediaGroup ._.
#
#
# Set your web, save, and temp directories, or set tmp and save to the same, if you are not moving the
# files off to a SAN
#
# You can alter the date functions to change how often files are deleted. It's on my todo list to make it easily editable
#




EMAIL="jzakhar@newnewmedia.com" # Set your email address here.
EMAILAC=1 # Set this value to 1 to get confirmation emails, or 0 to not.
SQLPW="xxxxxxxxxxxxxxx" # Set your root password for mysql here.

#---------------------------------------------------------------------------------------------
# Begin editable regions.

SVDIR=/usr/home/nfs/sys_backups # Network share, were moving from Temp to here.
WEBDIR=/usr/local/www/ # Web directory.
TMPSV=/usr/home/media/sys_tmp # Temp save directory.
ARGS1="/tmp/bk_output"
ARGS2="/root/backup_info" # File that stores output from the dump and gzip commands.
CLEVEL=`/bin/cat /root/.dump_level`
DLEVEL=$[ $CLEVEL+1 ] # This was a bitch, do not change it.
DUMPLF=/root/.dump_level # File that stored current dump levels.
BKFAILUREFILE=/root/backup_failure # File that stores failure information to be mailed.
DDATES=`/bin/cat /etc/dumpdates`

# End editable regions.
#---------------------------------------------------------------------------------------------

OS=`uname` # Checking what version of nix you are running.

case $OS in

FreeBSD)

WDELDATE=`/bin/date -v-4d +%m-%d-%Y` # Web files Delete date.
DELDATE=`/bin/date -v-10d +%m-%d-%Y` # Dump files delete date.
CURDATE=`/bin/date +%m-%d-%Y` # Current date.
;;

Linux)

WDELDATE=`/bin/date --date '4 days ago' '+&m-%d-%y'` # Web files Delete date.
DELDATE=`/bin/date --date '10 days ago' '+&m-%d-%y'` # Dump files delete date.
CURDATE=`/bin/date --date 'today' '+&m-%d-%y'` # Current date.

;;

*)


echo "No valid OS found for this script"

;;

esac
# Don't edit below here unless you know what you are doing.

function printERROR() {
echo "ERROR:" $@ >&2
}


function getFreespace () {
if [ $# -lt 1 ] ; then
printERROR "No path specified"
return 1
fi
/bin/df -h "$1" | /usr/bin/awk 'NR !=1 {print $4;}' | /usr/bin/sed -e s/G//;
}


if [ $EMAILAC -eq 0 ];then
ARGS3=$ARGS1
else
ARGS3=$ARGS2
fi


function sendEmail() {
if [ $EMAILAC -eq 1 ]; then
/usr/bin/mail -s "Daily Backups" $EMAIL < $ARGS3;

fi
}


#echo $EMAILAC ## Checking some vars while writing this script.
#echo $EMAIL
#echo $ARGS3
#echo $CLEVEL
#echo $DLEVEL

function backup_RUN() {

if [ "`getFreespace /usr/home/nfs`" -ge 4 ]; then
if [ -e $SVDIR/$CURDATE.sysBackup.gz ] && [ -e $SVDIR/$CURDATE.sysBackup.gz ] && [ -e $SVDIR/$CURDATE.usrBackup.gz ] && [ -e $SVDIR/$CURDATE.mysqlBackup.gz ] && [ -e $SVDIR/$CURDATE.websites.Backup.tar.bz2 ]; then
echo "Backup Run already for today"
else
echo -n "" > $ARGS3
(/sbin/dump -$DLEVEL -auL -f - / | gzip -9 > $TMPSV/$CURDATE.sysBackup.gz) > $ARGS3 2>&1
(/sbin/dump -$DLEVEL -auL -f - /usr | gzip -9 > $TMPSV/$CURDATE.usrBackup.gz) > $ARGS3 2>&1
(/sbin/dump -$DLEVEL -auL -f - /var | gzip -9 > $TMPSV/$CURDATE.varBackup.gz) > $ARGS3 2>&1
echo "Mysql Dump" >> $ARGS3
(/usr/local/bin/mysqldump -u root -p$SQLPW -AeClqv | gzip -9 >$TMPSV/$CURDATE.mysqlBackup.gz) > $ARGS3 2>&1
echo "Web Tar" >> $ARGS3
(/usr/bin/tar -cps --bunzip2 $WEBDIR -f $TMPSV/$CURDATE.websites.Backup.tar.bz2) > $ARGS3 2>&1
echo "Moving Files From Temp to the SAN" >> $ARGS3
(/bin/mv -v $TMPSV/$CURDATE.sysBackup.gz $SVDIR/$CURDATE.sysBackup.gz) > ARGS3 2>&1
(/bin/mv -v $TMPSV/$CURDATE.varBackup.gz $SVDIR/$CURDATE.varBackup.gz) > $ARGS3 2>&1
(/bin/mv -v $TMPSV/$CURDATE.usrBackup.gz $SVDIR/$CURDATE.usrBackup.gz) > $ARGS3 2>&1
(/bin/mv -v $TMPSV/$CURDATE.websites.Backup.tar.bz2 $SVDIR/$CURDATE.websites.Backup.tar.bz2) > $ARGS3 2>&1
(/bin/mv -v $TMPSV/$CURDATE.mysqlBackup.gz $SVDIR/$CURDATE.mysqlBackup.gz) > $ARGS3 2>&1
echo "All Files Moved to SAN" >> $ARGS3
echo "Success!" >> $ARGS3
/bin/rm -rf $ARGS1
fi
else
getFreespace $SVDIR > $BKFAILUREFILE
exit 0
fi
}



if [ $CLEVEL -eq 9 ]; then
echo 0 > $DUMPLF
export DLEVEL=0
echo "in that loop"
echo $DLEVEL
backup_RUN

else
echo "in this loop"
echo $DLEVEL
echo $DLEVEL > $DUMPLF
backup_RUN
fi


if [ -e $SVDIR/$DELDATE.sysBackup.gz -o $SVDIR/$DELDATE.varBackup.gz -o $SVDIR/$DELDATE.usrBackup.gz ]; then
/bin/rm -rfv $SVDIR/$DELDATE.sysBackup.gz >> $ARGS3 2>&1
/bin/rm -rfv $SVDIR/$DELDATE.varBackup.gz >> $ARGS3 2>&1
/bin/rm -rfv $SVDIR/$DELDATE.usrBackup.gz >> $ARGS3 2>&1

else
continue
fi



if [ -e $SVDIR/$CURDATE.mysqlBackup.gz ]; then
if [ -e $SVDIR/$DELDATE.mysqlBackup.gz ]; then
rm -rf $SVDIR/$DELDATE.mysqlBackup.gz > $ARGS3 2>&1
else
continue
fi
fi


if [ -e $SVDIR/$CURDATE.websites.Backup.tar.bz2 ]; then
if [ -e $SVDIR/$DELDATE.websites.Backup.tar.bz2 ]; then
rm -rf $SVDIR/$WDELDATE.websites.Backup.tar.bz2 > $ARGS3 2>&1
else
continue
fi
fi

# Need to finish below here.
# Mail time.

sendEmail

[/code]

Last edited by turnip; 05-03-2005 at 06:54 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to remove backup ~ files hallamigo Linux - General 3 09-13-2010 04:47 PM
Backup script in bash gauge73 Programming 13 10-17-2005 07:25 AM
Shell Script for backup BBQ_Matt Linux - Software 7 06-30-2005 06:19 PM
Incremental Backup shell script. datadriven Linux - General 3 06-02-2004 10:19 AM
writting a backup shell script yenonn Slackware 2 03-18-2004 08:49 PM


All times are GMT -5. The time now is 05:04 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration