Summary of db2 automatic backup (linux and windows)

Source: Internet
Author: User
Tags db2 connect db2 connect to
1. linuxdb2 automatic backup, compression after backup, copy to another server full backup :#! Binsh # ----------------------------------- # db2 automatic backup script # bylushuai # createat2013-08-05 # ------------------------------------ # parameter Basepathhomedb2-backup-linuxd

1. linux db2 automatic backup, compression after backup, copy to another server full backup :#! /Bin/sh # ----------------------------------- # db2 automatic backup script # by lushuai # create at 2013-08-05 # ---------------------------------- # parameter Basepath =/home/db2-backup-linux/d

1. linux db2 automatic backup, compression after backup, and copy to another server

Full backup:

#! /Bin/sh
#-------------------------------------
# Db2 automatic backup script
# By lushuai
# Create at 2013-08-05
#------------------------------------
# Parameters
Basepath =/home/db2-backup-linux/
Dblist = $ Basepath "script/dblist.txt" # list of database files to be backed up
Bkdate = 'date + % Y % m % d' # backup time
# Bkrootpath = $ Basepath "" # backup path
TargetPath = $ Basepath
FilePath = $ TargetPath "backup/" $ bkdate
Bkyear = "'date + % Y" '# year to be backed up. The default value is the current year.
Ftphost = 192.168.0.15 # ftp server ip Address
Ftpuser = gever # the ftp server user must have the create read write Permission
Ftppwd = geverabc # password of the username of the ftp server
DbUser = db2admin
DbPwd = db2admin
Backup_Log = $ TargetPath $ bkdate. log # log

Echo "1. Backup initialization, backup time: 'date + '% Y-% m-% d % H: % M: % S'"'> $ Backup_Log

If [-e "$ Backup_Log"]; then {
Echo "the log file is already exist! ">>$ Backup_Log
} Else {
Touch $ Backup_Log
Echo "create log file Successful! ">>$ Backup_Log
}
Fi


# Create a backup file storage path and authorize
If [-w "$ {FilePath}"]; then {
Echo "--- The database dir is already exist! ">>$ Backup_Log
} Else {
Mkdir-p "$ FilePath"
Chmod 777-R "$ FilePath"
Echo "--- The database dir create Successful! ">>$ Backup_Log
}
Fi

# Read the database file list and back up the database
While read LINE
Do
Echo "2. Start backing up the database:" $ LINE >>$ Backup_Log
Su-db2admin-c "db2 backup db $ LINE online to $ FilePath include logs"
Echo "3. Database Backup ended:" $ LINE "($ FilePath)"> $ Backup_Log
Done <$ dblist

# File Packaging
Echo "4. compressed file" >>$ Backup_Log
Cd $ TargetPath "backup"
Tar-zcvf $ bkdate. tgz $ bkdate> $ Backup_Log

# Delete data before compression
Echo "5. delete data before compression"> $ Backup_Log
Echo
Rm-rf $ {bkdate}
# Deleting backup data 10 days ago

If [-w "'date + % Y % m % d -- date '20 days ago '"']; then {
Echo "6. Delete backup data 20 days ago"> $ Backup_Log
Rm-rf "$ TargetPath" backup/"'date + % y % m % d -- date '20 days ago '"'
} Else {
Echo "6. You have not backed up data for deletion 20 days ago"> $ Backup_Log
}
Fi
Echo "7. Backup ended" >>$ Backup_Log

# Scp $ bkdate. tgz root@10.10.0.154:/home/212_db_backup
# Echo "8. Upload the backup file to 10.10.0.154:/home/212_db_backup, multi-machine backup"> $ Backup_Log
Echo "============================================== ============================================== ">> $ Backup_Log
Exit

Incremental Backup:

#! /Bin/sh
#-------------------------------------
# Db2 automatic backup script
# By lushuai
# Create at 2013-08-05
#------------------------------------
# Parameters
Basepath =/home/db2-backup-linux/
Dblist = $ Basepath "script/dblist.txt" # list of database files to be backed up
Bkdate = 'date + % Y % m % d' # backup time
# Bkrootpath = $ Basepath "" # backup path
TargetPath = $ Basepath
FilePath = $ TargetPath "backup/incremental" $ bkdate
Bkyear = "'date + % Y" '# year to be backed up. The default value is the current year.
Ftphost = 192.168.0.15 # ftp server ip Address
Ftpuser = gever # the ftp server user must have the create read write Permission
Ftppwd = geverabc # password of the username of the ftp server
DbUser = db2admin
DbPwd = db2admin
Backup_Log = $ TargetPath "incremental" $ bkdate. log # log

