lauching a task before backup

Asked by cedric on 2013-08-23

Hello,
What is the best way to launch a few commands before the backup starts ? I need to unmount and remount a volume.
Thank you for your help.
Cédric

Question information

Language:
English Edit question
Status:
Answered
For:
sbackup Edit question
Assignee:
No assignee Edit question
Last query:
2013-08-23
Last reply:
2013-09-18
Anton (feenstra) said : #1

I have something similar (I want to check which network my laptop is on, and if a succesful backup was already made this day), so I created a script in /usr/share/sbackup (mine is called 'retry_backup' but that doesn't matter).
To run it daily, I did:

$ sudo ln -s /usr/share/sbackup/retry_backup /etc/cron.daily/sbackup

(I also run it from /etc/pm/sleep.d/20_sbackup to try a backup when my laptop wakes up from suspend, and from /etc/network/if-up.d/sbackup to try a backup whenever eth0 comes online).

After checking what needs checking, /usr/share/sbackup/retry_backup starts /usr/share/sbackup/sbackup-launch to start the actual backup.

(I'll attach the three scripts separately, if possible, otherwise I'll just post them.)

Alternatively, you could modify /usr/share/sbackup/sbackup-launch to insert the commands you need.

Anton (feenstra) said : #2

/usr/share/sbackup/retry_backup:

#!/bin/bash
#
# retry running Sbackup if previous attempt failed
# used by pm to run at thaw/resume

# find config file and read some variables
my_dir=`dirname $0`
if [ -x $my_dir/get_conf ]; then
    . $my_dir/get_conf
else # get from default location
    . /usr/share/sbackup/get_conf
fi

# find backup schedule and check with latest succesful backup:
if [ -x /usr/share/sbackup/check_backup_schedule ]; then
    /usr/share/sbackup/check_backup_schedule
    if (( $? != 1 )); then
 # no backup needed
 exit 0
    fi
fi

# check ping to backup host
# strip username, if needed:
buhost=`echo $buhost | sed 's/^.*@//'`
echo "Checking ping average round-trip time (rtt) to $buhost"
# get average ping time in micro seconds (so it can be integer, not real):
ping=`ping -c 10 $buhost | tail -1 | awk -F= '{print $2}' | awk -F/ '{print $2*1000}'`
ping_max=1000
if [ $ping -gt $ping_max ]; then
    echo "Rtt is too slow ($ping; max $ping_max), not making backup now!"
    exit 0
fi

for sbackup in /usr/share/sbackup/sbackup-launch /usr/bin/sbackup /usr/sbin/sbackupd; do
    if [ -x $sbackup ]; then
 echo "Launching $sbackup"
 $sbackup
 exit 0
    fi
done

#last line

Anton (feenstra) said : #3

/etc/pm/sleep.d/20_sbackup

#!/bin/bash
case "$1" in
    thaw|resume)
 # start backup 5 minutes in the future to allow network to come up:
        echo /usr/share/sbackup/retry_backup | at now + 5 min >/dev/null
        ;;
    *)
        ;;
esac
exit $?

Anton (feenstra) said : #4

/etc/network/if-up.d/sbackup

#!/bin/sh
# If eth0 goes up, see if we can and should make a backup now:
if [ "$IFACE" = "eth0" ]; then
        # start backup 5 minutes in the future to allow network to come up:
        echo /usr/share/sbackup/retry_backup | at now + 5 min >/dev/null
fi
exit $?

Anton (feenstra) said : #5

p.s., the scripts work for me. They should work for you, too, but I cannot guarantee that. Please just ask if you run into problems.

Jean-Peer Lorenz (peer.loz) said : #6

In sbackup 0.11.5 and newer support for Pre/Post backup script hooks was added:

  It is completely optional. To use it, please add following options to your configuration file:
  ...
  [hooks]
  pre-backup = <path to pre-backup script>
  post-backup = <path to pre-backup script>
  ...
  You only need to specify scripts that should run, just leave it out if not
  required! Please note, the script must be executable (chmod +x) and will be
  run with same privileges as your backup process. So, be careful!

Anton (feenstra) said : #7

That's nice to know!

Does sbackup read the exit status of the pre-backup script, so the script could allow or block starting of the backup?
Likewise, it would be good if the post-backup script could read/know the exit status of sbackup.

(Ubuntu LTS - 12.04 - is providing sbackup 0.11.4)

Jean-Peer Lorenz (peer.loz) said : #8

Well, the result/exit code of the scripts are checked against 0. If a script fails, the whole backup will fail/not start. Post-backup scripts are being run regardless of the 'exit' status of the backup so far. Here is the corresponding code snippet:

   def do_hook(self, hookname):
        """Runs scripts optionally defined in section 'hooks' of
        configuration file. Currently defined are
        * pre-backup - before preparation of backup starts
        * post-backup - after completion and finishing of backup

        """
        #LP #173490
        import commands
        hooks = None
        if self.config.has_option('hooks', hookname):
            hooks = str(self.config.get('hooks', hookname)).split(",")

        if hooks is not None:
            self.logger.info(_("Running of hooks: %s") % hookname)
            for hook in hooks:
                result = commands.getstatusoutput(hook)
                if( 0 != result[0]):
                    raise exceptions.HookedScriptError(\
                      "Hook %s returned error: '%s' (exit code=%s)" % (hookname,
                                                                    result[1],
                                                                    result[0]))

Please feel free to file an enhancement bug report which details the desired behaviour. Thanks.

DiegoRivera (diego-rivera) said : #9

My only comment would be that the "post" backup script should at least be informed of the exit status of the backup process (i.e. backup succeeded or failed) so the script can react accordingly.

Otherwise, this functionality is exactly what I'm looking for. My need is to inhibit PM-suspend while the backup is running, but un-inhibit it once the backup is completed so the machine can be left taking a full backup (which takes ~45 minutes to complete), without concern of whether it will suspend midway and interrupt the backup. However, once it's done, the machine will suspend when appropriate.

Can you help with this problem?

Provide an answer of your own, or ask cedric for more information if necessary.

To post a message you must log in.