[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Need help/feedback on backup script



I've created a backup script and would like some feedback/help on it.  Any tips or
pointers would be greatly appreciated.  Feel free to use/hack it on your own.

here it is:

<-- Begin Script -->
#!/bin/bash

# Many thanks to Robb Kidd who brought up the question of backup
# strategies on the Debian-user mailing list and Karsten M. Self's
# responses. This script wouldn't have been possible without them. :-)


# To do list
	# I would like to make all the configuration
	# done in the variable section.  Let the script
	# remain the same; just update/change variables
	# as necessary.  That way anyone can use it.

	# Need to read up on sed or awk so I can change
	# the "/" of the directories and change them
	# to "." for the saved tar's filename.

	# Develop a system which allows tar to exclude files
	# based on choices made in the variable section.
	# This will have to be pretty flexible. This should
	# take care of the way the /usr/local section looks
	# right now.  :-(

	# Would it be possible to create one base script
	# and then just loop through it with variables
	# controlling what is backed up and what is not?
	# Of course.  This is Linux. Study harder!

	# Script to rebuild.

# My simple script for backing up files (it started that way, jeesh)

	# Create variables
		# The root directory of the backed up files 
		# before you commit it to the backup media.
		# Note, this could be the media itself if you
		# don't use another program to put it there.
		# (i.e. Zip/tape drives vs. CD-R's)
 		ARCHIVE_ROOT="/usr/local/cdsrc/backup"

		# This is where you'll put the tar-zipped files.
		# I add this primarily for users who put files
		# in their backup other than what is covered in
		# this script.  (i.e. custom .deb files, etc.)
		# This way you can keep these files "seperate"
		# from the remainder of your backup volume. I
		# believe this really only helps debian users
		# who are making the media "apt-getable".
		BACKUP_DIR="$ARCHIVE_ROOT/backup"

		# This is where you store your custom kernel.
		# Do not change this variable or comment it out
		# if you do not have a custom kernel.
		# The script tests against it being "not_set".
		# If you do not set this, the script will attempt
		# to grab the kernel from /usr/src/linux/.config
		# If you do have a custom kernel include the full
		# absolute path AND filename of the configuration file.
		CUSTOM_KER="not_set"

		# Default tar options are -IPpscvf.  I recommend you
		# leave it in verbose (-v) mode at least the first time
		# through so you know it's doing what you want.  You can
		# find out exactly what the options are doing by typing
		# man tar at the prompt.  One weird thing I noticed about
		# tar; you need to keep the capital letter options first.
		# Keep that in mind if you edit this variable.
		TAR_OPT_DEF="-IPpscf"

		# The tar file suffix.  In case you want .tgz or .bzip2, etc.
		# Note: do not include a "." in front of the suffix (extentsion)
		TAR_FILE_SUF="bzip2"

############################################################################
# Once this script is complete, people shouldn't have to configure anything#
# below this point.                                                        #
############################################################################


	# Create "Test" variables.  These will be used in the
	# "Test script configuration" section.
		# If you add more variables ensure there is a
		# space between them. Testing to see if the
		# root and backup directories exist and are directories.
		DIR_VAR_LIST="$ARCHIVE_ROOT $BACKUP_DIR"

		# Program variables (setting up for PROG_VAR_LIST)
		PROG_TAR=`which tar`
		PROG_GZIP=`which gzip`
		PROG_BZIP2=`which bzip2`
		PROG_TEST_NULL=

			# So the variable list below works we need to set variables
			# to some value if they're not here, otherwise the output
			# of the for loop doesn't look right.
			if [ $PROG_TAR == $PROG_TEST_NULL ]; then
			PROG_TAR="tar"
			fi
	
			if [ $PROG_GZIP == $PROG_TEST_NULL ]; then
			PROG_GZIP="gzip"
			fi
	
			if [ $PROG_BZIP2 == $PROG_TEST_NULL ]; then
			PROG_BZIP2="bzip2"
			fi

		# If you add more variables ensure there is a
		# space between them and you add an if statement
		# covering it like the ones above. Testing to see
		# if the programs needed are here.
		PROG_VAR_LIST="$PROG_TAR $PROG_GZIP $PROG_BZIP2"

 	# For future use, if I get around to it.
	# Would allow user to change backed up file's name, etc.
	#COPY_DIR="directory being copied"
	#COPY_DIR_FN="The aboves filename after back up"


# Test script configuration
	echo ""
	echo "Testing script configuration..."	

	for CURRENT_VAR in $DIR_VAR_LIST;
	do
		if [ -d $CURRENT_VAR ]; then
			echo "$CURRENT_VAR exists."
		else
			echo "$CURRENT_VAR does not exist."
			echo "There is a problem with the way your variables are configured."
			echo "Check your script configuration."
			exit 1
		fi
	done
	
	echo "Variable configuration seems to be OK.  All directories exist."
	echo ""

	echo "Testing to see if needed programs are available..."

	for CURRENT_VAR in $PROG_VAR_LIST;
	do
		if [ -e $CURRENT_VAR ]; then
			echo "$CURRENT_VAR exists."
		else
			echo ""
			echo "$CURRENT_VAR is not located on this system,"
			echo "could not be found, or is not in your path."
			echo "If you know the program is installed put it"
			echo "in your path, otherwise you can ignore this message."
			echo ""
		fi
	done	


	echo "Some programs are not required depending on how you"
	echo "configure this script. This message is meant to be"
	echo "informative in case you are having problems."
	echo ""
	echo "Done testing for programs."



# Backup the /home directory

	# Change to backup directory. I
	# keep all my backup files inside
	# a backup directory on the root
	# of the archive.
		cd $BACKUP_DIR
		tar $TAR_OPT_DEF home.$TAR_FILE_SUF /home
		echo '/home directory backed up!'	


# Backup the /usr/local directory

	# change to root directory
	cd $ARCHIVE_ROOT

	# I keep my backup inside /usr/local
	# so I need to exclude the backup 
	# directory so I don't back it up twice
	# This creates a list of files tar will exclude
	find . * > excludelist
	tar -IPX excludelist -pscvf usr.local.$TAR_FILE_SUF /usr/local

	# I like to keep all backup files together
	# Move usr.local.bzip2 and excludelist to backup directory
	cd $ARCHIVE_ROOT
	mv usr.local.$TAR_FILE_SUF $BACKUP_DIR/
	mv excludelist $BACKUP_DIR/
	echo '/usr/local directory backed up!'


# Backup the /etc directory

	# Haven't figured out how to incorporate
	# this one in yet. Need to do some studying.
	# I guess I could run the whole script as root
	# but would that affect permissions?  Shouldn't with
	# tar's -p option?  Maybe give a choice to su?
	# Don't want to su if I don't have to. Too much power.

	# Change to backup directory
	#cd $BACKUP_DIR
	
	# Need to be root for permissions sake.
	# su  
	# tar $TAR_OPT_DEF etc.$TAR_FILE_SUF /etc
	# echo '/etc directory backed up!'


# If it's Debian ;-> Backup dpkg's database of what's installed


	if [ -e /etc/debian_version ]; then
	     	cd $BACKUP_DIR/
		echo -n "Getting copy of dpkg database..."
		dpkg --get-selections > dpkg.selections
		echo "Done"
	else
		echo "This doesn't appear to be a Debian system...8-("
		echo "Skipping dpkg --get-selections"
	fi


# Backup kernel configuration 

	# Change to backup directory
	cd $BACKUP_DIR

	# Grab custom kernel if CUSTOM_KER doesn't equal not_set
	if [ $CUSTOM_KER != "not_set" ]; then
		echo -n "Getting copy of custom kernel config file..."
		cp $CUSTOM_KER kernel.custom.config
		echo "Done"
	# Grab default kernel configuration if CUSTOM_KER equals not_set
	elif [ -e /usr/src/linux/.config ]; then
		echo -n "Getting copy of kernel config file..."
		cp /usr/src/linux/.config kernel.custom.config
		echo "Done"
		echo 'CUSTOM_KER not set in script.  No custom kernel!?'
		echo 'You are not taking advantage of Linux like you could!'
	else
		echo "No kernel .config file found on your system!"
		echo "If you have a custom kernel or it's not located"
		echo "in /usr/src/linux then set the CUSTOM_KER variable"
		echo "in this script to back it up.  If you don't know"
		echo "what this is about, you don't need to worry."
		echo "Kernel .config file NOT backed up!"
	fi


# Reminder to copy /etc as root. Plus you can just cut
# and paste it from the console this way.
	echo ''
	echo ''
	echo '/etc directory not backed up! You need to be root!'
	echo "Please use the command:"
	echo " cd $BACKUP_DIR; tar $TAR_OPT_DEF etc.$TAR_FILE_SUF /etc"


# Reminder about custom kernels

	echo ""
	echo ""
	echo "Warning!"
	echo "Your kernel config file may need to be backed up"
	echo "to media other than your backup volume.  This is especially"
	echo "true if you backup to hardware you had to configure in the"
	echo "kernel during installation of your OS in order to use it."
	echo "A zip or tape drive would be an example."
	echo "Warning !"
	echo ""
	echo ""

<-- End Script -->

-- 
Got freedom?
http://www.debian.org



Reply to: