1

I made a script which executes successfully online backup when it is run manually by the instance owner. Script is the following (two lines originally).

 #!/bin/bash
    /opt/ibm/db2/V9.7/bin/db2 backup database maxdb71 online to "/data/archiveBackup"
 WITH 2 BUFFERS BUFFER 1024 PARALLELISM 1 INCLUDE LOGS WITHOUT PROMPTING;

As I said when I execute it manually by the instance owner it works. I put it in the instance owner crontab. crontab -l lists this script, and date/time on the server is OK.

Why the script is not executing the online backup when it works OK when it is run manually?

Thank you

  • If you don't provide output people can only guess as to why the script is failing. – Ian Bjorhovde Feb 12 '15 at 19:26
  • I'll guess that you need to execute as instance owner. You may need to su into the instance owner when executing your script. – Chris Aldrich Feb 12 '15 at 19:30
4

Chances are that the reason your script runs from the command line but fails from cron is that your DB2 environment is already initialized (i.e. via your .profile) for the interactive session, where it is not when called by cron.

Here's a suggested modification:

 #!/bin/bash
 #

 # Setup DB2 Environment
 . $HOME/sqllib/db2profile

 # Back up Database
 db2 -v "backup database maxdb71 online to /data/archiveBackup INCLUDE LOGS WITHOUT PROMPTING"

A few comments:

  • This will work if you're executing it via cron for the instance owner. However, because it depends on the $HOME environment variable you would need to use the actual path to the instance owner's home directory if you wanted to run this via cron as a different user.

  • You should not reference binaries in /opt/ibm/db2/..., you should be using the binaries from the DB2 instance. Because the script initializes the environment from the db2profile, the db2 command will be in the script's $PATH.

  • I removed some of the options from your backup command as these are not necessary. DB2 is very intelligent when it comes to selecting run-time values for these, so you're likely to get worse backup performance if you override the defaults.

  • When you execute the script from cron, make sure you capture the output in a log file. It will be very useful for debugging purposes. Example crontab:

    00 23 * * * /path/to/script/backup.sh > /path/to/script/backup.log 2>&1

  • Thank you Ian it works. But I am still little confused with solution. What actually did you do with this db2profile? I haven't invoked it previously. Thank you very much once again for your great support – Dejan Feb 12 '15 at 20:17
  • It's important to source db2profile because it sets all of the environment variables needed to use the DB2 Command Line Processor. – Fred Sobotka Feb 16 '15 at 19:26
  • @Dejan You may not have sourced the db2profile explicitly before, but that is because it is already sourced when you log in (via your ~/.profile or ~/.bashrc) – Ian Bjorhovde Feb 18 '15 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.