LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2016, 01:14 PM   #1
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Rep: Reputation: Disabled
using getopts with flags to backup cronjobs


Hi, i am new to this forum so please let me know if i post in the wrong place.

I was tasked to create a script so we can backup cronjobs on the server.

suggested syntax:
./script_name.sh -m backup -p /path/to/backup/dir

m = method = backup or restore
p = path to backup or to restore to
u = (optional) jobs by user

i created the script, however, it is not working as expected and i continue to get the following error:
./getops_local.sh -m backup -p /home/test/backup
./getops_local.sh: line 55: syntax error near unexpected token `;;'
./getops_local.sh: line 55: ` ;;'


can anyone take a look and see if you can point me to the right direction?


below is the script that i created

Code:
#!/bin/bash

#bkuplocation="backups/$hostname"
bkuplocation="/systems/homes/cronjobs_bk/$hostname"

#Help function
function HELP {
  echo " Command line switches -m & -p are required, -u is optional."
  echo " -m  --Sets the value for option to backup or restore (backup/restore)."
  echo " -p  --Sets the path to backup from and restore to."
  echo " -u  --(OPTIONAL) Sets the user jobs to backup."
  echo " -h  --Displays this help message. No further functions are performed."\\n
  exit 1
}

#Check the number of arguments. If none are passed, print help and exit.
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
  HELP
fi

### Start getopts code ###

while getopts :m:p:u?h? OPTIONS; do
  case $OPTIONS in
    m) 
      method=$OPTARG
      ;;

    p) 
      location=$OPTARG
      ;;

    u)
      user=$OPTARG
      ;;

    h) HELP
       ;;

   \?) echo -e "Option $OPTARG not allowed."
       HELP
       #echo -e "Use -h to see the help documentation."
       #exit 2
       ;;
  esac
done

case $method in
  backup)
    if [$method = 'backup'] && if [$user ==""]; then
      cp "$location/*" "${bkuplocation}/$hostname";
    else 
      cp "$location/$user" ${bkuplocation};
    fi
  ;;

  restore)
    if [$method = 'restore'] && if [$user ==""]; then
      cp "${bkuplocation}/$hostname/*" ${location};
    else 
      cp "${bkuplocation}/$hostname/$user" ${location};
    fi
  ;;
  esac
done

shift $((OPTIND-1))

exit 0

Last edited by linuxnewbie0101; 06-21-2016 at 01:30 PM.
 
Old 06-21-2016, 01:23 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,459

Rep: Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053
Please put the code in [code][/code] tags to preserve formatting and prevent characters from being replaced by smiley faces.
 
Old 06-21-2016, 01:26 PM   #3
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
TY for the tip.
 
Old 06-21-2016, 01:36 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,459

Rep: Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053
The problem with the ";;" error is your if statements. They should look more like this:
Code:
if [ $method == 'backup' ] && [ $user == "" ]; then
Note the spaces around the brackets and equal signs, and you have an extra "if" in there.
 
1 members found this post helpful.
Old 06-21-2016, 02:21 PM   #5
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
The problem with the ";;" error is your if statements. They should look more like this:
Code:
if [ $method == 'backup' ] && [ $user == "" ]; then
Note the spaces around the brackets and equal signs, and you have an extra "if" in there.
Thank you for the quick response and keen eyes. yes this did resolve the immediate issue, however, i am now receiving the following error with changes for testing purpose,

bkuplocation="/tmp/test2/$hostname"

and the syntax i used to call is ./getops_local.sh -m backup -p /tmp/test

