Oracle regular backup and oracle backup

Source: Internet
Author: User

Oracle regular backup and oracle backup

1. Copy the following code to the text, and modify the text suffix name to a XXX. bat batch file;

**************************************** **************************************** **************************************** *********

@ Echo off
Setlocal ENABLEDELAYEDEXPANSION

: Read the configuration file
Md % windir % \ OracleAutoBackup> nul 2> nul
Set configFile = % windir % \ OracleAutoBackup \ config. ini
Set I = 0
If not exist % configFile % echo.> % configFile %
For/f "delims =" % x in (% configFile %) do (
If! I! = 0 set bak_hou = % x
If! I! = 1 set bak_lot = % x
If! I! = 2 set bak_dir = % x
If! I! Gtr 2 (
Set/a gup =! I! -2
Set ora [! Gup!] = % X
)
Set/a I + = 1
)

: Take the default value
If "! Bak_hou! "=" "Set bak_hou = 3
Echo! Bak_hou! | Findstr "^ [0-9] * $"> nul | set bak_hou = 3
If "! Bak_lot! "=" "Set bak_lot = 7
Echo! Bak_lot! | Findstr "^ [0-9] * $"> nul | set bak_lot = 7
If "! Bak_dir! "=" "Set bak_dir = % cd % \ Database Backup
For/f "tokens = *" % x in ("! Bak_dir! ") Do set bak_dir = % ~ Fx
If not exist! Bak_dir! Md! Val! > Nul 2> nul
: Remove incorrectly formatted database connection configuration items
Set j = 0
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (
Set ora [% I]> nul 2> nul &&(
Set ora_cur =
For/f "usebackq delims =. tokens = 1-3" % a in ('set ora [% I] ') do set ora_cur = % B
Set ora [% I] =
Echo! Ora_cur! | Findstr "\/"> nul 2> nul & echo! Ora_cur! | Findstr "@"> nul 2> nul &&(
Set/a j + = 1
Set ora [! J!] =! Ora_cur!
)
)
)

: Enter the Management Program
If "% 1" = "" goto init


: Check whether the exp command is available
: Checkexp
Set resultFile = % temp % \ % random=.txt
Del % resultFile %/q> nul 2> nul
Exp a/a @ a % random % file = % temp % \ % random %. dmp> nul 2> % resultFile %
If exist % resultFile % (
Type % resultFile % | find "'exp' is not an internal or external command"> nul
If! Errorlevel! = 0 (
Del % resultFile %> nul
The echo exp command is unavailable! The program is about to exit!
Ping-n 10 127.1> nul 2> nul
Exit
)
Del % resultFile %> nul
)

