LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-23-2016, 01:47 PM   #46
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled

so now i went back to the long code to include $locations and $users in my case statement and the code is now working. i will continue to work on it the long way instead of simplifying them for now.

one quick question,

when i backup the user jobs, i named them $host-$user,

now the question is when im trying to restore them, how do i remove the $host- part of the backup file?

TY
 
Old 06-23-2016, 01:53 PM   #47
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
In Bash when you assign a value to a variable, you don't put a '$' in the variable name, I mean

location="value" and not $location="value"
 
1 members found this post helpful.
Old 06-23-2016, 01:55 PM   #48
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
In Bash when you assign a value to a variable, you don't put a '$' in the variable name, I mean

location="value" and not $location="value"
TY, will keep that in mind.
 
Old 06-23-2016, 01:57 PM   #49
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,028

Rep: Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851
Oops. My excuse is that I've been writing php code...
 
Old 06-23-2016, 02:08 PM   #50
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Quote:
Originally Posted by linuxnewbie0101 View Post

now the question is when im trying to restore them, how do i remove the $host- part of the backup file?

TY
Code:
file="hostname-user"
user=${file##*-}
See:
https://www.gnu.org/software/bash/ma...eter-Expansion
Code:
${parameter#word}
${parameter##word}

    The word is expanded to produce a pattern just as in filename expansion 
    (see Filename Expansion). If the pattern matches the beginning of the 
    expanded value of parameter, then the result of the expansion is the expanded 
    value of parameter with the shortest matching pattern (the ‘#’ case) or the 
    longest matching pattern (the ‘##’ case) deleted.
edit: use ## rather than # as the hostname could contain '-'

Last edited by keefaz; 06-23-2016 at 02:12 PM.
 
1 members found this post helpful.
Old 06-23-2016, 02:22 PM   #51
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Code:
file="hostname-user"
user=${file##*-}
See:
https://www.gnu.org/software/bash/ma...eter-Expansion
Code:
${parameter#word}
${parameter##word}

    The word is expanded to produce a pattern just as in filename expansion 
    (see Filename Expansion). If the pattern matches the beginning of the 
    expanded value of parameter, then the result of the expansion is the expanded 
    value of parameter with the shortest matching pattern (the # case) or the 
    longest matching pattern (the ## case) deleted.
edit: use ## rather than # as the hostname could contain '-'
going to take a few mins to digest this info.

TY
 
Old 06-23-2016, 02:30 PM   #52
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
so with keefaz suggestion could i do

declare:

Code:
file="$hostname-$user"
userfile=${file##*-}
and under the first if statement in restore

do this?

Code:
    if [[ -z $location && -z $user ]]; then
      cd $bkuplocation
      for i in "$host-*"; do
        crontab $i < $bkuplocation/$userfile
      done
TY
 
Old 06-23-2016, 02:53 PM   #53
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,028

Rep: Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851
You changed your backup from $bkuplocation/$host/$user to $bkuplocation/$host-$user?

What you had before looked ok for the conditionals. As suggested have you manually tested restoring a crontab?

No, those statements have to be in the loop. (sorry keefax for replying)

Last edited by michaelk; 06-23-2016 at 02:59 PM.
 
Old 06-23-2016, 02:59 PM   #54
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Quote:
Originally Posted by linuxnewbie0101 View Post
so with keefaz suggestion could i do

declare:

Code:
file="$hostname-$user"
userfile=${file##*-}
and under the first if statement in restore

do this?

Code:
    if [[ -z $location && -z $user ]]; then
      cd $bkuplocation
      for i in "$host-*"; do
        crontab $i < $bkuplocation/$userfile
      done
TY
just echo the crontab line, you will notice the $i value and the $userfile value...
Code:
for i in "$host-*"; do
        echo "crontab $i < $bkuplocation/$userfile"
 done
 
1 members found this post helpful.
Old 06-24-2016, 08:19 AM   #55
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
how can i set default values to either -m -p or -u but its not required when running the script?


as far as i can tell, my -p ($location) is already defined to my $bkuplocation. and -u ($user) if not provided, it will just backup ALL users.

how do i set -m to default to backup or restore if is not provided?

and what can i do to implement error checking or variable validation / sanitization?

basically the requirement just change to if the script can run by itself and backup all users cronjob or with option flags.

TY

Last edited by linuxnewbie0101; 06-24-2016 at 08:28 AM.
 
Old 06-24-2016, 08:31 AM   #56
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Looks like you're overthinking this

When you set values to global variables and don't modify them, they stay
I mean you have a default backup dir set on top of script, if you don't change the value with location option, it will default to backup dir value. Same for method, if you set a default method and don't change it, it will stay...
 
Old 06-24-2016, 08:34 AM   #57
michaelk
Moderator
 
Registered: Aug 2002
Posts: 16,028

Rep: Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851Reputation: 1851
Just set a default value for $method like you do for bkuplocation.

Validate paths and users.
 
Old 06-24-2016, 08:39 AM   #58
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks michaelk, i've got it working.

can anyone point me to some reading on how to implement error checking?

so far i only have -e set

TY

Last edited by linuxnewbie0101; 06-24-2016 at 09:27 AM.
 
Old 06-24-2016, 11:16 AM   #59
linuxnewbie0101
Member
 
Registered: Dec 2015
Posts: 46

Original Poster
Rep: Reputation: Disabled
So i think i've figure this out. Can someone take a look and provide me with some feedback?

TY

Code:
#!/bin/bash -e

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

location=$bkuplocation
method='backup'
#comment the above 2 lines if -m is a require at later time

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

#Uncomment this block to ensure required arguments
#=======================================================================
#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

#Error checking for $bkuplocation and $location
if [[ ! -d $bkuplocation ]]; then
  echo " ERROR: default backup location does not exist. Please make sure $bkuplocation is available or use -p to specify backup location."
  exit 1
fi
if [[ ! -z $location && ! -d $location]]; then
  echo " ERROR: Specified location does not exist. Please provide another location or use default location."
  exit 1
fi

#Error checking for user backup
if [[ $method='backup' && $location=$bkuploacation ]]; then
  if [[ ! -z $user && $user != "cat $cron_source | grep $user" ]]; then
    echo " ERROR: no cronjob found for $host $user."
    exit 1
  fi
fi
if [[ $method='backup' && $location != $bkuplocation ]]; then
  if [[ ! -z $user && $user != "cat $location | grep $user" ]]; then
  echo " ERROR: no crontab jobs found on specified location for $host $user."
  exit 1
fi

#Error checking for user restore
if [[ $method='restore' && $location=$bkuploacation ]]; then
  if [[ ! -z $user && $host-$user != "cat $bkuplocation | grep $host-$user" ]]; then
    echo " ERROR: no cron backup found for $host $user."
    exit 1
  fi
fi
if [[ $method='restore' && $location != $bkuplocation ]]
  if [[ ! -z $user && $user != "cat $location | grep $user" ]]; then
  echo " ERROR: no backup crontab jobs found for $host $user."
  exit 1
fi

#parameter expansion for $host-$user
file="$hostname-$user"
userfile=${file##*-}

case $method in
  backup)
    if [[ -z $location && -z $user ]]; then
      cd $cron_source
      for users in *; do 
        crontab -l -u $users > $bkuplocation/$host-$users
      done
    elif [[ ! -z $location && ! -z $user ]]; then
        crontab -l -u $user > $location/$host-$user
    elif [[ -z $location && ! -z $user ]]; then
        crontab -l -u $user > $bkuplocation/$host-$user
    elif [[ ! -z $location && -z $user ]]; then
      cd $cron_source
      for users in *; do 
        crontab -l -u $users > $location/$host-$users
      done
    else 
      show_help
    fi
  ;;

  restore)
    if [[ -z $location && -z $user ]]; then
      cd $bkuplocation
      for users in "$host-*"; do
        crontab $users < $bkuplocation/$userfile
      done  
    elif [[ ! -z $location && ! -z $user ]]; then
        crontab $user < $location
    elif [[ ! -z $location && -z $user ]]; then
      cd $location
      for users in *; do 
        crontab $users < $location
      done
    elif [[ -z $location && ! -z $user ]]; then
        crontab $user < $bkuplocation/$host-$user
    else
      show_help
    fi
  ;;
esac

exit 0

Last edited by linuxnewbie0101; 06-24-2016 at 11:18 AM.
 
Old 06-24-2016, 11:34 AM   #60
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
Code:
#Error checking for $bkuplocation and $location
if [[ ! -d $bkuplocation ]]; then
  echo " ERROR: default backup location does not exist. Please make sure $bkuplocation is available or use -p to specify backup location."
  exit 1
fi
if [[ ! -z $location && ! -d $location]]; then
  echo " ERROR: Specified location does not exist. Please provide another location or use default location."
  exit 1
fi
You make 2 tests for backup location dir, this could be shortened to one test: if -p option is set, then assign its value to $bkuplocation
After that, you only have to deal with $bkuplocation, the backup directory

Code:
#Error checking for user backup
if [[ $method='backup' && $location=$bkuploacation ]]; then
  if [[ ! -z $user && $user != "cat $cron_source | grep $user" ]]; then
    echo " ERROR: no cronjob found for $host $user."
    exit 1
  fi
fi
Wrong, you can't cat $cron_source, it's a directory and syntax for pipe is incorrect (inside "", won't execute but will test equality with "cat $cron_source | grep $user" string)

Last edited by keefaz; 06-24-2016 at 11:38 AM.
 
1 members found this post helpful.
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 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