Shanghai Tengke Education Dream Database Training Dry Goods Sharing Use BAT on WINDOWS to periodically perform backup operations

In the Dameng read-write separation system, when the host is under high pressure, we can choose to back up the backup machine for full database backup, but the backup machine cannot use the proxy job that comes with DM7 for backup. Only use ETL or scripts for database backup. In this case, we can use the BAT script to perform regular full backups, incremental backups of the database, and delete unused backup sets.

 

First create a stored procedure PRO_BackupDatabase for backup in the database

 

create or replace PROCEDURE  PRO_BackupDatabase

as

declare

bakname  VARCHAR2(300);

BAKSTR VARCHAR2 (300);

V_DATE VARCHAR2 (50);

begin

select to_char(sysdate,'day') day into  V_DATE;

IF   V_DATE ='Saturday'  THEN

set bakname='DB_DAMENG_FULL_'||TO_CHAR(SYSDATE,'YYYY_MM_DD_HH24_MI_SS');

set BAKSTR= 'backup database full to '||' " '||bakname||' " '||' backupset ' 'D:\data\bak\' ||bakname|| ''' task thread 4 parallel 4;';

PRINT   BAKSTR;

EXECUTE IMMEDIATE  BAKSTR;

ELSE

set bakname='DB_DAMENG_INCRE_' || TO_CHAR(SYSDATE,'YYYY_MM_DD_HH24_MI_SS');

set BAKSTR = 'backup database increment with backupdir ''D:\data\bak'' to '||'"'||bakname||'"'||' backupset ''D:\data\bak\'||bakname||''' task thread 4 parallel 4;';

PRINT   BAKSTR;

EXECUTE IMMEDIATE  BAKSTR;

END IF;

end;

 

Here is a full backup on Saturday and incremental backup on other days. The backup directory is D:\data\bak

Then start writing the BAT script

Display the backup script and the corresponding call SQL

add_bak content:

 

@echo off

Set User=SYSDBA

Set Pass=SYSDBA

Set IP=LOCALHOST

Set SysDate=%date:~0,4%-%date:~5,2%-%date:~8,2%

@ echo  time = %SysDate%

@ echo  username = %User%

@ echo  password = %Pass%

@ echo  IP address = %IP%

@ echo ***** Database backup ******

cd E:\工作\dmdbms\dm7_setup_win64_ent_release_20180504\source\bin

E:

disql.exe %User%/%Pass%@%IP% `D:\data\bak_job\bak.sql

@ echo  end

 

You can manually modify USER, PASS, IP and the corresponding execution SQL script. My bak.sql is placed under D:\data\bak_job.

bak.sql content:

 

CALL PRO_BackupDatabase();

EXIT;

 

Then the BAT script deleted from the backup

del_bak content: 

 

@echo off

set SrcDir=D:\data\bak

@ echo  delete date %date%

@ echo  deletes backup files and empty directories that expired 7 days ago

REM specifies the number of days

set DaysAgo=7

 

powershell -c "Get-ChildItem -Path '%SrcDir%' -Recurse -ErrorAction:SilentlyContinue | Where-Object -FilterScript {(((get-date) -($_.CreationTime)).days -gt %DaysAgo% -and $_.PsISContainer -ne $True)} |Remove-Item -Force"

 

for /f "delims=%%a in ('dir /ad /b /s D:\data\bak\^|sort /r') do (

rd  " %%a " > nul  2 > nul &&echo The empty directory " %%a " was successfully deleted! )

@ echo  delete end

 

Then there is the BAT file for logging and calling

start_bak.bat content:

 

@echo off

Set SysDate=%date:~0,4%-%date:~5,2%

cd  D: \ data \ bak_job

D:

@ echo  delete backup

call del_bak.bat>>Delete_%SysDate%_log.txt

@ echo  start backup

call add_bak.bat>>Bakup_%SysDate%_log.txt

@ echo  end

Pause

 

Now let's make a full backup and an increase, and delete the data to try

Set the time to Saturday

Then execute start_bak.bat, where I added pause to the script for easy viewing

 

 

 

No problem for the first time

Then modify the time to the second or third day, restart the database for backup

 

Increasing backup can also be executed normally, now try to delete the backup, I will adjust the date to the third Saturday

 

The deletion and backup log is recorded once a month

Guess you like

Origin blog.csdn.net/qq_42726883/article/details/108528580