Examle of helpful utilities

Discussion in 'Linux' started by jimkd1yv, Jan 4, 2011.

  1. jimkd1yv

    jimkd1yv Well-Known Member

    Joined:
    Jul 6, 2010
    Messages:
    64
    Likes Received:
    0
    I realized that that I didn't post the utility that I use to perform periodic backups on my laptop. I am posting it here. The purpose is not to show the backup utility, but rather the scheduling technique.

    The laptop is used very sporadically; some days, I start it several times each day, other times it will go several days without receiving the warmth of human touch. My objective is to backup critical data when I log onto the machine, but in no case to do it more than once per day. In addition, I don't want the backup to start immediately, in case I am doing very important work as soon as I start up the machine.

    Following is the code, with hopefully enough comments to make it understandable. If you want any clarification, feel free to ask.

    Jim R

    The file is in:
    /etc/init.d/zz-backup2l
    which means it gets started every time the machine starts Linux.

    #! /bin/bash
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides: backuo
    # Required-Start: $local_fs
    # Required-Stop: $local_fs
    # Default-Start: 2 3 4 5
    # Default-Stop:
    # Short-Description: run backup2l after n minute sleep - only runs once in 24 hours
    # Description: backup2l
    ### END INIT INFO

    # Author: jfr
    # find -mtime +0 finds the file at least 24 hours old. +1 would be at least 48 hours old.

    #
    MARK="/var/backup2l_last_run" # Season to taste, but not in /tmp

    # Can we run yet?
    # Set variable GOTCHA to result of find command. If the file from the last run
    # is less than 24 hours old, GOTCHA will be 1.
    # test the results in GOTCHA. If the result shows file < 24 hours old, exit with rc 0
    test 0 -lt $? -o -n "$GOTCHA" || exit 0

    # else, it should be safe to row run your code...

    echo gonna run this code

    # sleep in case you want to cancel this job, or are doing something important for 9 minutes
    sleep 9m

    # do the actual program that you want run run
    echo Subject: Backup2l `date` > /var/log/backup2l_mini.log
    ! which backup2l > /dev/null || nice -n 19 backup2l -b >>/var/log/backup2l_mini.log
    myrc=$?
    if [ "$myrc" = "0" ] ; then
    # All done
    touch "$MARK" # Only on successful completion
    fi

    # now mail the results to root
    sendmail root </var/log/backup2l_mini.log

    exit 0
     
  2. snh

    snh Well-Known Member
    Director

    Joined:
    Oct 6, 2008
    Messages:
    137
    Likes Received:
    0
    That's a very tidy, compact piece of code. Thanks for posting.
     

Share This Page