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


#!/bin/sh

# Copyright (c) 2015-2018 Oliver Mahmoudi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions 
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

### backupuser - a user backup utility

# Functions:
# make_package_list
# create_backup
# backup_etc
# do_hook
# restart
# strip_trailing_slash
# scp_get_args
# scp_get_destination
# copy_backup_scp
# get_path_to_file
# get_filename
# delete_backup
# usage

# Variables:
WHO=`whoami`		# == WHO=$(whoami)
DATE=$(date +%m.%d.%Y)
YEAR=$(date +%Y)	# This year
TIME=$(date +%H-%M-%S)
FILENAME=${WHO}_${DATE}_${TIME}.tar.gz
ETC_NAME=etc_${DATE}_${TIME}.tar.gz
BACKUP_BASE=${HOME}/backups
BACKUP_DIR=${BACKUP_BASE}/${YEAR}
BACKUP_DIR_ETC=${BACKUP_BASE}/etc/${YEAR}
BU_CONTENTS=		# contents of ${HOME}/.bu
OS_TYPE=$(uname)
SCP_DEST=
h_flag=0			# create backup in ${HOME} flag
p_flag=0			# create installed packages list flag
s_flag=0			# scp flag
v_flag=0			# verbose flag

#
# Save a list of the installed packages in root's home direcory if desired.
# Different operating systems use different package managers. 
# Currently supported are: Linux's yum and FreeBSD's pkg.
#

make_package_list()
{
	if [ "${OS_TYPE}" = "Linux" ] ; then
		if [ -e "/usr/bin/yum" ] ; then
			if [ $v_flag -eq 1 ] ; then
				echo -n "Generating the installed packages list..."
			fi
			yum list installed > ~/yum_list_installed.txt
			if [ $v_flag -eq 1 ] ; then
				echo "done."
				echo -n "Generating the installed groups list..."
			fi
			yum grouplist > ~/yum_grouplist.txt
			if [ $v_flag -eq 1 ] ; then
				echo "done."
				echo -n "Generating the installed repos list..."
			fi
			yum repolist all > ~/yum_repolist_all.txt
			if [ $v_flag -eq 1 ] ; then
				echo "done."
			fi
		fi
	elif [ "${OS_TYPE}" = "FreeBSD" ] ; then
		if [ $v_flag -eq 1 ] ; then
			echo -n "Generating the installed packages list..."
		fi
		pkg list > ~/installed_packages.txt
		if [ $v_flag -eq 1 ] ; then
			echo "done."
		fi
	else
		echo "Unknown package manager."
	fi
}

#
# Backup the user's home directoy. This is the main function.
#

