#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Creating script & Cron file - Plz help


    Hey everyone, I was hoping someone can help me create a Cron file and script. I've tried reading through my Linux book but I am just lost here. I need to create a Cron file that will perform a Level 0 backup once per month, a Level 2 dump one day per week, and a Level 5 dump every day that neither a level 0 nor a level 2 dump is performed. Thanks for any help, it is greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    You have a couple of options: a cron entry for each form of backup, or a single entry that points to a script the decides what to do.
    The latter is probably going to be easier, assuming that you will want to start the different backups at the same time of day. How much leeway do you in deciding which day of the month is the Level 0 and which is the Level 2?
    If you can do something like 1st day of month is the Level 0 it will make things easier.
    So, you would have a script akin to:
    Code:
    dump_date=$(date "+%a %e")
    dump_weekday=$(echo "$dump_date" | awk '{print $1}')
    dump_daynum=$(echo "$dump_date" | awk '{print $2}')
    if [ $dump_daynum -eq 1 ]
    then
    # Do Level 0 dumps on first day of month
      Level=0
    else
      if [ "$dump_weekday" = "Fri" ]
      then
    # Do Level 2 dump seach Friday (unless also 1st of month)
         Level=2
      else
    # Else do a Level 5 dump
         Level=5
      fi
    fi
    # Do the required Level dump
    /sbin/dump -${Level}... your other options here
    dump_rc=$?
    if [ $dump_rc -eq 0 ]
    then
      echo "Level $Level dump ok"
    else
      echo "Level $Level dump falied, code $dump_rc"
    fi
    And in cron you'd have something like:
    Code:
    0 22 * * * /path/to/script/my_dump_script
    To start the dump every day at 22:00
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0
    Thanks for that SimonJM. This is something I need to do for class so it's not really limited to anything in particular, I posted the question word for word as it appears in the book. I am just clueless with this stuff as I had to miss the class when we went over this information and the Linux Redhat Fedora book is just horrible for explaining this stuff.

    Would you be able to break down your script into newb terms for me so I can try to understand it better? Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Ok, we can do that - you may want a Linux command prompt handy to type in man commands ...

    Here we set a variable (called dump_date) to the result of the date command - the $( ... ) means run the command inside the brackets and return the result. So the command we run is date (try: man date) which, as the name suggests, does things with dates. The style of command we have here is to return 'information snippets' about the current date/time. The format we ask the date command to use %a followed by a space, then %e. As you will in the man page for the command those result in the abbreviated day name and the day of the month. So, if today was Wednesday 17th August we would see "Wed 17" being returned. Thus the variable we specify is given the value of Wed 17.
    Code:
    dump_date=$(date "+%a %e")
    We do that all in one command on the very, very, very small chance that the script may at a future date be run at a time very close to midnight, to prevent two uses of the date command spanning the change in day. Paranoia I know, but I have seen weirder timing issues! But now we have the information we need, we also need to isolate it. Here we arrange for another variable to be given the value created as a result of running another command. Inside the $( .. ) this time we have an echo command which does what it says on the name ... it echoes (or prints) what you pass to it. Here we have given it the previous variable (which is "Wed 17" in our ongoing exmaple). Just doing that alone would just end up in the new variable getting the same value as the old one, which is no use. Thus we pass on (the *nix term is pipe) that output of the echo command to the awk command using the pipe (the | character). What the pipe does is, in brief, take the output of one command and pass it on to the input of another command. So, the awk command, which usually acts on a file as it's input is given the output of our echo as it's input. The awk command (man awk) is a very powerful program which allows simple (and complex!) processing of files and the records within those files. By default awk will see each white-space delimited piece of text on each line as a separate field. With this use we are just asking awk to print the first field - which is Wed. Thus, after all this our variable will be given the value Wed.
    Code:
    dump_weekday=$(echo "$dump_date" | awk '{print $1}')
    Pretty much the same thing, but we ask awk to only print the 2nd field.
    Code:
    dump_daynum=$(echo "$dump_date" | awk '{print $2}')
    Here we get to some logic. In effect we have three levels of condition. Do we need run a level 0 dump. That is the main concern, so it's the firts test we do. I have arbitrarily decided that the 1st of each month is a good (and easy to identify!) time to do this. Thus we check the variable we populated with the day of the month. Rather obviously, if that variable contains 1 it is the 1st day of the month! In that case we make arrangements to do a Level 0 dump.
    Code:
    if [ $dump_daynum -eq 1 ]
    then
    # Do Level 0 dumps on first day of month
      Level=0
    If it is not the first of th emonth we will want to either do a level 2 (which is our next most important dump) so we check to see if it is a Friday, but testing the variable holding our abbreviated day name in for being equal to Fri. If it is, we arrange for a Level 2 dump. Remember, the first day of the month is a Friday we won't do this - the logic above identifying the 1st of the month evaluates as true first so we will nto process this section of code.
    Code:
    else
      if [ "$dump_weekday" = "Fri" ]
      then
    # Do Level 2 dump seach Friday (unless also 1st of month)
         Level=2
    Now, if it's not the 1st of the month and it also not a Friday then it will be a 'normal' Level 5 dump, so make the arrangements
    Code:
      else
    # Else do a Level 5 dump
         Level=5
      fi
    fi
    With each section of the condition above we set a variable called Level to 0, 2 or 5 depending on requirement. That enables us to have just a single dump (man dump) command, which will cut down on possible errors if what is to be dumped is changed - there will only be one place to edit it, not three.
    Code:
    # Do the required Level dump
    /sbin/dump -${Level}... your other options here
    By convention *nix commands return an exit code of 0 if they work, and non-zero if they don't. Some have a range of values which further indicate the type of fail. here we set a variable to the system-supplied variable of $? which holds the last exit code produced. We do that because any *nix 'command', such as an if will update the value in $? meaning that we would lose the value immediately after testing it! So, we save it, test and report on the result. Maybe even spelling failed correctly! (whoops!)
    Code:
    dump_rc=$?
    if [ $dump_rc -eq 0 ]
    then
      echo "Level $Level dump ok"
    else
      echo "Level $Level dump falied, code $dump_rc"
    fi
    The cron file entry I generally create using crontab -e (man crontab) which edits the cron file for the current user. The columns in the file tell cron when to run the command, with * being a wild card meaning any. The order of the columns is as shown in the man pages, but in essence:
    Code:
    0 22 * * * /path/to/script/my_dump_script
    means run the command /path/to/script/my_dump_script on every day of every month as 0 minutes past 22 hours. So every day at 10pm the script will be executed.

    I hope that is a good explanation?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0
    SimonJM: "I hope that is a good explanation?"
    Hell yeah! That guy/gal should really thank you, that is one of the best explanations I have ever seen on *nix stuff. In fact, it is why I just signed up, if there are any more people like you around,maybe I will finally get the whole system behind it, rather than spewing rote formulas. Pity that you just did Spyder's homework for him/er, tho', you know you won't see him/er back until next assignment...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Thank you. Sometimes I drop into 'teacher-mode' ...
    I have become used to getting minimal feedback (apart from one person who seemed not to like my questions!) so it doesn't really bother me that much, though it does warm the old cockles of the heart foe someone to say 'thanks', so again, thank you.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo