Lets start with an important warning: Setting a password as an option to a script has security implications:
- The password will be visible in the process list while running the script,
- The password will be visible in the .bash_history file.
Unless this is an exercise or run exclusively on your local, private machines I would strongly advise against doing this.
Limit your backup modes to solutions that do not require the user to provide a password. Using cp and passwordless scp are 2 examples that are safe to use. Cifs and some ftp clients (ncftp comes to mind) can also be set up in a safe way.
That said: Have a look at the following, which concentrates on using getops and option/argument handling. It is obviously a rough unfinished example, but it will hopefully give you an idea on how to proceed with your script.
Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# getopts - example
# -------------------------------------------------------------------------- #
# set defaults:
legalModes='(cp|ftp|gvfs|scp|smb)'
pwdModes='(ftp|gvfs|smb)'
backupMode=""
remoteHost=""
sourcePath=""
targetPath=""
localUser=$(id -un)
remoteUser=""
# -------------------------------------------------------- #
# functions
# show help
function showHelp() {
echo "
usage: $0 options
Options:
-m : mode # legal modes: cp, ftp, gvfs, scp and smb
-h : host
-s : source directory
-t : target directory
-l : local user
-u : remote user
-H : show help.
"
exit 0
}
# use cp
function useCp() {
[[ ! -d "${targetPath}" ]] && { echo "Target dir doesn't exist" ; exit 1 ; }
echo "do cp action here "
}
# use ftp
function useFtp() {
[[ -z "${remoteUser}" ]] && { echo "No remote user provided" ; exit 1 ; }
echo "do ftp action here"
}
# -------------------------------------------------------- #
# main
# all options, except -H, require an argument
while getopts "m:h:s:t:l:u:p:H" OPTS
do
case ${OPTS} in
m) backupMode=${OPTARG}
[[ "${backupMode}" =~ $legalModes ]] || \
{ echo "No legal mode selected" ; exit 1; }
;;
h) remoteHost=${OPTARG} ;;
s) sourcePath=${OPTARG} ;;
t) targetPath=${OPTARG} ;;
l) localUser=${OPTARG} ;;
u) remoteUser=${OPTARG} ;;
p) remotePwd=${OPTARG} ;;
H) showHelp ;;
\?) exit 1 ;;
esac
done
# show options used
echo ""
echo "Selcted options are:"
echo " (m) backupMode : ${backupMode}"
echo " (h) remoteHost : ${remoteHost}"
echo " (s) sourcePath : ${sourcePath}"
echo " (t) targetPath : ${targetPath}"
echo " (l) localUser : ${localUser}"
echo " (u) remoteUser : ${remoteUser}"
echo " (p) remotePwd : ${remotePwd}"
echo ""
# needed for all actions:
# - is a mode provided
[[ -z "${backupMode}" ]] && { echo "Mode not provided." ; exit 1; }
# - are source and target provided
[[ -z "${sourcePath}" ]] && { echo "Source dir not provided." ; exit 1 ; }
[[ -z "${targetPath}" ]] && { echo "Target dir not provided." ; exit 1 ; }
# - is source present
[[ ! -d "${sourcePath}" ]] && { echo "Source dir doesn't exist" ; exit 1 ; }
# needed for remote actions
[[ -z "${remoteHost}" && "${backupMode}" != "cp" ]] && \
{ echo "No host provided" ; exit 1; }
# needed for remote actions (except for passwordless modes)
if [[ -n "${remoteUser}" && "${backupMode}" =~ $pwdModes ]]
then
[[ -z "${remotePwd}" ]] && { echo "No password provided" ; exit 1; }
fi
# do action for selected mode
case ${backupMode} in
cp) useCp ;;
ftp) useFtp ;;
gvfs) echo "useGvfs" ;;
scp) echo "useScp" ;;
smb) echo "useSmb" ;;
esac
exit 0
# -------------------------------------------------------------------------- #
# End
The above script is safe to run, it doesn't perform any actual backup actions, it just echo's information.