create_backup()
{
	if [ $v_flag -eq 1 ] ; then
		echo "Getting ready to backup ${WHO}'s home directory:"
		echo
	fi

	if [ "${WHO}" = "root" -a $p_flag -eq 1 ] ; then
		make_package_list
	fi

	if [ ! -d ${BACKUP_DIR} ] ; then
		if [ $v_flag -eq 1 ] ; then
			echo -n "Your backup directory: ${BACKUP_DIR} doesn't seem to exist. Creating it..."
		fi
		mkdir -p ${BACKUP_DIR}
		if [ $v_flag -eq 1 ] ; then
			echo done.
		fi
	fi

	# Call the do_hook function which checks for any extra files to process in 
	# ${HOME}/.bu
	do_hook

	# Move to the temporary backup directory /tmp or else ${BACKUP_DIR} if h_flag is set
	if [ $v_flag -eq 1 -a $h_flag -eq 0 ] ; then
		echo -n "Moving to temporary backup folder /tmp..."
	elif [ $v_flag -eq 1 -a $h_flag -eq 1 ] ; then
		echo -n "Moving to backup folder ${BACKUP_DIR}..."
	fi

	if [ $h_flag -eq 0 ] ; then
		cd /tmp > /dev/null 2>&1
	else
		cd ${BACKUP_DIR} > /dev/null 2>&1
	fi

	if [ $v_flag -eq 1 ] ; then
		echo "done."
	fi

	# Create the backup
	if [ -e "${HOME}/bu_excludes.txt" ] ; then
		if [ $v_flag -eq 1 ] ; then
			echo "The following files and folders will not be backed up:"
			cat ${HOME}/bu_excludes.txt
			echo
			if [ $h_flag -eq 0 ] ; then
				echo -n "Creating backup in /tmp..."
			else
				echo -n "Creating backup in ${BACKUP_DIR}..."
			fi
		fi
		tar -czf ${FILENAME} --exclude ${BACKUP_BASE} -X ${HOME}/bu_excludes.txt ${HOME} > /dev/null 2>&1
		if [ $v_flag -eq 1 ] ; then
			echo "done."
		fi
	else
		if [ $v_flag -eq 1 -a $h_flag -eq 0 ] ; then
			echo -n "Creating backup in /tmp..."
		elif [ $v_flag -eq 1 -a $h_flag -eq 1 ] ; then
			echo -n "Creating backup in ${BACKUP_DIR}..."
		fi
		tar -czf ${FILENAME} --exclude ${BACKUP_BASE} ${HOME} > /dev/null 2>&1
		if [ $v_flag -eq 1 ] ; then
			echo "done."
		fi
	fi

	# If $h_flag == 0, move the backup to the directory it is destined for, ${BACKUP_DIR}
	# When being verbose, we here only need to report "echo done" to the user, when
	# we are compiling the archive in /tmp.
	if [ $v_flag -eq 1 -a $h_flag -eq 0 ] ; then
		echo -n "Moving backup from /tmp to ${BACKUP_DIR}..."
	fi

	if [ $h_flag -eq 0 ] ; then
		mv ${FILENAME} ${BACKUP_DIR} > /dev/null 2>&1
	fi

	if [ $v_flag -eq 1 -a $h_flag -eq 0 ] ; then
		echo done.
	fi

	# In case $h_flag == 1, we are already there, so therefore...
	if [ $h_flag -eq 0 ] ; then
		cd ${BACKUP_DIR} > /dev/null 2>&1
	fi

	# Test the backup
	if [ $v_flag -eq 1 ] ; then
		echo -n "Testing the backup..."
	fi
	gzip --test ${FILENAME} > /dev/null 2>&1

	if [ $? -eq 0 ] ; then
		if [ $v_flag -eq 1 ] ; then
			echo "OK"'!'
		fi
	else
		echo "File: ${FILENAME} is corrupted."
		rm -v ${FILENAME}
		exit 1
	fi

	# Checksums
	if [ $v_flag -eq 1 ] ; then
		echo -n "Generating checksum in ${BACKUP_DIR}/${WHO}_report.txt..."
	fi

	if [ "${OS_TYPE}" = "Linux" ] ; then
		sha256sum ${FILENAME} >> ${WHO}_report.txt	# Linux uses the sha256sum command
	elif [ "${OS_TYPE}" = "FreeBSD" ] ; then
		sha256 ${FILENAME} >> ${WHO}_report.txt		# FreeBSD uses sha256
	fi

	if [ $v_flag -eq 1 ] ; then
		echo "done."
	fi

	# Print out a status report.
	echo 'Backup successful!'
	echo "Your backup file is: ${BACKUP_DIR}/${FILENAME}"
}

#
# Make a backup of the /etc directory.
#

