Shell Scripts

On this page you’ll find the code of my shell scripts. You’re welcome to use them!

Have a lot of fun…


backup script

I wrote this script so I could learn a bit more about writing shell scripts and finally I can put the command of this script in my crontab file.

This is a script which does a backup of specific folders and/or files. It creates a .tar.gz archive and sets the name of the backup as Backup-<date>.tar.gz.

Put your folders and files where I declared the variables PATH_# and change the path where the archive has to go.

Before you start the shell script, you maybe need to make it executable with this command:

$ chmod +x backup.sh

Run it with:

$ ./backup.sh

And here is the code of my script:

#!/bin/bash

# backup script
# backup.sh

# -e activates escape sequences like \n
echo -e "\nBackup will start now...\n"
echo -n -e "Continue? (y|n): "

read input

if [ "$input" = "y" ]

	then
	
	# prints the date like "11-11-2011"
	# new variable "VARDATE"

	VARDATE=`date +"%d-%m-%Y"`

	# print value of VARDATE with:
	# echo $VARDATE

	# test if you are at home
	test "$PWD" = "$HOME"

	if [ `echo $?` = "1" ]
	
		then cd ~
	fi

	# backup paths, work with relative paths
	PATH_1=Documents
	PATH_2=Studium
	PATH_3=Pictures
	PATH_4=.mozilla
	PATH_5=.thunderbird
	PATH_6=Public
	PATH_7=Shell.Skripte
	
	FILE_1=.conkyrc

	echo -e "\nThe following Folders will be archived:\n$PATH_1\n$PATH_2\n$PATH_3\n$PATH_4\n$PATH_5\n$PATH_6\n$PATH_7\n$FILE_1\n"

	# create the archive, preserve the time stemp when the files where accessed
	tar -czf Backup-$VARDATE.tar.gz --atime-preserve $PATH_1 $PATH_2 $PATH_3 $PATH_4 $PATH_5 $PATH_6 $PATH_7 $FILE_1

	FILESZ=`du -h Backup-$VARDATE.tar.gz`
	echo -e "\nSize of archive is:\n$FILESZ"
	
	# check if external hdd MaryJane is connected
	test -e /media/MaryJane/

	if [ `echo $?` = "1" ]
	then
		echo -e "\nExternal HDD 'MaryJane' not connected!\nArchive remains in HOME directory!\n"
	else
		# move the created archive to the external hdd "MaryJane"
		mv Backup-$VARDATE.tar.gz /media/MaryJane/Backups/
	fi

elif [ "$input" = "n" ]

	then echo -e "\nAborted"
else

	echo -e "\nIncorect Input"
fi

One thought on “Shell Scripts

Leave a reply to subertux Cancel reply