Echo "1. Backup initialization. The Incremental backup time is 'date + '% Y-% m-% d % H: % M: % S'"'> $ Backup_Log

If [-e "$ Backup_Log"]; then {
Echo "the log file is already exist! ">>$ Backup_Log
} Else {
Touch $ Backup_Log
Echo "create log file Successful! ">>$ Backup_Log
}
Fi


# Create a backup file storage path and authorize
If [-w "$ {FilePath}"]; then {
Echo "--- The database dir is already exist! ">>$ Backup_Log
} Else {
Mkdir-p "$ FilePath"
Chmod 777-R "$ FilePath"
Echo "--- The database dir create Successful! ">>$ Backup_Log
}
Fi

# Read the database file list and back up the database
While read LINE
Do
Echo "2. Start backing up the database:" $ LINE >>$ Backup_Log
Su-db2admin-c "db2 backup db $ LINE online incremental to $ FilePath include logs"
Echo "3. Database Backup ended:" $ LINE "($ FilePath)"> $ Backup_Log
Done <$ dblist

# File Packaging
Echo "4. compressed file" >>$ Backup_Log
Cd $ TargetPath "backup"
Tar-zcvf "incremental" $ bkdate. tgz "incremental" $ bkdate >>$ Backup_Log

# Delete data before compression
Echo "5. delete data before compression"> $ Backup_Log
Echo
Rm-rf "incremental" $ {bkdate}
# Deleting backup data 10 days ago

If [-w "'date + % Y % m % d -- date '20 days ago '"']; then {
Echo "6. Delete backup data 20 days ago"> $ Backup_Log
Rm-rf "$ TargetPath" backup/incremental "'date + % y % m % d -- date '20 days ago '"'
} Else {
Echo "6. You have not backed up data for deletion 20 days ago"> $ Backup_Log
}
Fi
Echo "7. Backup ended" >>$ Backup_Log

# Scp & quot; incremental & quot; $ bkdate. tgz root@10.10.0.154:/home/212_db_backup
# Echo "8. Upload the backup file to 10.10.0.154:/home/212_db_backup, multi-machine backup"> $ Backup_Log
Echo "============================================== ============================================== ">> $ Backup_Log
Exit

2. Export table structure and data in linux db2

Echo Data Backup starts
# Loading environment variables
./Home/db2inst1/sqllib/db2profile
# BACKDIR is the directory for storing backup files
BACKDIR = "/home/db2inst1/backup"
# DBNAME is the database name
DBNAME = "ATMP"
# Connecting to a database
Db2 connect to $ DBNAME user db2inst1 using 000000
# Creating a temporary directory to store exported table structure files
# Mkdir $ BACKDIR
# Creating a temporary directory to store exported table structure files
Mkdir $ BACKDIR/$ DBNAME
# Grant readable and writable permissions to several directories
Chmod a + w $ BACKDIR
Chmod a + w $ BACKDIR/$ DBNAME
# Switch to the new temporary directory
Cd $ BACKDIR/$ DBNAME
# Execute the Export Database Table Structure
Db2look-d $ DBNAME-td @-I db2inst1-w 000000-e-o ATMP. SQL
# Creating a temporary directory to store exported table data files
Mkdir atmpdata
# Grant readable and writable permissions to directories
Chmod a + w atmpdata
# Switch to the new temporary directory
Cd atmpdata
# Export database table data
Db2move $ DBNAME export-u db2inst1-p 000000
# Retrieve system date
DATE = 'date + % Y % m % d'
Hh = 'date + % H'
Mm = 'date + % m'
Now = $ DATE-$ hh: $ mm
# Switch to the backup directory
Cd $ BACKDIR
Echo $ now
# Start packaging and compressing backup files
Tar cvf-$ DBNAME | gzip-qc> mongodbname.w.now.tar.gz
# Deleting a temporary directory
Rm-rf $ BACKDIR/$ DBNAME
Echo completes Data Backup

3. window automatically backs up data

Reference: http://lushuai1987.iteye.com/admin/blogs/1920669

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.