backupuser


Creating backups is an essential part of a users routine operations. Here are two scripts that a user can use, to manage backups of his home folder ~/:

backupuser.sh

#!/bin/sh

# variables
WHO=`whoami`
DATE=$(date +%m.%d.%Y)
TIME=$(date +%H-%M-%S)
FILENAME=${WHO}_${DATE}_${TIME}.tar.gz
ETC_NAME=etc_${DATE}_${TIME}.tar.gz

create_backup()
{
	# sanity checks
	sanity_checks

	# delete any core files that office or others 
	# programs may have left behind
	rm -f ${HOME}/*.core

	# working directory
	cd /usr/local/backups/${WHO}/

	# create the backup
	tar -czf ${FILENAME} ${HOME} > /dev/null 2>&1

	# test the backup
	gzip --test ${FILENAME} > /dev/null 2>&1

	if [ $? -eq 0 ] ; then
		echo "File: ${FILENAME} is OK." >> ${WHO}_report.txt
	else
		echo "File: ${FILENAME} is corrupted."
 		rm -v ${FILENAME}
		exit 1
	fi

	# checksums
	md5 ${FILENAME} >> ${WHO}_report.txt
	sha1 ${FILENAME} >> ${WHO}_report.txt

	if [ "${WHO}" = "root" ] ; then
		cd /usr/local/backups/etc/ > /dev/null 2>&1

		if [ $? -ne 0 ] ; then
			mkdir -p /usr/local/backups/etc
			cd /usr/local/backups/etc
		fi

		tar -czf ${ETC_NAME} /etc > /dev/null 2>&1

		# test the backup
		gzip -t ${ETC_NAME} > /dev/null 2>&1

		if [ $? -eq 0 ] ; then
			echo "File: ${ETC_NAME} is OK." >> etc_report.txt
		else
			echo "File: ${ETC_NAME} is corrupted."
                        rm -v ${ETC_NAME}
			exit 1
		fi
		
		# checksums
		md5 ${ETC_NAME} >> etc_report.txt
		sha1 ${ETC_NAME} >> etc_report.txt
	fi ### root ###
}

sanity_checks()
{
	# does our target directory exist?
	if [ ! -d "/usr/local/backups/${WHO}/" ] ; then
		echo "Your backup directory: /usr/local/backups/${WHO}/ does not exist."
		echo "Please create it or contact your administrator."
		exit 1		# does not exist
	fi

	# is it writeable by the caller?
	if [ ! -w "/usr/local/backups/${WHO}/" ] ; then
		echo "Your backup directory: /usr/local/backups/${WHO}/ is not writeable."
		echo "Please make it writeable or contact your administrator."
		exit 1		# is not writeable
	fi
}

restart()
{
	# sanity checks
	sanity_checks

	# remove old files
	# : > /usr/local/backups/${WHO}/*.txt		
	rm -f /usr/local/backups/${WHO}/*.tar.gz
	rm -f /usr/local/backups/${WHO}/*.tar.gz.gpg
}

############################################################
##### entry point

if [ "$1" = "restart" ] ; then
	restart
	exit 0
fi

create_backup

# done
exit 0

The second script check_files.sh is placed on the remote drive.

#!/bin/sh

# Place this script in the folder to which you copy your backups and run it
# to:
# 	a) check the files for errors via gzip --test and 
# 	b) generate their checksums.
# 
# Later, the generated report file can be diffed with the original report file 
# on the users hard drive.
# 

### variables
USER=$(whoami)
REPORT_FILE=${USER}_report.txt
CONTENTS=$(ls *.tar.gz)

### entry point
: > ${REPORT_FILE}				# create a fresh report file

for i in ${CONTENTS} ; do
	gzip --test ${i} > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		echo "File: ${i} is OK." >> ${REPORT_FILE}
		md5 ${i} >> ${REPORT_FILE}
		sha1 ${i} >> ${REPORT_FILE}
	else
		echo "File: ${i} is corrupted." >> ${REPORT_FILE}
	fi
done

exit 0

The idea is as follows:

The script backupuser.sh runs on the host machine where the backup is created.

You can place it in say the /usr/local/bin directory.

The second script: check_files.sh is run on whatever media you have placed your backup files in.

The report file which both scripts generate can then be diffed to see if anything bad happened while transferring them.

Porting these scripts to Linux is very much straightforward. Just change the md5 and sha1 commands to md5sum and sha1sum respectively.