: 1. Database Backup
Title backup process
Echo.
Echo.
Echo 1. Backing up ......
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (
Set ora [% I]> nul 2> nul &&(
Set ora_cur =
For/f "usebackq delims =. tokens = 1-3" % a in ('set ora [% I] ') do set ora_cur = % B
Set ora_usr =
Set ora_net =
For/f "delims =/" % a in ('echo! Ora_cur! ') Do set ora_usr = %
For/f "delims = @ tokens = 2" % a in ('echo! Ora_cur! ') Do set ora_net = %
Echo.
Echo.
Echo % I. Backing up! Ora_usr! /******@! Ora_net !......
Md! Bak_dir! \! Ora_net! __! Ora_usr! \> Nul 2> nul
Set ftmr =! Time: = 0!
Set bak_cur_dir =! Bak_dir! \! Ora_net! __! Ora_usr! \
For/f "tokens = *" % x in ("! Bak_cur_dir! ") Do set bak_cur_dir = % ~ Fx
Set bak_cur_fnm =! Ora_net! __! Ora_usr! __! Date :~ 0, 4 !! Date :~ 5, 2 !! Date :~ 8, 2! -! Ftmr :~ 0, 2 !! Ftmr :~ 3, 2!
Set bakfile =! Bak_cur_dir !! Bak_cur_fnm !. Dmp
Set logfile =! Bak_cur_dir !! Bak_cur_fnm !. Log
Exp! Ora_cur! File = "! Bakfile! "Log = "! Logfile! "
Echo if the backup is successful, compress> nul
If exist "! Bakfile! "(
Pushd! Bak_cur_dir!
Set zipfile =
If exist "% ProgramFiles % \ winrar \ winrar.exe "(
Echo uses WinRAR for compression> nul
Set zipfile =! Bak_cur_fnm !. Rar
"% Programfiles % \ winrar" a-r "! Zipfile! ""! Bak_cur_fnm !. Dmp ""! Bak_cur_fnm !. Log"
) Else (
Echo uses ZIP commands for compression> nul
Set zipfile =! Bak_cur_fnm !. Zip
Zip "! Zipfile! ""! Bak_cur_fnm !. Dmp ""! Bak_cur_fnm !. Log "> nul
)
If exist "! Zipfile! "(
Del/q "! Bakfile! "
Del/q "! Logfile! "
)
Popd
) Else (
Echo if there is no backup file but there is a log file, delete the Log File> nul
If exist "! Logfile! "Del/q "! Logfile! "
)
)
)
: 2. Delete expired Database Backup
Echo.
Echo.
Echo 2. Clearing expired backup files ......
For/f "tokens = 1, 2, 3 delims =-" % a in ('echo wscript. echo date -! Bak_lot! ^> T ~. Vbs ^ & cscript // nologo t ~. Vbs ^ & del t ~. Vbs ') do (
Set y = % a & set m = % B & set d = % c
If % B lss 10 set m = 0% % B
If % c lss 10 set d = 0% % c
)
Set DateE =! Y! -! M! -! D!
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (
Set ora [% I]> nul 2> nul &&(
Set ora_cur =
For/f "usebackq delims =. tokens = 1-3" % a in ('set ora [% I] ') do set ora_cur = % B
Set ora_usr =
Set ora_net =
For/f "delims =/" % a in ('echo! Ora_cur! ') Do set ora_usr = %
For/f "delims = @ tokens = 2" % a in ('echo! Ora_cur! ') Do set ora_net = %
Set cur_dir =! Bak_dir! \! Ora_net! __! Ora_usr!
For/f "tokens = *" % x in ("! Cur_dir! ") Do set cur_dir = % ~ Fx

Echo check that the backup is successful today. No> nul
Set fnm_pre =! Cur_dir! \! Ora_net! __! Ora_usr! __! Date :~ 0, 4 !! Date :~ 5, 2 !! Date :~ 8, 2! -& Set today_success = 0
Dir! Fnm_pre! *. Dmp! Fnm_pre! *. Zip! Fnm_pre! *. Rar/B> nul 2> nul & set today_success = 1
If "! Today_success! "=" 1 "(
Echo checks whether the folder conditions meet> nul
For % a in (! Cur_dir! \ *. Dmp ,! Cur_dir! \ *. Log ,! Cur_dir! \ *. Zip ,! Cur_dir! \ *. Rar) do (
Echo checks whether the file name conditions meet> nul
Set n = % a & set n =! N :~ -17,-9! & Amp; set n =! N :~ 0, 4! -! N :~ 4, 2! -! N :~ 6, 2!
Set t = % ~ Ta
Set FileDate =! T :~ 0, 10!
If "! N! "= "! FileDate! "(
Echo checks whether the time condition meets> nul
If! FileDate! Leq % DateE % (
Echo % date :~ 0, 10% % time :~ 0, 8% Delete expired backup %
Echo % date :~ 0, 10% % time :~ 0, 8% Delete expired backup % a>! Cur_dir! \ Delete. log
Del/q "%"
)
)
)
) Else (
Echo % date :~ 0, 10% % time :~ 0, 8% [! Ora_net! __! Ora_usr!] Because the backup failed today, the expired files are not deleted now!
Echo % date :~ 0, 10% % time :~ 0, 8% [! Ora_net! __! Ora_usr!] Because the backup failed today, the expired files are not deleted now!>! Cur_dir! \ Delete. log
)
)
)
: 3. Exit successfully.
Echo.
Echo.
Echo 3. This backup operation is complete and will be exited soon.
Ping-n 10 127.1> nul 2> nul
Exit

 

:: ================================================= ====================================
:: ======================================== ====================================
: Init
Mode con cols = 100 lines = 40
Title Oracle automatic backup-by zhouyou96
Color 0e

: Copy to Windows Directory
Copy "% ~ F0 "" % windir % \ OracleAutoBackup. bat "> nul 2> nul

: Register a scheduled task
: Regtasks
SC config schedule start = auto> nul 2> nul
At | find "service not started"> nul 2> nul &&(
Net start schedule
If not! Errorlevel! = 0 (
Echo Task Scheduler ^ (scheduled Task ^) the service is not started, and the program is about to quit!
Pause> nul
Goto exit
)
)
Set job_tmr =! Bak_hou! : 00
If! Bak_hou! Lss 10 set job_tmr = 0! Bak_hou! : 00
At! Job_tmr! /Every, 31% windir % \ OracleAutoBackup. bat-backup> nul 2> nul
For/f "usebackq" % I in ('dir % windir % \ tasks \ at *. job/B/o: D') do set lastAt = % I
Del % windir % \ tasks \ Oracle automatic backup. job> nul 2> nul
Rename % windir % \ tasks \! LastAt! Oracle automatic backup. job

: Save the configuration file
: Saveconfig
Echo! Bak_hou!> % ConfigFile %
Echo! Bak_lot!> % ConfigFile %
Echo! Bak_dir!> % ConfigFile %
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (
Set ora [% I]> nul 2> nul &&(
Set ora_cur =
For/f "usebackq delims =. tokens = 1-3" % a in ('set ora [% I] ') do set ora_cur = % B
Echo! Ora_cur!> % ConfigFile %
)
)
: Prepare the Database Configuration string
Set ora_str =
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (
Set ora [% I]> nul 2> nul &&(
Set ora_cur =
For/f "usebackq delims =. tokens = 1-3" % a in ('set ora [% I] ') do set ora_cur = % B
Set ora_usr =
Set ora_net =
For/f "delims =/" % a in ('echo! Ora_cur! ') Do set ora_usr = %
For/f "delims = @ tokens = 2" % a in ('echo! Ora_cur! ') Do set ora_net = %
Set ora_str =! Ora_str! % I .! Ora_usr! /******@! Ora_net !;
)
)

: Start
: Start
Cls
Echo success --------------------------------------------------------------------------------------------------
Echo Oracle automatic backup
Echo Author: zhouyou96 QQ: 191458000
Echo success --------------------------------------------------------------------------------------------------
Echo uses the scheduled task function provided by the operating system to periodically run the exp command every day to export and compress the specified Oracle database, and then delete it as needed.
Echo expired compressed export files to achieve automatic backup.
Echo generally, for ease of management, an oracle user in our company has full permission to manage a database, so the login name of this user
Echo can be regarded as the database name.
Echo.
Echo 1. Add a database :! Ora_str!
Echo 2. delete a database
Echo 3. Set the folder :! Bak_dir!
Echo 4. Backup in a few minutes :! Bak_hou!
Echo 5. delete a few days ago :! Bak_lot!
Echo 6. Backup Now
Echo 7. Exit
Echo.


: Select
: Cho
Set choice =
Set/p choice = select:
If not "% choice %" = "" set choice = % choice :~ 0, 1%
If "% choice %" = "1" goto addora
If "% choice %" = "2" goto delora
If "% choice %" = "3" goto setdir
If "% choice %" = "4" goto sethou
If "% choice %" = "5" goto setlot
If "% choice %" = "6" goto nowbak
If "% choice %" = "7" goto exit
Echo.
Echo ===================================================== ========================================================== ============================
Echo ============================================ select 1 ~ 7. Press any key to reselect it! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
Goto start


: Add a database
: Addora
Set maxora = 0
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (set ora [% I]> nul 2> nul & (set maxora = % I ))
Set str_result = up to 9!
If not! Maxora! = 9 (
Set/a maxora + = 1
Set new_ora =
Set/p new_ora = enter (User Name/password @ network service name ):
Set str_result = format error!
If not "! New_ora! "= ""(
Echo! New_ora! | Findstr "\/"> nul 2> nul & echo! New_ora! | Findstr "@"> nul 2> nul &&(
Set ora [! Maxora!] =! New_ora!
Set str_result = added successfully,
)
)
)
Echo ===================================================== ========================================================== ============================
Echo ==========================================! Str_result! Press any key to continue! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
If "! Str_result! "=" Added successfully, "goto saveconfig
Goto start


: Delete a database
: Delora
Set str_result = operation error!
Set del_idx = 0
Set/p del_idx = enter the ordinal number (1 ~ 9 ):
If not "% del_idx %" = "" set del_idx = % del_idx :~ 0, 1%
If "! Del_idx! "=" "Set del_idx = 0
Echo! Del_idx! | Findstr "^ [0-9] * $"> nul | set del_idx = 0
If not "! Del_idx! "=" 0 "(
Set ora [! Del_idx!] =
Set str_result = deleted successfully,
)
: Remove incorrectly formatted database connection configuration items
If "! Str_result! "=" Deleted successfully ,"(
Set j = 0
For % I in (1, 2, 3, 4, 5, 6, 7, 8, 9) do (
Set ora [% I]> nul 2> nul &&(
Set ora_cur =
For/f "usebackq delims =. tokens = 1-3" % a in ('set ora [% I] ') do set ora_cur = % B
Set ora [% I] =
Echo! Ora_cur! | Findstr "\/"> nul 2> nul & echo! Ora_cur! | Findstr "@"> nul 2> nul &&(
Set/a j + = 1
Set ora [! J!] =! Ora_cur!
)
)
)
)
Echo ===================================================== ========================================================== ============================
Echo ==========================================! Str_result! Press any key to continue! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
If "! Str_result! "=" Deleted successfully, "goto saveconfig
Goto start

: Set folders
: Setdir
Set new_dir =
Set/p new_dir = enter the folder used for backup:
If "! New_dir! "=" "Set new_dir = % cd % \ Database Backup
For/f "tokens = *" % x in ("! New_dir! ") Do set new_dir = % ~ Fx
Set bak_dir =! New_dir!
Echo ===================================================== ========================================================== ============================
Echo ================================================================== the setting is successful, press any key to continue! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
Goto saveconfig

: What is the backup time?
: Sethou
Set str_result = operation error!
Set new_hou =
Set/p new_hou = enter the number of minutes for backup every day (0 ~ 23 ):
Echo! New_hou! | Findstr "^ [0-9] * $"> nul | set new_hou =
If not "! New_hou! "= ""(
If! New_hou! Geq 0 (
If! New_hou! Leq 23 (
Set bak_hou =! New_hou!
Set str_result = set successfully,
)
)
)
Echo ===================================================== ========================================================== ============================
Echo ==========================================! Str_result! Press any key to continue! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
If "! Str_result! "=" Set successfully, "goto regtasks
Goto start


: Deleted a few days ago
: Setlot
Set str_result = operation error!
Set new_lot =
Set/p new_lot = enter the backup that was deleted a few days ago (greater than zero ):
Echo! New_lot! | Findstr "^ [0-9] * $"> nul | set new_lot =
If not "! New_lot! "= ""(
If! New_lot! Gtr 0 (
Set bak_lot =! New_lot!
Set str_result = set successfully,
)
)
Echo ===================================================== ========================================================== ============================
Echo ==========================================! Str_result! Press any key to continue! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
If "! Str_result! "=" Set successfully, "goto saveconfig
Goto start

: Backup Now
: Nowbak
Start % windir % \ OracleAutoBackup. bat-backup
Echo ===================================================== ========================================================== ============================
Echo ====================================================== started successfully, press any key to continue! ==========================================
Echo ===================================================== ========================================================== ============================
Pause> nul
Goto start


: Exit the program
: Exit
: Pause> nul

 

**************************************** **************************************** **************************************** ***************

2. to configure a network connection, ensure that the network can access the operating system of the specified backup database, or ping the database.

3. Install the oracle database and configure the connection to the database that can be backed up. You can use PLSQL Developer to connect to oracle;

1. configuration method: 1. find tnsnames in the oracle Installation File. ora, (installation directory: \ product \ 10.2.0 \ db_1 \ network \ ADMIN \ tnsnames. ora,) use the editor to add connection information

For example:

LINUX =

(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (HOST =Backup database IP Address) (PORT = 1521 ))
)
(CONNECT_DATA =
(SERVICE_NAME =Back up database instances)
)
)

2. Use PLSQL Developer to test whether the connection passes

4. Set the scheduled task and run XXX. bat in the copied code.Task PlanFind the plan, and add a parameter-backup to set the backup time.

 

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.