ex:1
robocopy "\\192.168.0.254\sahre" "e:" /e /xo /purge /R:0 /W:0 /log+:c:\log\backup_log.txt /xd "@Recycle" ".syncing_db"
ex:2 (只產生 bad_log)
robocopy "\\192.168.0.254\sahre" "e:\Databackup\share" /mir /copyall /tee /nfl /ndl /R:0 /W:0 /mt:10 /log+:c:\log\%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%.log /xd "#recycle" "#snapshot" /xf "thumbs.db"
/e 空白目錄 /xo排除較舊檔案 /purge 刪除來源 /R:n :: 失敗副本的重試次數: 默認為 1 百萬。/W:n :: 兩次重試間的等待時間: 默認為 30 秒。/LOG+:文件:: 將狀態輸出到日誌檔(附加到現有日誌中)。/xd 排除目錄
/nfl ::不產生 file_list /ndl ::不產生 directory_list /copyall ::拷貝全部屬性時間 /TEE ::輸出至主控台視窗,以及記錄檔。/NP :: 無進度 – 不顯示複製百分比。
robocopy 很好用, win7以上就支援了, 但是備份的時候會造成目錄隱藏, 很不方便.
因此備份的時候請加上這個/A-:SH參數
"/MT" 指的是通道數量,一般而言設定100就夠了。另還可替換以下功能:
"/S" (複製模式) 包含子目錄一併複製,但空目錄除外
"/MOVE" (移動模式) 移動來源到目的,並刪除來源
E:\ :: 檔案來源 硬碟 E:
U:\ :: 檔案目的地 硬碟 U:
/e :: 複製所有的子資料夾,包含空的資料夾。
/dcopy:t :: 保留所有檔案與資料夾的時間戳記。
/xo :: 如果已有相同的檔案,且時間相同或較舊就跳過。注意不是零 -:)
/xa:sh :: 跳過屬性為 s (系統) 與 h (隱藏) 的檔案。因為是複製整個硬碟,預設會連 Windows 隱藏的系統檔都複製過去,所以要排除。
/xj :: 跳過 NTFS junction 檔案,關於 junction 請參考這篇文章。
/unicode :: 畫面上的進度資訊以 unicode 顯示,若是操作非中文及英文檔名,這參數可以正確顯示名稱。有無開啟並不影響檔案複製、移動等操作,只是差在畫面顯示正不正確。
/eta :: 顯示預計完成時間。
———————————
@echo off
REM 依日期設定 log 檔名
for /f "tokens=1-4 delims=-/ " %%i IN ("%date%") DO (
set year=%%i
set month=%%j
set day=%%k
)
SET fname=%year%-%month%-%day%
SET fname=%fname: =0%
SET fname=D:\web-sync\log\%fname%.txt
REM 刪除網路資料夾 \\192.168.0.123\test 的連接(這邊註解不執行)
REM net use \\192.168.0.123\test /d
REM 建立網路資料夾 \\192.168.0.123\test 的連接,xyz為帳號,1234為密碼
net use \\192.168.0.123\test /user:192.168.0.123\xyz 1234
REM 1.使用 robocopy 將遠端 \\192.168.0.123\test 的資料夾同步到 C:\aaa
REM 2./MIR,複製子目錄(含空目錄),刪除已經不存在的目錄、檔案
REM 3./R:n,失敗複本的重試次數: 預設值是 1 百萬次。
REM 4./LOG+:檔案,將 log 記錄到檔案(附加模式)
REM 5./XF,排除不同步的檔案
robocopy "\\192.168.0.123\test" "C:\aaa" /MIR /R:0 /LOG+:%fname% /XF *.bat
—————————————————–
#robocopy實現源文件夾內容完全COPY到目標文件夾中,並在指定目錄中生成日記文件!
For /F %%i In (‘date /t’) do ren i:\RobocopyLogs\exchange.log exchange%%i.log
#用FOR循環實現對生成的日記文件名改名,以當天的日期做爲你的文件名,便於查看,也便於下面的FORFILES的執行!
start i:\automail1.vbs
#automail1.vbs實現的是執行完備份操作後發一封郵件,以保證備份的正常結束!
forfiles /p i:\RobocopyLogs /m *.log -d -7 /c "cmd /c del /f @path"
#用forfiles刪除七天前的日記文件,以免日記文件過大,佔用過多的磁盤空間!
最新能發郵件的腳本
NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
Set Email = CreateObject("CDO.Message")
Email.From = "******@sina.com"
Email.To = "*******@sina.com"
Email.Subject = "Test sendmail.vbs"
Email.Textbody = "OK!"
‘Email.AddAttachment "C:\foo.zip"
With Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.Item(NameSpace&"smtpserver") = "smtp.sina.com"
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = "**********"
.Item(NameSpace&"sendpassword") = "**********"
.Update
End With
Email.Send
——————————————–
robocopy errorlevel 錯誤 表可加在 bat 後
if errorlevel 16 echo ***FATAL ERROR*** & goto end
if errorlevel 15 echo FAIL MISM XTRA COPY & goto end
if errorlevel 14 echo FAIL MISM XTRA & goto end
if errorlevel 13 echo FAIL MISM COPY & goto end
if errorlevel 12 echo FAIL MISM & goto end
if errorlevel 11 echo FAIL XTRA COPY & goto end
if errorlevel 10 echo FAIL XTRA & goto end
if errorlevel 9 echo FAIL COPY & goto end
if errorlevel 8 echo FAIL & goto end
if errorlevel 7 echo MISM XTRA COPY & goto end
if errorlevel 6 echo MISM XTRA & goto end
if errorlevel 5 echo MISM COPY & goto end
if errorlevel 4 echo MISM & goto end
if errorlevel 3 echo XTRA COPY & goto end
if errorlevel 2 echo XTRA & goto end
if errorlevel 1 echo COPY & goto end
if errorlevel 0 echo –no change– & goto end
:end