kinson
April 17th, 2008, 11:38 AM
Hi guys, I've been doing a spot of newbie scripting, mainly for backing up my data to my external drive. I don't need to backup the system, just my pictures, songs etc.
Basically this is my goal:
1) backup to my external's path(which I've set to fixed)
2) have a backup and a backup and check option(cause checking takes quite a while, so backup for quickie backups)
3) rename the folder to current date (i.e. Data ddmmyy)
I've been fiddling with it so far, but I think its getting a little too complex for its own good....thus I wanted to get a second opinion, perhaps I might be screwing myself :p
#////////////////////GLOBAL VARIABLES/////////////////////////////
sourcePath="/home/kinson/Data/"
destPath="/media/usbdisk/"
fullDestPath="/media/usbdisk/"
folderVar="Data"\ $(date +%d%m%y)
#////////////////////FUNCTIONS/////////////////////////////
showMenu(){
echo "1)Backup (Daily Use)"
echo "2)Backup and Check Differences (Takes a long time)"
echo "3)Quit"
}
backupData(){ #Function to backup the data ONLY, much faster after everything's been backed up once
echo "preparing to backup the data now..."
rsync -av --delete $sourcePath $destPath"/Data\ 280108/" > /home/kinson/Desktop/Backup_Script_Report.log
echo "finished backing up the data..."
}
checkData(){ #Function to check if the data has been backed up, and output to a log file
echo "Will now proceed to check the differences(hopefully none) in the data..."
diff -ru /home/kinson/Data/ /media/usbdisk/Data\ 280108/ > /home/kinson/Desktop/compareBackupData.log
echo "finished checking the data..."
}
thankExit(){ #Function to thank the user, and exit the script
echo -e "\nThank you for using my backup program."
echo "The program will now exit...."
exit
}
checkFolderExist(){#unfinished
echo "Checking to see if folder exists..."
if [ -d Data\ ?????? ]; then
if [ Data\ ?????? != "$folderVar" ]; then
echo "Renaming existing backup folder to current date...
mv "$destPath"Data\ ?????? "$fullDestPath"
echo -e "\nfinished renaming the previous folder"
fi
else
while [ 1 ]
do
echo "Folder doesn't exist yet, create?"
read -p "Do you want to create the folder and continue backup job?(y/n)"
if [ "$REPLY" = "y" ]; then
echo "Creating folder for backup"
break
elif ["$REPLY" = "n"]; then
echo "Exiting program..."
exit
else
echo "Please enter 'y' or 'n'"
fi
done
fi
}
#////////////////MAIN///////////////////////////
#Clear screen and start program
clear
#prepare the fullDestPath variable
#e.g. /media/usbdisk/Data\ 280108/
fullDestPath=$fullDestPath$folderVar
echo "*************************************************"
echo "* *"
echo "* *"
echo "* *"
echo "* Welcome to Kinson's backup script(v0.1) *"
echo "* *"
echo "* *"
echo "* *"
echo "*************************************************"
echo $fullDestPath
while [ 1 ]
do #Loop the whole program until exit
echo -e "Please make a selection:\n"
showMenu
read CHOICE
case "$CHOICE" in
"1")# Backup (Daily Use)
backupData
;;
"2")#Backup and Check Differences (Takes a long time)"
backupData
checkData
;;
"3")#Quit
thankExit
;;
*)
echo "Please make a valid selection"
;;
esac
done
Currently I'm finishing up that "checkFolderExist" function...but it seems kinda uneccessary. Since there's only gonna be one Data xxxxxx folder there, but just thought that it'd be a good experience. And to check if "Data ?????" exists by using wildcards seems a little tacky.....any comments would be welcome, cheers :)
Kinson
Basically this is my goal:
1) backup to my external's path(which I've set to fixed)
2) have a backup and a backup and check option(cause checking takes quite a while, so backup for quickie backups)
3) rename the folder to current date (i.e. Data ddmmyy)
I've been fiddling with it so far, but I think its getting a little too complex for its own good....thus I wanted to get a second opinion, perhaps I might be screwing myself :p
#////////////////////GLOBAL VARIABLES/////////////////////////////
sourcePath="/home/kinson/Data/"
destPath="/media/usbdisk/"
fullDestPath="/media/usbdisk/"
folderVar="Data"\ $(date +%d%m%y)
#////////////////////FUNCTIONS/////////////////////////////
showMenu(){
echo "1)Backup (Daily Use)"
echo "2)Backup and Check Differences (Takes a long time)"
echo "3)Quit"
}
backupData(){ #Function to backup the data ONLY, much faster after everything's been backed up once
echo "preparing to backup the data now..."
rsync -av --delete $sourcePath $destPath"/Data\ 280108/" > /home/kinson/Desktop/Backup_Script_Report.log
echo "finished backing up the data..."
}
checkData(){ #Function to check if the data has been backed up, and output to a log file
echo "Will now proceed to check the differences(hopefully none) in the data..."
diff -ru /home/kinson/Data/ /media/usbdisk/Data\ 280108/ > /home/kinson/Desktop/compareBackupData.log
echo "finished checking the data..."
}
thankExit(){ #Function to thank the user, and exit the script
echo -e "\nThank you for using my backup program."
echo "The program will now exit...."
exit
}
checkFolderExist(){#unfinished
echo "Checking to see if folder exists..."
if [ -d Data\ ?????? ]; then
if [ Data\ ?????? != "$folderVar" ]; then
echo "Renaming existing backup folder to current date...
mv "$destPath"Data\ ?????? "$fullDestPath"
echo -e "\nfinished renaming the previous folder"
fi
else
while [ 1 ]
do
echo "Folder doesn't exist yet, create?"
read -p "Do you want to create the folder and continue backup job?(y/n)"
if [ "$REPLY" = "y" ]; then
echo "Creating folder for backup"
break
elif ["$REPLY" = "n"]; then
echo "Exiting program..."
exit
else
echo "Please enter 'y' or 'n'"
fi
done
fi
}
#////////////////MAIN///////////////////////////
#Clear screen and start program
clear
#prepare the fullDestPath variable
#e.g. /media/usbdisk/Data\ 280108/
fullDestPath=$fullDestPath$folderVar
echo "*************************************************"
echo "* *"
echo "* *"
echo "* *"
echo "* Welcome to Kinson's backup script(v0.1) *"
echo "* *"
echo "* *"
echo "* *"
echo "*************************************************"
echo $fullDestPath
while [ 1 ]
do #Loop the whole program until exit
echo -e "Please make a selection:\n"
showMenu
read CHOICE
case "$CHOICE" in
"1")# Backup (Daily Use)
backupData
;;
"2")#Backup and Check Differences (Takes a long time)"
backupData
checkData
;;
"3")#Quit
thankExit
;;
*)
echo "Please make a valid selection"
;;
esac
done
Currently I'm finishing up that "checkFolderExist" function...but it seems kinda uneccessary. Since there's only gonna be one Data xxxxxx folder there, but just thought that it'd be a good experience. And to check if "Data ?????" exists by using wildcards seems a little tacky.....any comments would be welcome, cheers :)
Kinson