backup_etc()
{
	if [ "${WHO}" = "root" ] ; then
		if [ $v_flag -eq 1 ] ; then
			echo "Getting ready to backup the /etc directory:"
			echo
		fi

		if [ ! -d ${BACKUP_DIR_ETC} ] ; then
			if [ $v_flag -eq 1 ] ; then
				echo -n "Backup directory: ${BACKUP_DIR_ETC} doesn't seem to exist. Creating it..."
			fi
			mkdir -p ${BACKUP_DIR_ETC}
			if [ $v_flag -eq 1 ] ; then
				echo "done."
			fi
		fi

		# Move to the temporary backup directory /tmp. Only root can initiate a backup of
		# /etc so /tmp is always available.
		if [ $v_flag -eq 1 ] ; then
			echo -n "Moving to temporary backup folder /tmp..."
		fi
		cd /tmp > /dev/null 2>&1
		if [ $v_flag -eq 1 ] ; then
			echo "done."
		fi

		if [ $v_flag -eq 1 ] ; then
			echo -n "Creating the backup..."
		fi
		tar -czf ${ETC_NAME} /etc > /dev/null 2>&1
		if [ $v_flag -eq 1 ] ; then
			echo "done."
		fi

		# Move the backup to the directory it is destined for
		if [ $v_flag -eq 1 ] ; then
			echo -n "Moving backup from /tmp to ${BACKUP_DIR}..."
		fi
		mv ${ETC_NAME} ${BACKUP_DIR_ETC} > /dev/null 2>&1
		if [ $v_flag -eq 1 ] ; then
			echo done.
		fi
		cd ${BACKUP_DIR_ETC} > /dev/null 2>&1

		# test the backup
		if [ $v_flag -eq 1 ] ; then
			echo -n "Testing the backup..."
		fi
		gzip -t ${ETC_NAME} > /dev/null 2>&1

		if [ $? -eq 0 ] ; then
			if [ $v_flag -eq 1 ] ; then
				echo 'OK!'
			fi
		else
			echo "File: ${ETC_NAME} is corrupted."
			rm -v ${ETC_NAME}
			exit 1
		fi
		
		# Checksums
		if [ $v_flag -eq 1 ] ; then
			echo -n "Generating checksums in ${BACKUP_DIR_ETC}/etc_report.txt..."
		fi

        if [ "${OS_TYPE}" = "Linux" ] ; then
			sha256sum ${ETC_NAME} >> etc_report.txt		# Linux uses sha256sum
        elif [ "${OS_TYPE}" = "FreeBSD" ] ; then
			sha256 ${ETC_NAME} >> etc_report.txt		# FreeBSD uses sha256
        fi

        if [ $v_flag -eq 1 ] ; then
                echo "done."
        fi

		# Print out a status report.
		echo 'Backup successful!'
		echo "/etc backup file is: ${BACKUP_DIR_ETC}/${ETC_NAME}"
	else
		echo 'You are not the root user. You need to be root to backup the /etc directory!'
		exit 1
	fi ### root ###
}

#
# The modular hook function begings here. Check whether there are any files in the
# $HOME/.bu folder. If so, then source them.
#

do_hook()
{
if [ -d ${HOME}/.bu ] ; then
	cd $HOME/.bu
	BU_CONTENTS=*
	if [ -n "$BU_CONTENTS" ] ; then
		if [ $v_flag -eq 1 ] ; then
			echo "Sourcing files from folder $HOME/.bu:"
		fi
		for i in $BU_CONTENTS
		do
			if [ $v_flag -eq 1 ] ; then
				echo "Sourcing $HOME/.bu/$i"
			fi
			source ./$i
			if [ $v_flag -eq 1 ] ; then
				echo
			fi
		done
		if [ $v_flag -eq 1 ] ; then
			echo "Done sourcing $HOME/.bu."
		fi
	fi
fi
}

#
# Clean the users backup directory.
#

