首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于覆盖6周前的备份的批处理文件

用于覆盖6周前的备份的批处理文件
EN

Stack Overflow用户
提问于 2018-01-08 18:38:57
回答 1查看 512关注 0票数 0

我有以下批处理文件,它进行每日备份,并保存7天。

代码语言:javascript
复制
@echo off
C:
CD\ProAuto
setlocal enableextensions

:: Get Numeric Date
for /F "skip=1 delims=" %%F in ('wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE') do ( for /F "tokens=1-3" %%L in ("%%F") do (
   set CurDay=0%%L
   set CurMonth=0%%M
   set CurYear=%%N
   ))
set CurDay=%CurDay:~-2%
set CurMonth=%CurMonth:~-2%

:: Get numeric Day of the Week
for /f %%a in ('wmic path win32_localtime get dayofweek /format:list ^| findstr "="') do (set %%a)

:: Get Time and Date with full Month and Day of week
for /f "skip=8 tokens=2,3,4,5,6,7,8,9 delims=,: " %%D in ('robocopy /l * T: T: /ns /nc /ndl /nfl /np /njh /XF * /XD *') do (
   set "dow=%%D"
   set "month=%%E"
   set "DD=%%F"
   set "YYYY=%%G"
   set "HH=%%H"
   set "MM=%%I"
   set "SS=%%J"
   set "AMPM=%%K"
   )
IF "%AMPM%"=="AM" ( Set AMPM=am )
IF "%AMPM%"=="PM" ( Set AMPM=pm )

Set RoboLog="C:\ProAuto\Logs\~%DayofWeek% %dow%.log"
Echo QuickBooks Backup  %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >%RoboLog%

:: The following checks to see if the database is locked by attempting to rename the file to the same name.
ren "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files\XYZ.QBW" "XYZ.QBW" >nul && (goto UnLocked) || (goto Locked)
Goto Donelocked
:UnLocked
echo QuickBooks is UnLocked and will be backed up >>%RoboLog%
Net Use "\\192.168.0.246\Backup Files"
Net Use "\\192.168.0.252\QB_BK"
Move "\\192.168.0.252\QB_BK\%DayofWeek% %dow%*" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\%DayofWeek% %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee
Call :RoboCheck
If %RoboRC% GTR 7 Goto QBBKFail
:QBBKGood
Echo QuickBooks Daily Backup Successful! >>%RoboLog%
goto doneQBBK
:QBBKFail
Echo QuickBooks Daily Backup Had Errors! >>%RoboLog%
Echo Sending Error Email with Log to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0QB_KS_Fail.ps1
:DoneQBBK
Echo Copying Log to Backup Directory >>%RoboLog%
Copy %RoboLog% "\\192.168.0.252\QB_BK\" >>%RoboLog%
Goto Donelocked
:Locked
echo QuickBooks is Locked and cannot be backed up because it is open on a client computer >>%RoboLog%
echo Generating an Open File report and Emailing it to Clients
Set OpenFileLog="C:\ProAuto\Logs\QB_Open_Files %curmonth%-%curday%-%curyear%.log"
Echo Open QuickBooks Files >> %RoboLog%
Echo Open QuickBooks Files > %OpenFileLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %RoboLog%
Echo %dow%, %month% %dd%, %yyyy% @ %hh%:%mm%:%ss%%AmPm% >> %OpenFileLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %RoboLog%
openfiles /query  /v | findstr /rc:"^$" /c:"Locally" /c:"remotely" /c:"----------" /c:"======" /c:"Accessed" /c:"QuickBooks" >> %OpenFileLog%
Echo Sending Open Files Email to Clients >> %RoboLog%
powershell -executionpolicy bypass -file %~dp0Locked_Email.ps1
Del %OpenFileLog%
:DoneLocked
Goto :EOF
:RoboCheck
Set RoboRC=%ErrorLevel%
Echo Return Code: %RoboRC% >>%RoboLog%
    if %RoboRC% EQU 16 echo ***FATAL ERROR*** >>%RoboLog% & echo Serious error. Robocopy did not copy any files. >>%RoboLog% & echo Either a usage error or an error due to insufficient access privileges >>%RoboLog% & echo on the source or destination directories. >>%RoboLog%
    if %RoboRC% EQU 15 echo Code 15 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 14 echo Code 14 = Code 2 XTRA + Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 13 echo Code 13 = Code 1 OKCOPY + Code 4 - MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 12 echo Code 12 = Code 4 MISMATCHES + Code 8 COPYFAIL: >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 11 echo Code 11 = Code 1 OKCOPY + Code 2 - XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 10 echo Code 10 = Code 2 XTRA + Code 8 COPYFAIL: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 9  echo Code 9 = Code 1 OKCOPY + Code 8 - COPYFAIL: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 8  echo Code 8 COPYFAIL - Some files or directories could not be copied >>%RoboLog% & echo (copy errors occurred and the retry limit was exceeded). >>%RoboLog% & echo Can be caused by Open or Locked Files, Permission Issues, or Network Rescources Unavailable >>%RoboLog% & echo Check these errors further. >>%RoboLog%
    if %RoboRC% EQU 7  echo Code 7 = Code 1 OKCOPY + Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 6  echo Code 6 = Code 2 XTRA + Code 4 MISMATCHES: >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 5  echo Code 5 = Code 1 OKCOPY + Code 4 MISMATCHES: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 4  echo Code 4 MISMATCHES - Some Mismatched files or directories were detected. >>%RoboLog% & echo Examine the output log. Housekeeping might be required. >>%RoboLog% & echo Can be causes by different file systems reporting of timestapmps >>%RoboLog%
    if %RoboRC% EQU 3  echo Code 3 = Code 1 OKCOPY + Code 2 XTRA: >>%RoboLog% & echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog% & echo Code 2 XTRA - Some Extra files or directories were detected. >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 2  echo Code 2 XTRA - Some Extra files or directories were detected. No files were copied >>%RoboLog% & echo Examine the output log for details. >>%RoboLog%
    if %RoboRC% EQU 1  echo Code 1 OKCOPY - One or more files were copied successfully >>%RoboLog%
    if %RoboRC% EQU 0  echo No errors occurred, and no copying was done. >>%RoboLog% & echo The source and destination directory trees are completely synchronized >>%RoboLog%
