In this post I’m going to show you how to do SAP DB2 databases backup on Linux with Veeam. I came across a SAP DB2 installation during one of my last projects and wanted to share with you how to backup these databases.
Introduction on pre-freeze and post-thaw scripts
Since there is no native integration of SAP DB2 in Veeam, we need to use pre-freeze and post-thaw scripts within the guest processing options.
Pre-freeze and post-thaw scripts are being used, when an application does not support VSS for example. The pre-freeze script is used to quiesce the VM file system or the application prior Veeam triggers a VMware snapshot. After the VMware snapshot is created, the post-thaw script triggers and brings the VM and the corresponding application back to the initial state.
How do the scripts work ?
Pre-freeze and post-thaw scripts are used on backup jobs, replication jobs and VM copy jobs.
Furthermore, you need to create the scripts beforehand. When the specific job starts, Veeam uploads the script to the guest operating system of your VM and executes it with the credentials you specified in the “Guest OS credentials” section of the job settings. Keep that in mind, as Veeam copies the script to the “admin$” share over the network or by using VIX API to execute them from there (on Windows). So your account needs to have sufficient permissions.
For linux, the scripts are copied over SSH or the VIX API to “/tmp”.
In addition, make sure that your scripts return a “0” when they execute, because Veeam expects that as successful. Furthermore, the default time period for script executions is 10 minutes.
For Microsoft Windows, the folowing script file formats are supported: “EXE, BAT, CMD, WSF, JS, VBS, and PS1”
For Linux, Veeam supports only shell scripts in the “SH” file format.
In the picture below you can see where to add those scripts for guest processing operations.
The process itself is very well explained in the Veeam Helpcenter. Make sure to have a look there !
How to backup SAP DB2 on Linux with pre-freeze and post-thaw scripts
To process the SAP DB2 with a VMware Veeam job, we need to suspend the SAP DB2 logwriter, prior of a VM snapshot. To achieve this, we need to use the db2 binary of the SAP DB2 database to suspend the logwriter and afterwards resume it again.
If you are running SAP DB2 on Windows, then there is already a pre-freeze and post-thaw example available on the Veeamhub. However, this post focuses on a pre-freeze and post-thaw script for Linux systems.
Here is a pre-freeze and post-thaw script example for backing up SAP DB2 databases on Linux. Make sure to test this on a test environment before you roll this out into production. Furthermore, this script (as all of any community script) comes with no warranty. Originally I found this example on this forum and modified it a bit to suit me well with Veeam: https://community.hitachivantara.com/s/article/db2-freeze-thaw-quiesce-and-unquiesce-scripts-for-hitachi-virtual-infrastructure-integrator
Before using the scripts, make sure to adjust it to your needs.
With adjusting, I mean selecting a path where the user you are using is able to write log files to, and the correct database name indicated in the script with “YOUR_DATABASE_NAME_HERE”.
I also uploaded those scripts to Github, because I’m maintaining all my scripts there.
Pre-Freeze Script for SAP DB2 databases backup
#!/bin/sh
# MIT License
# Copyright (c) 2021 Falko Banaszak
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
export INSTHOME=/SAP/DB2/PATH/TO/bin
export LOGHOME=/PATH/TO/A/FOLDER/TO/WRITE/LOGS
date >> $LOGHOME/veeam_write_suspend.log
echo "----START SUSPEND----" >> $LOGHOME/write_suspend.log << EOF
EOF
# Connect to your desired database
$INSTHOME/db2 connect to YOUR_DATABASE_NAME_HERE >> $LOGHOME/veeam_write_suspend.log
echo "+++" >> $LOGHOME/veeam_write_suspend.log << EOF_1
EOF_1
# Suspend the Logwriter for your desired database
$INSTHOME/db2 set write suspend for database >> $LOGHOME/veeam_write_suspend.log
echo "+++" >> $LOGHOME/veeam_write_suspend.log << EOF_2
EOF_2
$INSTHOME/db2pd -db YOUR_DATABASE_NAME_HERE -dbcfg | grep 'Database is in write suspend state' >> $LOGHOME/veeam_write_suspend.log
echo "----END----" >> $LOGHOME/veeam_write_suspend.log << EOF_3
EOF_3
Post-Thaw Script for SAP DB2 databases backup
#!/bin/sh
# MIT License
# Copyright (c) 2021 Falko Banaszak
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
export INSTHOME=/SAP/DB2/PATH/TO/bin
export LOGHOME=/PATH/TO/A/FOLDER/TO/WRITE/LOGS
date >> $LOGHOME/veeam_write_resume.log
echo "----START RESUME----" >> $LOGHOME/veeam_write_resume.log << EOF
EOF
$INSTHOME/db2 connect to YOUR_DATABASE_NAME_HERE >> $LOGHOME/veeam_write_resume.log
echo "+++" >> $LOGHOME/veeam_write_resume.log << EOF_1
EOF_1
$INSTHOME/db2 set write resume for database >> $LOGHOME/veeam_write_resume.log
echo "+++" >> $LOGHOME/veeam_write_resume.log << EOF_2
EOF_2
echo "----END----" >> $LOGHOME/veeam_write_resume.log << EOF_3
EOF_3
General Advice while using Pre-Freeze and Post-Thaw scripts within Linux
- Due to the fact, that the script gets copied to “/tmp” of your linux machine, make sure, that the user is allowed to execute scripts from /tmp. Most systems permit doing so.
- Make sure to use a linux user with permissions to log on the system over SSH and in the same way has permissions to execute the db2 binary.
- Also make sure to set your sshd_config to allow password authentication of SSH and not to use public key authentication
CRUCIAL: Double check if your Shell Script has the LF format and not the CRLF format in terms of end-of-line-markers. I recommend using Notepad++ to check this, like so:
If you are using VSCode like me, then double-check the bottom right bar to see whether LF or CRLF is toggled.
If your Shell script isn’t formatted well you are going to get an error similiar to this one, telling you that there is a “bad character” in your Shell Script.
I hope this script helps and I always love to hear feedback !