在整理手上幾台SQL SERVER 2000的Database Backup時,一方面為了方便快速還原資料庫,另外一方面為了備份冗餘、備份方式統一(先備份到本地,然後收上磁帶),將以前通過Symantec Backup Exec直接備份上帶的作業改成了如下方式:
Step 1: 通過資料庫維護計劃將備份產生在本地磁碟M,完整備份保留2天,交易記錄備份保留3天
M:\DB_BACKUP\FULL_BACKUP
M:\DB_BACKUP\LOG_BACKUP
Step 2: 備份完成後通過Symantec Backup Exec將備份檔案收上磁帶。
但是發現即使SQL SERVER 2000的資料庫維護計劃設定了刪除幾天前的備份檔案,但是發現根本沒有刪除到期備份。於是只好使用dos命令來處理。剛開始想用forfiles命令,結果我搜尋的時候,發現Windows 2000下沒有forfiles命令,後來通過從第三方複製過來,發現Windows 2000下也可以使用forfiles(如果不藉助於forfiles命令,直接用批處理命令完成這個,那簡直痛苦死了)
關於forfiles命令的文法如下所示
C:\>forfiles /?
FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {yyyy-MM-dd | dd}]
Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.
Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).
/M searchmask Searches files according to a searchmask.
The default searchmask is '*' .
/S Instructs forfiles to recurse into
subdirectories. Like "DIR /S".
/C command Indicates the command to execute for each file.
Command strings should be wrapped in double
quotes.
The default command is "cmd /c echo @file".
The following variables can be used in the
command string:
@file - returns the name of the file.
@fname - returns the file name without
extension.
@ext - returns only the extension of the
file.
@path - returns the full path of the file.
@relpath - returns the relative path of the
file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in
bytes.
@fdate - returns the last modified date of the
file.
@ftime - returns the last modified time of the
file.
To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should be preceded with
"cmd /c".
/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"yyyy-MM-dd" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
the range of 0 - 32768.
"+" is taken as default sign if not specified.
/? Displays this help message.
Examples:
FORFILES /?
FORFILES
FORFILES /P C:\WINDOWS /S /M DNS*.*
FORFILES /S /M *.txt /C "cmd /c type @file | more"
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C "cmd /c echo @path 0x09 was changed 30 days ago"
FORFILES /D 2001-01-01
/C "cmd /c echo @fname is new since Jan 1st 2001"
FORFILES /D +2014-12-15 /C "cmd /c echo @fname is new today"
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
對應的中文提示資訊如下所示:
文法
forfiles [/p Path ] [/m SearchMask ] [/s ] [/c Command ] [/d [{+ | - }] [{MM / DD / YYYY | DD }]]
參數
/p Path
指定Path ,表明要從哪裡開始搜尋。預設的檔案夾是當前工作目錄,該目錄通過鍵入句號(.) 指定。
/m SearchMask
按照SearchMask 搜尋檔案。預設的SearchMask 是*.* 。
/s
指示forfiles 在子目錄中搜尋。
/c Command
在每個檔案上運行指定的Command 。帶有空格的命令字串必須用引號括起來。預設的Command 是"cmd /c echo @file" 。
/d [{+ | - }] [{MM / DD / YYYY | DD }]
選擇日期大於或等於(+ )(或者小於或等於(- ))指定日期的檔案,其中MM / DD / YYYY 是指定的日期,DD 是當前日期減去DD 天。如果未指定+ 或- ,則使用+ 。DD 的有效範圍是0 - 32768。
/?
在命令提示字元下顯示協助。
如下所示,由於Windows Server 2000下拷貝過來的forfiles命令的版本是V 1.1,使用參數必須為-p、-c、-m 而且參數後面不能有空格。
如下所示,delete_old_backup.bat 刪除2天前的完整備份、交易記錄備份、以及維護計劃產生的記錄檔。
echo --------------------------------------------- >>delete_old_backup.log echo Delete the backup log start at %Date% - %time% >>delete_old_backup.log rem Delete days. set DaysAgo=2 rem delete old backup log files. set LogPath=M:\DB_BACKUP\ forfiles -p%LogPath% -m*.txt -d-%DaysAgo% -c"cmd /c del /q @FILE" >> delete_old_backup.log echo Delete the backup log Stop at %Date% - %time% >>delete_old_backup.log echo Delete the full backup start at %Date% - %time% >>delete_old_backup.log set FullBackupPath=M:\DB_BACKUP\FULL_BACKUP forfiles -p%FullBackupPath% -m*.bak -d-%DaysAgo% -c"cmd /c del /q @FILE" >> delete_old_backup.log echo Delete the full backup Stop at %Date% - %time% >>delete_old_backup.log echo Delete the log backup start at %Date% - %time% >>delete_old_backup.log set LogBackupPath=M:\DB_BACKUP\LOG_BACKUP forfiles -p%LogBackupPath% -m*.TRN -d-%DaysAgo% -c"cmd /c del /q @FILE" >> delete_old_backup.log echo Delete the log backup Stop at %Date% - %time% >>delete_old_backup.log echo --------------------------------------------- >>delete_old_backup.log
指令碼編寫、測試成功後,然後設定Task Schedule,大體步驟如下步驟所示:
Step 1:在控制台找到任務計劃,執行任務計劃嚮導:
Step 2: 點擊“瀏覽”按鈕,選擇M:\DB_BACKUP\delete_old_backup.bat檔案
Step 3:輸入任務的名稱,以及執行任務的Schedule
Step 4:設定任務執行的時間以及頻率
Step 5:輸入知曉計劃任務的帳號以及密碼
Step 6:完成任務計劃設定。
上面的bat主要是支援參數與日誌的現實,確實不錯,其實核心代碼就是這樣的
forfiles /p "c:\backup" /d -10 /c "cmd /c echo deleting @file ... && del /f @path"
測試前最好備份一下資料