jfang@jfang-T530:~/test$ ./getops_local.sh -m backup -p /tmp/test
cp: cannot stat /tmp/test/*: No such file or directory

TY for all your help
 
Old 06-21-2016, 02:35 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,459

Rep: Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053
Looks like the files you're telling it to back up (/tmp/test/*) don't exist, so it's complaining when it tries to copy them to the backup location. It's not an error per-se, but if you want it to do something other than yell at the user for providing a location with no files you'll need to test for that case explicitly and do something special.
 
Old 06-21-2016, 03:15 PM   #7
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
implemented crontab to do some work.

Code:
#!/bin/bash

bkuplocation="/systems/homes/cronjobs_bk/$hostname"

#Help function
function show_help {
  echo " Command line switches -m & -p are required, -u is optional."
  echo " -m  --Sets the value for option to backup or restore (backup/restore)."
  echo " -p  --Sets the path to backup from and restore to."
  echo " -u  --(OPTIONAL) Sets the user jobs to backup."
  echo " -h  --Displays this help message. No further functions are performed."
  exit 1
}

#Check the number of arguments. If none are passed, print help and exit.
numargs=$#
if [ $numargs -eq 0 ]; then
  show_help
fi

# Start getopts code

while getopts :m:p:u?h? options; do
  case $options in
    m) 
      method=$optarg
      ;;
    p) 
      location=$optarg
      ;;
    u)
      user=$optarg
      ;;
    h) show_help
       ;;
   \?) echo -e "Option $optarg not allowed."
       show_help
       #echo -e "Use -h to see the help documentation."
       #exit 2
       ;;
  esac
done

case $method in
  backup)
    if [ $method == 'backup' ] && [ $user =="" ]; then
      for i in "/var/spool/cron/crontabs/*"; do 
        crontab -l -u $i > "${bkuplocation}/$hostname/";
      done
    else 
      crontab -l -u $user > "${bkuplocation}/$hostname/";
    fi
    ;;
  restore)
    if [ $method == 'restore' ] && [ $user =="" ]; then
      for i in "${bkuplocation}/$hostname/*"; do
        crontab $i; 
      done  
    else 
        crontab $user < "${bkuplocation}/$hostname/$user"; 
    fi
    ;;
esac
shift $((OPTIND-1))

exit 0

any thoughts and feedbacks?
 
Old 06-21-2016, 04:10 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
You could add an usage function with just one line resuming program invokation, like
Code:
Usage:
    program_name [-m -p -u -h]
Maybe, add an exit number parameter to the help function, this way when one wants to just see the help page, the program doesn't exit with error (1)

Don't see the relevance of shift $((OPTIND-1)) just before exit 0 there...

[edit]
I think you forget filenames for cron backups files here:
Code:
if [ $method == 'backup' ] && [ $user =="" ]; then
      for i in "/var/spool/cron/crontabs/*"; do 
        crontab -l -u $i > "${bkuplocation}/$hostname/";
      done
    else 
      crontab -l -u $user > "${bkuplocation}/$hostname/";
    fi

Last edited by keefaz; 06-21-2016 at 04:13 PM.
 
Old 06-22-2016, 08:56 AM   #9
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
update the script for review, for some reason the script completed without errors. but it didnt do anything.

also the bkuplocation is also a nas drive on the network.

Please help.
Code:
#!/bin/bash

bkuplocation="/systems/homes/cronjobs_bk/$host"
cron_srouce="/var/spool/cron/crontabs/*"

#Help function
function show_help {
  echo " Command line switches -m & -p are required, -u is optional."
  echo " -m  --Sets the value for option to backup or restore (backup/restore)."
  echo " -p  --Sets the path to backup from and restore to."
  echo " -u  --(OPTIONAL) Sets the user jobs to backup."
  echo " -h  --Displays this help message. No further functions are performed."
  exit 1
}

#Check the number of arguments. If none are passed, print help and exit.
numargs=$#
if [ $numargs -eq 0 ]; then
  show_help
fi

# Start getopts code
while getopts :m:p?u?h? options; do
  case $options in
    m) 
      method=$optarg
      ;;
    p) 
      location=$optarg
      ;;
    u)
      user=$optarg
      ;;
    h) show_help
       ;;
   \?) echo -e "Option $optarg not allowed."
       show_help
       ;;
  esac
done

shift $((OPTIND-1))

case $method in
  backup)
    if [ ! -d $bkuplocation ]; then
	mkdir $bkuplocation
    fi
    if [ -z $location ] && [ -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $i > "$bkuplocation";
      done
    elif [ ! -z $location ] && [ ! -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $i > "$location";
      done
    elif [ -z $location ] && [ ! -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $user > "$bkuplocation";
      done
    elif [ ! -z $location ] && [ -z $user ]; then
      crontab -l -u $i > "$location";
    else show_help
    fi
  ;;

  restore)
    if [ ! -d $bkuplocation ]; then
	echo "no backup found for $host"
    fi
    if [ -z $location ] && [ -z $user ]; then
      for i in $bkuplocation; do
        crontab $i; 
      done  
    elif [ ! -z $location ] && [ ! -z $user ]; then
      for i in $location; do 
        crontab -l -u $user < $location;
      done
    elif [ ! -z $location ] && [ -z $user ]; then
      for i in $location; do 
        crontab -l -u $i < $location;
      done
    elif [ -z $location ] && [ ! -z $user ]; then
      crontab -l -u $user < "$bkuplocation/*";
    else
      show_help
    fi
  ;;
esac

shift $((OPTIND-1))

exit 0
 
Old 06-22-2016, 09:17 AM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,459

Rep: Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053Reputation: 2053
How do you know it completed without errors and how do you know it didn't do anything?
Was this running in cron or did you run it yourself?

I suggest you take a step back from the script and just focus on the crux of what it's doing, because even ignoring the numerous logic errors in your if/elif blocks and for loops, neither the backup nor restore portions of your script are correct. Pick one example, eg:
Code:
crontab -l -u $user > "$location";
and
Code:
crontab -l -u $user < $location;
What is $user and what is $location? Pick an example value for each, and actually run those commands, neither of them will work correctly. Your backup is trying to dump text to a directory, which you can't do, and you can't pipe text into the stdin of "crontab -l" and expect it to do anything. You need to figure out how to tackle the actual function of the script before you can wrap it in if statements and for loops to do something useful.

Once you've tackled the actual backup/restore commands, you need to take a hard look at your if/elif blocks and for loops, because there are many problems there, eg:
1)
Code:
elif [ ! -z $location ] && [ -z $user ]; then
  crontab -l -u $i > "$location";
There is no for loop, therefore $i is undefined. I assume you meant $user.

2)
Code:
elif [ ! -z $location ] && [ ! -z $user ]; then
  for i in $cron_srouce; do 
    crontab -l -u $i > "$location";
  done
Why are you looping over $cron_srouce (which is misspelled btw) when they provided a user?

3)
Code:
if [ -z $location ] && [ -z $user ]; then
  for i in $bkuplocation; do
    crontab $i; 
  done
What is that supposed to do?


Also, bash does not require semi-colons at the end of lines like C does. You have that all over the place in the script, and while it won't hurt anything, it's unnecessary and the usage is inconsistent, which makes things look messy.

Last edited by suicidaleggroll; 06-22-2016 at 09:21 AM.
 
1 members found this post helpful.
Old 06-22-2016, 09:19 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Output files still missing, I am surprised no error raise on output when the script tries to redirect stdout in a directory

[edit]
Yes as suicidaleggroll said
Code:
 if [ -z $location ] && [ -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $i > "$bkuplocation";
      done
    elif [ ! -z $location ] && [ ! -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $i > "$location";
      done
    elif [ -z $location ] && [ ! -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $user > "$bkuplocation";
      done
    elif [ ! -z $location ] && [ -z $user ]; then
      crontab -l -u $i > "$location";
    else show_help
    fi

Last edited by keefaz; 06-22-2016 at 09:26 AM.
 
Old 06-22-2016, 09:28 AM   #12
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
the script was suppose to backup or restore for all or specific user.
bkuplocation is the designated location, however, -p can introduce new path location to backup and restore.

i ran the script and no errors, i checked the bkuplocation and was not able to see any files.

TY
 
Old 06-22-2016, 09:51 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,722

Rep: Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751Reputation: 1751
In addition

Is the NAS mounted by default?

Quote:
bkuplocation="/systems/homes/cronjobs_bk/$host"
$host is not defined.

[ -z string ] means true if string is blank. As stated go back and make sure your conditionals match your cases.

To simplify a bit don't use different variable names for the backup location.

Last edited by michaelk; 06-22-2016 at 09:53 AM.
 
Old 06-22-2016, 10:02 AM   #14
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
In addition

Is the NAS mounted by default?


$host is not defined.

[ -z string ] means true if string is blank. As stated go back and make sure your conditionals match your cases.

To simplify a bit don't use different variable names for the backup location.


yes the NAS is mounted by default,
$host is suppose to be $hostname as a directory of hostname
-z string does mean the string is blank blank as in optional flag.

TY
 
Old 06-22-2016, 10:03 AM   #15
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
updated script

Code:
#!/bin/bash

bkuplocation="/systems/homes/cronjobs_bk/$hostname"
cron_srouce="/var/spool/cron/crontabs/*"
hostname=$(hostname -s)

#Help function
function show_help {
  echo " Command line switches -m is required, -p & -u are optional."
  echo " -m  --Sets the value for option to backup or restore (backup/restore)."
  echo " -p  --(OPTIONAL) Sets the path to backup from and restore to."
  echo " -u  --(OPTIONAL) Sets the user jobs to backup."
  echo " -h  --Displays this help message. No further functions are performed."
  exit 1
}

#Check the number of arguments. If none are passed, print help and exit.
numargs=$#
if [ $numargs -eq 0 ]; then
  show_help
fi

# Start getopts code
while getopts :m:p?u?h? options; do
  case $options in
    m) 
      method=$optarg
      ;;
    p) 
      location=$optarg
      ;;
    u)
      user=$optarg
      ;;
    h) show_help
       ;;
   \?) echo -e "Option $optarg not allowed."
       show_help
       ;;
  esac
done

shift $((OPTIND-1))

case $method in
  backup)
    if [ ! -d $bkuplocation ]; then
	mkdir $bkuplocation
    fi
    if [ -z $location ] && [ -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $i > "$bkuplocation/$i"
      done
    elif [ ! -z $location ] && [ ! -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $user > "$location/$user"
      done
    elif [ -z $location ] && [ ! -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $user > "$bkuplocation/$user"
      done
    elif [ ! -z $location ] && [ -z $user ]; then
      for i in $cron_srouce; do 
        crontab -l -u $i > "$location/$i"
    else 
      show_help
    fi
  ;;

  restore)
    if [ ! -d $bkuplocation ]; then
	echo "no backup found for $host"
    fi
    if [ -z $location ] && [ -z $user ]; then
      for i in $bkuplocation; do
        crontab $i
      done  
    elif [ ! -z $location ] && [ ! -z $user ]; then
      for i in $location; do 
        crontab $user < "$location/$user"
      done
    elif [ ! -z $location ] && [ -z $user ]; then
      for i in $location; do 
        crontab $i < $location
      done
    elif [ -z $location ] && [ ! -z $user ]; then
      for i in $bkuplocation; do
        crontab $user < "$bkuplocation/$user"
    else
      show_help
    fi
  ;;
esac

shift $((OPTIND-1))

exit 0

Last edited by linuxnewbie0101; 06-22-2016 at 10:34 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to verify Cronjobs smilemukul Linux - Newbie 1 03-10-2011 02:57 PM
[SOLVED] cronjobs in ubuntu dinakumar12 Linux - Server 7 04-02-2010 05:31 AM
CronJobs error archey Linux - Newbie 4 08-22-2009 05:16 PM
Using cronjobs to mysql dump and scp backup to another box daiver Linux - General 4 11-10-2006 07:37 AM
Problem with cronjobs..... freakin'me Linux - General 7 02-11-2006 03:08 PM


All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration