[HOME] [BASH] [Window Managers]

Some BASH scripts

A first solid step towards a real independence is not full yourself: the power of UNIX like operating systems is at command line.

The following are some BASH scrips I have in my ~/bin folder. Themselves have nothing special but cover some usability holes, especially for desktop users.


Backup data to a single or multi session CD

#!/bin/sh
# backup-data - Walter A. Iglesias (last modif. 2011-10-18)

# cdrecord needs to be executed by root
[ `whoami` != "root" ] && echo "You must be root" && exit 126

# Change the following
device='2,0,0'	# (cdrecord -scanbus)
mountdir='/media/cdrom'

usage()
{
	printf "Usage: `basename $0` [-m] [<cd/path1>=]<hd/path1> \\
	[[<cd/path2>=]hd/path2> [<cd/pathN=]hd/pathN>...]

-m for multisession\n"
}

# Label to graphical file manager
label="BAK-`date +%Y-%m-%d`"

# Save iso to $TMPDIR and clean on exit
image=`mktemp -t bak.iso.XXXXXX`
trap "rm $image* 2>/dev/null" EXIT

# Checking general args
[ $# -eq 0 ] &&	usage && exit 1

# If cdrom is mounted unmount it
grep -q cdrom /etc/mtab && umount $mountdir
	
# Checking if option -m is present
if [ "$1" = "-m" ]; then
	# ** Multiple session **
	# Checking -m args
	[ "$2" = "" ] && usage && exit 1

	# Geting mkisofs -graft-points arguments
	while [ $2 ]; do
		paths="$paths $2"
		shift
	done

	# If files are >= 4G add iso-level option to mkisofs command
	filesize=`du -cs $paths | awk '/total/ { print $1 }'`
	[ $filesize -ge 4000000 ] && isolevel='-iso-level 4'

	# Save to a variable the session offset (where finish sessions recorded
	# in the cd)
	sectors=`cdrecord -msinfo dev=$device 2>/dev/null`
	
	if [ "$sectors" != "" ] ; then
		mkisofs $isolevel -dir-mode 0755 -r -J -C $sectors -M $device \
			-o $image -graft-points $paths
	else
		mkisofs $isolevel -dir-mode 0755 -r -J -o $image -V $label \
		       	-graft-points $paths
	fi || exit 1

	cdrecord -v driveropts=burnfree speed=2 -multi -data dev=$device \
	       	-eject $image || exit 1

else
	# ** Single session **
	while [ $1 ]; do
		paths="$paths $1"
		shift
	done

	# If files are >= 4G add iso-level option to mkisofs command
	filesize=`du -cs $paths | awk '/total/ { print $1 }'`
	[ $filesize -ge 4000000 ] && isolevel='-iso-level 4'

	mkisofs $isolevel -r -J -o $image -V $label -graft-points $paths ||
		exit 1

	cdrecord -v driveropts=burnfree speed=2	-data dev=$device \
		-eject $image || exit 1
fi

exit 0

# End

Next =>



[HOME] [BASH] [Window Managers]