Goto :EOF

它所做的是每周一重命名星期一的备份目录以包含当前日期,然后使用Robocopy将当前数据镜像到新目录。然后是星期二,它对星期二目录执行相同的操作,因此每个备份都保留7天。

我想修改这个,使其每周备份,并保留6周。我正在考虑将目录名称更改为第1周到第6周,并按顺序回收目录,但现在我没有一种简单的方法来确定我在哪周,因为没有日历事件每6周重复一次,就像我使用一周中的某一天进行每日备份一样。

因此,我正在寻找的是一些方法或建议,关于如何确定哪个备份目录最接近6周的历史,以便我可以重新循环正确的目录。有可能6周前没有备份,在这种情况下,它只会创建一个备份,就像如果星期三丢失时每日备份一样,它只会创建一个新的星期三备份。我也不想总是使用最旧的备份,例如,如果机器停机,它错过了备份,我不想更改顺序,我想使用6周前的备份或创建新的备份。

我想找出一种计算%WeekofCycle%的方法,这样我就可以使用以下代码:

代码语言:javascript
复制
Move "\\192.168.0.252\QB_BK\Week %WeekofCycle%*" "\\192.168.0.252\QB_BK\Week %WeekofCycle%* %dow% %CurYear%-%CurMonth%-%CurDay%"
robocopy "C:\Users\Public\Documents\Intuit\QuickBooks\Company Files" "\\192.168.0.252\QB_BK\Week %WeekofCycle%* %dow% %CurYear%-%CurMonth%-%CurDay%" /XD "QuickBooksAutoDataRecovery" /MIR /FFT /V /NP /NDL /copy:DAT /dcopy:T /r:2 /W:2 /log+:%RoboLog% /tee

我正在尝试找出一种方法来计算周期中的哪一周,而不管可能已经或可能尚未完成的备份,以及是否存在额外的备份或不存在的备份。如果我计算我是在第3周,我将重命名以前的备份在"\192.168.0.252\QB_BK\Week 3*“,现在有日期和日期附加到它,然后使用新的名称和Robocopy来更新更改的文件。

EN

回答 1

Stack Overflow用户

发布于 2018-01-08 21:52:22

我要做的是将每个目标目录命名为yyyymmdd -根据需要附加dayname/date/other data数据,并将目标目录名称附加到一个文件(backups.log)

成功备份当前数据后,

代码语言:javascript
复制
for /f "skip=42,delims=" %%a in (`sort /r backups.log`) do (
 ECHO rd /s /q "%%a"
 findstr /v /x "%%a" backups.log >dummy.txt
 move dummy.txt backups.log
)

这将按反向日期顺序对备份目录的名称进行排序,因此最新的目录名在第一位。将跳过前42 =6周*7天的行,并使用rd删除任何进一步的条目(将是最早的),然后使用findstrbackups.log文件中删除这些行,以输出/v与刚刚删除的目录不完全匹配的那些行,因此将从文件和文件系统中删除保留的42行上的任何备份。

请注意,for将处理读取backups.log文件的反向排序结果。此结果保存在内存中,不受for循环对backups.log所做更改的影响。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48148565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档