Automatic backup of Oracle database under Windows

the cover

Original link: Automatic backup of Oracle database under Windows | Elvin

foreword

The original intention of writing this article is the same as the previous article, some details are not repeated here, and more detailed explanations can be found in the previous article: Automatic backup of MySQL database under Windows

1. Script file for database backup

1. Script processing files

First use the txt file to copy the following content into it, and then modify the suffix .txt to .bat

@echo off
echo ================================================
echo Windows环境下Oracle11.2g数据库的自动备份脚本
echo  1. 使用data_当前日期命名备份文件
echo  2. 自动删除7天前的备份,防止文件越来越多
echo ================================================

::以“YYYYMMDD”格式取出当前时间。
echo =====================当天时间=====================  
set BACKUPDATE=%date:~0,4%%date:~5,2%%date:~8,2%%Time:~0,2%%Time:~3,2%%Time:~6,2%
echo ================================================

::设置用户名、密码和要备份的数据库
set USER=robot
set PASSWORD=robot123456
set DATABASE=134.175.59.49/orcl

::创建备份目录  
if not exist "C:\oraclebackup\data"    mkdir C:\oraclebackup\data
if not exist "C:\oraclebackup\log"     mkdir C:\oraclebackup\log
set DATADIR=C:\oraclebackup\data
set LOGDIR=C:\oraclebackup\log

echo =====================开始备份=====================
exp %USER%/%PASSWORD%@%DATABASE%  file=%DATADIR%\data_%BACKUPDATE%.dmp log=%LOGDIR%\log_%BACKUPDATE%.log‘
echo ================================================
echo 备份完成

::删除7天前的备份
forfiles /p "%DATADIR%" /s /m *.* /d -7 /c "cmd /c del @path"
forfiles /p "%LOGDIR%" /s /m *.* /d -7 /c "cmd /c del @path"
::exit
pause>nul

⚠️: log=’%LOGDIR%\log_%BACKUPDATE%.log‘The variables behind = must be ‘’enclosed in English single quotation marks

2. Script processing file parsing

  • :: Get the current time in "YYYYMMDD" format.

Explanation: This command is the same as rem, using :: is more rigorous than the rem command

  • exp

Explanation: After the command is executed, a dmp file will be generated, which contains the saved database data; you can learn more about it yourself

  • exit

Explanation: Use exit to exit the execution window directly, and use pause>nul to press any key to exit

3. Where the script needs to be changed

  • :: Set username, password and database to backup

The three parameters USER, PASSWORD, and DATABASE should be changed to their own database user name, password, and database to be backed up.

  • :: create backup directory

Define your own location to store backup files and log files

4. Script execution garbled error

If there is a garbled error, you can use the document to edit the .bat file, select the function 另存为, and select the encoding format asANSI

If Chinese garbled characters still appear, you can add the following code at the top of the file content, that is, @echo offabove this line of code

rem is marked as compiling Chinese using 936 encoding format

chcp 936

@echo off

5. The empty table is not saved to the dmp file

When we back up, there may be no way to save the table without data to the dmp file, so we need to do some other operations to ensure that the empty table can also be saved to the file normally. For details, please refer to this article "Oracle 11g Export Empty Table, less table solution "

I have a lot of tables here, and then I chose method three in the article

Two, Windows scheduled tasks

For details, refer to the previous article: Automatic backup of MySQL database under Windows

There are very detailed timing task settings

reference link

Oracle automatic backup in windows


More knowledge is being continuously updated!!!


statement

The source of the original text is indicated in the reference part, and the source of the original text can be obtained at the reference link of the article

If the content in the article involves the original copyright, please contact [email protected] by email , and the related articles or content will be changed or canceled in time

Guess you like

Origin blog.csdn.net/weixin_42464282/article/details/130991394