restart()
{
	local choice

	# Confirm
	echo "This will delete the entire contents of you backupfolder: ${BACKUP_BASE}"
	echo -n "Are you sure? [yes or no]: "
	read -t 30 choice		# We got 30 seconds to make a choice

	case $choice in

	[Yy][Ee][Ss] | [Yy] )			# remove old files : >
		rm -fvr ${BACKUP_BASE}/*
		;;
	[Nn][Oo] | [Nn] )
		echo 'Aborted!'
		exit 1
		;;
	*)
		echo "No input received. Terminating."
		exit 1
		;;
	esac
}

#
# Strip a trailing slash from given pathnames, i.e.
# /path/to/file/ becomes /path/to/file
#

strip_trailing_slash()
{
	local _length _strlen _last_char

	_length=$(awk -v value=$1 'BEGIN {
		n = length(value);
		print n;
	}')

	_last_char=${1:$_strlen-1:1}

	if [ "$_last_char" = "/" ] ; then
		echo ${1:0:$_strlen-1}
	else
		echo $1
	fi
}

#
# Copy the backup to another local device.
#

copy_backup_local()
{
	local _i

	_i=$1

	if [ -d $_i ] ; then
		if [ -w $_i ] ; then
			_i=$(strip_trailing_slash $_i)
			if [ $v_flag -eq 1 ] ; then
				echo -n "Copying backup to: $_i..."
			fi
			cp -i ${BACKUP_DIR}/${FILENAME} $_i
			if [ $v_flag -eq 1 ] ; then
				echo "done."
			fi
		else
			echo "$_i is not writable."
		fi
	else
		echo "$_i is not a directory."
	fi
}

#
# Get the destination for scp. Passed via -s.
#

scp_get_destination_path()
{
	local _dest

	_dest=$(awk -v value="$1" 'BEGIN {
		n=split(value, a);
		print a[n];
	}')

	echo $_dest
}

#
# Get the arguments (if any) for scp. Passed via -s.
#

scp_get_arguments()
{
	local _args

	_args=$(awk -v value="$1" 'BEGIN {
		args=""
		n=split(value, a);
		for(i = 1; i < n; i++)
			args=args" "a[i];

		print args;
	}')

	echo $_args
}

#
# Copy the backup via scp
#

copy_backup_scp()
{
	local _scp_args _scp_path _file

	_file=$2

	if [ $v_flag -eq 1 ] ; then
		echo "Copying backup via scp:"
	fi

	_scp_args=$(scp_get_arguments "$1")
	_scp_path=$(scp_get_destination_path "$1")

	if [ ! -z "$_scp_args" ] ; then
		scp $_scp_args $_file $_scp_path
	else
		scp $_file $_scp_path
	fi
}

#
# usage function
#

usage()
{
	echo "usage: backupuser [-hpuv] [-d backup] [-s remote_server] \
local_disk_1 local_disk_2 ..."
	echo "       backupuser restart"
	echo "       backupuser etc"
}

############################################################
##### Point of entry

while getopts ":vhups:d:" opt ; do
        case $opt in
                h)
                        h_flag=1		# create archive in ${BACKUP_DIR}
                        ;;
                v)
                        v_flag=1		# be more verbose
                        ;;
                p)
                        p_flag=1		# create installed packages list flag
                        ;;
                s)
                        s_flag=1		# scp flag
						SCP_DEST=$OPTARG
                        ;;
                u)
                        usage
						exit 0
                        ;;
                d)
                        delete_backup $OPTARG
                        ;;
                \?)
                        echo "unkown flag: -$OPTARG."
                        usage
                        exit 1
                        ;;
				:)
						echo "The -$OPTARG flag needs an argument"
                        usage
						exit 1
						;;
        esac
done

shift $((OPTIND-1))

if [ "$1" = "restart" ] ; then
	restart
	echo 'Done!'
	exit 0
fi

if [ "$1" = "etc" ] ; then
	backup_etc
	if [ $s_flag -eq 1 ] ; then
		copy_backup_scp "$SCP_DEST" ${BACKUP_DIR_ETC}/${ETC_NAME}
	fi
	echo 'Done!'
	exit 0
fi

create_backup

if [ $s_flag -eq 1 ] ; then
	copy_backup_scp "$SCP_DEST" ${BACKUP_DIR}/${FILENAME}
fi

for i in $*
do
	copy_backup_local $i
done

# done
echo 'Done!'
exit 0


### implement from here

get_path_to_file()
{
	local _awkvar _ptf

	# Get the left part of the last "/" aka path to file.
	_ptf=$(awk -v _awkvar=$1 'BEGIN { 
		string = "";
		n = split(_awkvar, a, "/");

		for(i = 2; i < n; i++) 
			string = string"/"a[i];

		string = string"/";
		print string;
		}')

	echo $_ptf
}

get_filename()
{
	local _awkvar _fn

	# Get the right part of the last "/" aka filename.
	_fn=$(awk -v _awkvar=$1 'BEGIN { 
		n = split(_awkvar, a, "/");
		print a[n];
		}')

	echo $_fn
}

delete_backup()
{
	local _bu _choice _file _ln _nobs _rf

	# Check for the existence of the backup that we seek to delete.
	_bu=$(realpath $1)
	if [ ! -f $_bu ]; then
		echo "The backup: $_bu doesn't exist."
		exit
	fi

	# Construct the report file and see if it exists in the target directory.
	_rf=$(get_path_to_file $_bu)${WHO}_report.txt
	if [ ! -f $_rf ]; then
		_rf=$(get_path_to_file $_bu)etc_report.txt
		if [ ! -f $_rf ]; then
			echo "The reportfile doesn't exist."
			exit
		fi
	fi

	# Check if the backup in our reportfile is unique. If so, get the line number.
	_file=$(get_filename $_bu)
	_nobs=$(cat $_rf | grep -c $_file)

	if [ $_nobs -eq 0 ]; then
		echo "No entry for: $_file in the reportfile."
		exit
	elif [ $_nobs -eq 1 ]; then
		_ln=$(cat $_rf | grep -n $_file)
	else
		echo "Entry for: $_file in reportfile not unique."
		exit
	fi

	# Make sure and delete the backup. Otherwise abort.
	echo "This will delete your backup: $_bu"
	echo -n "Are you sure? [yes or no]: "
	read -t 30 _choice		# We got 30 seconds to make a choice

	case $_choice in
	[Yy][Ee][Ss] | [Yy] )
		sed -i ${_ln:0:1}d $_rf
		rm -v $_bu
		exit 0
		;;
	[Nn][Oo] | [Nn] )
		echo 'Aborted!'
		exit 1
		;;
	*)
		echo "No input received. Terminating."
		exit 1
		;;
	esac
}

if [ -e "/usr/bin/dpkg" ] ; then
	if [ $v_flag -eq 1 ] ; then
		echo -n "Generating the installed packages list..."
	fi
	dpkg -l > ~/installed_packages_list.txt
	if [ $v_flag -eq 1 ] ; then
		echo "done."
	fi
else
	echo "Unknown Linux package manager."
fi

test test test test test


bu_check_files


#!/bin/sh

# Copyright (c) 2015-2018 Oliver Mahmoudi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions 
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# 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=${USER}_report.txt
REPORT_FILE_ETC=etc_report.txt
OS_TYPE=$(uname)

### Functions:
# create_etc_report_file
# create_user_report_file
# get_path_to_file
# get_filename
# delete_backup

create_etc_report_file()
{
	local _etc_contents

	_etc_contents=etc*.tar.gz

	: > ${REPORT_FILE_ETC}								# create a fresh report file

	for i in ${_etc_contents} ; do
		gzip --test ${i} > /dev/null 2>&1
		if [ $? -eq 0 ] ; then							# Checksums
			if [ "${OS_TYPE}" = "Linux" ] ; then
				sha256sum ${i} >> ${REPORT_FILE_ETC}	# Linux uses the sha256sum command
			elif [ "${OS_TYPE}" = "FreeBSD" ] ; then
				sha256 ${i} >> ${REPORT_FILE_ETC}		# FreeBSD uses sha256
			fi
		else
			echo "File: ${i} is corrupted." >> ${REPORT_FILE_ETC}
		fi
	done
}

create_user_report_file()
{
	local _contents
	
	_contents=*.tar.gz

	: > ${REPORT_FILE_USER}								# create a fresh report file

	for i in ${_contents} ; do
		gzip --test ${i} > /dev/null 2>&1
		if [ $? -eq 0 ] ; then							# Checksums
			if [ "${OS_TYPE}" = "Linux" ] ; then
				sha256sum ${i} >> ${REPORT_FILE_USER}	# Linux uses the sha256sum command
			elif [ "${OS_TYPE}" = "FreeBSD" ] ; then
				sha256 ${i} >> ${REPORT_FILE_USER}		# FreeBSD uses sha256
			fi
		else
			echo "File: ${i} is corrupted." >> ${REPORT_FILE_USER}
		fi
	done
}

get_path_to_file()
{
	local _awkvar _ptf

	# Get the left part of the last "/" aka path to file.
	_ptf=$(awk -v _awkvar=$1 'BEGIN { 
		string = "";
		n = split(_awkvar, a, "/");

		for(i = 2; i < n; i++) 
			string = string"/"a[i];

		string = string"/";
		print string;
		}')

	echo $_ptf
}

get_filename()
{
	local _awkvar _fn

	# Get the right part of the last "/" aka filename.
	_fn=$(awk -v _awkvar=$1 'BEGIN { 
		n = split(_awkvar, a, "/");
		print a[n];
		}')

	echo $_fn
}

delete_backup()
{
	local _bu _choice _file _ln _nobs _rf

	# Check for the existence of the backup that we seek to delete.
	_bu=$(realpath $1)
	if [ ! -f $_bu ]; then
		echo "The backup: $_bu doesn't exist."
		exit
	fi

	# Construct the report file and see if it exists in the target directory.
	_rf=$(get_path_to_file $_bu)${REPORT_FILE_USER}
	if [ ! -f $_rf ]; then
		_rf=$(get_path_to_file $_bu)${REPORT_FILE_ETC}		# Maybe it's an etc report file.
		if [ ! -f $_rf ]; then
			echo "The reportfile doesn't exist."
			exit
		fi
	fi

	# Check if the backup in our reportfile is unique. If so, get the line number.
	_file=$(get_filename $_bu)
	_nobs=$(cat $_rf | grep -c $_file)

	if [ $_nobs -eq 0 ]; then
		echo "No entry for: $_file in the reportfile."
		exit
	elif [ $_nobs -eq 1 ]; then
		_ln=$(cat $_rf | grep -n $_file)
	else
		echo "Entry for: $_file in reportfile not unique."
		exit
	fi

	# Make sure and delete the backup. Otherwise abort.
	echo "This will delete your backup: $_bu"
	echo -n "Are you sure? [yes or no]: "
	read -t 30 _choice		# We got 30 seconds to make a choice

	case $_choice in
	[Yy][Ee][Ss] | [Yy] )
		sed -i ${_ln:0:1}d $_rf
		rm -v $_bu
		exit 0
		;;
	[Nn][Oo] | [Nn] )
		echo 'Aborted!'
		exit 1
		;;
	*)
		echo "No input received. Terminating."
		exit 1
		;;
	esac
}

usage()
{
	echo "usage: $0 bu_check_files [u] [-d backup] [etc]"
}


### entry point

while getopts ":ud:" opt ; do
        case $opt in
                u)
                        usage
						exit 0
                        ;;
                d)
                        delete_backup $OPTARG
                        ;;
                \?)
                        echo "unkown flag: -$OPTARG."
                        usage
                        exit 1
                        ;;
				:)
						echo "The -$OPTARG flag needs an argument"
                        usage
						exit 1
						;;
        esac
done

shift $((OPTIND-1))

if [ "${1}" = "etc" ] ; then
	create_etc_report_file
	echo "Done"'!'
	exit 0
fi

create_user_report_file
echo "Done"'!'

exit 0

A gzipped tarball with the source code can be found here:


Version 1.0:
File: backupuser-1.0.tar.gz
sha256sum: f33fdc33fd5e98e5fd2fbc517bfeddd83e1851d9f4e0348827c69de98ee07f53



Alternatively you could clone the source code from github with:



git clone https://github.com/olimah/backupuser