首页 > 解决方案 > 使用 RoboCopy 的命令行 3 天滚动备份

问题描述

我想要完成的是对源文件夹进行为期三天的滚动备份,每天都会对其进行修改。我希望删除超过 3 天的任何内容,因为在最坏的情况下,我将不得不恢复到最近的备份,但是如果需要,拥有 3 天可以让我恢复到以前的更改。

我在命令行中所做的如下:

SET STAMP=%DATE:/=-% %TIME::=.%
SET SOURCE="C:\Users\PersonA\Documents\Test\Source"
SET DEST="C:\Users\PersonA\Documents\Test\Destination\%STAMP%"

robocopy %SOURCE% %DEST% /e

forfiles -p "C:\Users\PersonA\Documents\Test\Destination\" -s -m *.* -d -3 -c "cmd /c echo @file"

我已经为 FolderName 和 Current Date and Time 添加了时间戳并连接起来。

但是,我正在为他删除超过 3 天的任何内容而苦苦挣扎,目前该过程只是在复制,但我需要的是一个过程来检测修改日期或创建日期并使用我生成的代码删除超过 3 天的目录. 我在上面添加的已删除命令似乎根本没有做任何事情。

如果有人可以提供帮助,我将不胜感激。

先感谢您

标签: batch-filecommand-line

解决方案


我会推荐这样的东西:

SET STAMP=%DATE:/=-% %TIME::=.%
SET SOURCE="C:\Users\PersonA\Documents\Test\Source"
SET DEST="C:\Users\PersonA\Documents\Test\Destination\%STAMP%"

REM Set Robocopy to only retry once after waiting 1 second; copy all
REM file attributes, security, etc; and create a log file in the 
REM temp folder with the same name as the date.
robocopy %SOURCE% %DEST% /e /r:1 /w:1 /COPYALL /LOG:%TEMP%\%STAMP%.LOG

C:
CD \Users\PersonA\Documents\Test\Destination\
REM Recursively and quietly delete all directories older than
REM 3 days
FORFILES /S /D -3 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

这是从这篇文章中给出的答案推断出来的。


推荐阅读