Linux/Unixshell automatic FTP backup file

Source: Internet
Author: User
Using shell scripts to monitor and manage Oracle databases will greatly simplify DBA workload, such as common instance monitoring, monitoring, and alarm log monitoring.

Using shell scripts to monitor and manage Oracle databases will greatly simplify DBA workload, such as common instance monitoring, monitoring, and alarm log monitoring.

Using shell scripts to monitor and manage Oracle databases will greatly simplify DBA workload, such as common instance monitoring, monitoring, and alarm log monitoring, and database backup, AWR report automatic mail, etc. This article describes how to use the shell script in Linux to implement automatic FTP backup of files.

Linux Shell and Import and Export references:

Linux/Unix shell scripts call SQL and RMAN scripts

Passing variables between Linux/Unix shell SQL statements

Linux Unix shell calls PL/SQL

1. Linux/Unix shell automatic FTP script

Robin @ SZDB :~ /Dba_scripts/custom/bin> more auto_ftp_dump.sh
# + --------------------------------------------------- +
# + Filename: ftp_dump_auto.sh |
# + Desc: Ftp the dump file automatically |
# + Usage: |
# +./Auto_ftp_dump.sh $ ORACLE_SID |
# + Author: Robinson |
# + Blog: |
# + --------------------------------------------------- +
#
#! /Bin/bash
#
#-----------------------------
# Define environment variable
#-----------------------------
#

If [-f ~ /. Bash_profile];
Then
.~ /. Bash_profile
Fi

#--------------------------
# Check SID
#--------------------------

If [-z "$ {1}"]; then
Echo "Usage :"
Echo "'basename $0 'oracle_sid"
Exit 1
Fi

ORACLE_SID =$ {1}; export ORACLE_SID
TARGET_SID = SY0755BK
TIMESTAMP = 'date + % Y % m % d % H % m ';
DT = 'date + % Y % m % d ';
RETENTION = 1

DUMP_DIR =/u01/oracle/admin/SY0755/BNR/dump
DUMP_FILE =$ {DUMP_DIR}/EXP _ $ {ORACLE_SID }_$ {DT}. dmp
LAST_EXP_DUMP_LOG =$ {DUMP_DIR}/EXP _ $ {ORACLE_SID }_$ {DT}. log

FTP_LOG =$ {DUMP_DIR}/ftp _ $ {ORACLE_SID }_$ {TIMESTAMP}. log
FTP_ERR_LOG =$ {DUMP_DIR}/ftp _ $ {ORACLE_SID }_$ {TIMESTAMP} _ err. log

Date >>$ {FTP_LOG}
Echo "FTP source :$ {ORACLE_SID}" >$ {FTP_LOG}
Echo "FTP target :$ {TARGET_SID}" >$ {FTP_LOG}
Echo "Staring tar the dump file" >$ {FTP_LOG}

#-------------------------
# Tar the dump file
#-------------------------

If [-s "$ {DUMP_FILE}"] & [-s "$ {LAST_EXP_DUMP_LOG}"]; then
Cd $ {DUMP_DIR}
Tar-czvf exp_1_1_oracle_sid1__1_dt1_.tar.gz./EXP _ $ {ORACLE_SID }_$ {DT }.???
Else
Echo "Dump file does not exist for $ {ORACLE_SID}" >$ {FTP_LOG}
MAIL_SUB = "Dump files were not found for $ {ORACLE_SID} on 'hostname' before start to tar the dump file"
E-mail-s $ MAIL_SUB dba@trade.com <$ {FTP_LOG}
Exit 1
Fi

#--------------------------------------------
# Config ftp parameter and ftp the dump file
#--------------------------------------------

If [! -S "$ {DUMP_DIR}/exp_1_1_oracle_sid1__1_dt1_.tar.gz"]; then
Echo "The dump files were not compressed before start ftp" >$ {FTP_LOG}
MAIL_SUB = "The dump files were not compressed for $ {ORACLE_SID} on 'hostname' before start ftp, exit! "
E-mail-s $ MAIL_SUB dba@trade.com <$ {FTP_LOG}
Exit 1
Fi

Echo "Start copy tar file to $ {TARGET_SID}..." >>$ {FTP_LOG}
Echo "--------------------------------------------------" >$ {FTP_LOG}

FTP_SRV = 10.200.48.21
FTP_USR = oracle
FTP_PWD = oracle
FTP_LOCAL_PATH =$ {DUMP_DIR}
FTP_REMOTE_PATH =/u02/database/$ {TARGET_SID}/BNR/dump

Ftp-nv> $ FTP_LOG < Open $ {FTP_SRV}
User $ {FTP_USR }$ {FTP_PWD}
Binary
Cd $ {FTP_REMOTE_PATH}
LCD $ {FTP_LOCAL_PATH}
Prompt
Mput exp_1_1_oracle_sid1__1_dt1_.tar.gz
Close
Bye
EOF

RC = $?
Flag = 'cat $ {FTP_LOG} | grep-I "receive OK" | grep-v grep'
If [-z "$ {flag}"] | ["$ {RC}"-ne 0]; then
Echo "FTP tar file to $ {TARGET_SID} failed" >$ {FTP_LOG}
MAIL_SUB = "FTP dump file from $ {ORACLE_SID} to $ {TARGET_SID} failed, please check! "
E-mail-s $ MAIL_SUB dba@trade.com <$ {FTP_LOG}
Exit 1
Else
Echo "End ftp dump file to $ {TARGET_SID}..." >$ {FTP_LOG}
Echo "End ftp time at:" 'date' -- 'date + % Y % m % d % H % m' >>$ {FTP_LOG}
Echo "" >>$ {FTP_LOG}
Echo "-------------------------- End of the log file -----------------------" >$ {FTP_LOG}
MAIL_SUB = "FTP the dump file from $ {ORACLE_SID} to $ {TARGET_SID} completed successful ."
E-mail-s $ MAIL_SUB dba@trade.com <$ {FTP_LOG}
Fi

Find $ {DUMP_DIR}-name "* ftp *"-mtime + $ RETENTION-exec rm {}\;
Find $ {DUMP_DIR}-name "EXP _ $ {ORACLE_SID} *. gz"-mtime + $ RETENTION-exec rm {}\;

Exit

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.