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 |
Please put the code in [code][/code] tags to preserve formatting and prevent characters from being replaced by smiley faces.
|
TY for the tip.
|
The problem with the ";;" error is your if statements. They should look more like this:
Code:
if [ $method == 'backup' ] && [ $user == "" ]; then |
Quote:
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 |
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.
|
implemented crontab to do some work.
Code:
#!/bin/bash any thoughts and feedbacks? |
You could add an usage function with just one line resuming program invokation, like
Code:
Usage: 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 |
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 |
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"; Code:
crontab -l -u $user < $location; 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 2) Code:
elif [ ! -z $location ] && [ ! -z $user ]; then 3) Code:
if [ -z $location ] && [ -z $user ]; then 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. |
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 |
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 |
In addition
Is the NAS mounted by default? Quote:
[ -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. |
Quote:
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 |
updated script
Code:
|
All times are GMT -5. The time now is 10:12 AM. |