4

我需要一个脚本来仅将更改/修改的文件和新文件从我的 C:\Dropbox 复制到我的 C:\backup。为什么这只会复制文件夹结构:

@echo off

set destination=C:\Backup
set source=C:\Users\XXXX\Dropbox\Intranet

for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1=%%j"."%%i"."%%k

xcopy %source%"\*" %destination%"\*" /m/e/y
mkdir %destination%"\LastBackupDate %yyyymmdd1%"
echo A folder containing the latest date has been created in root directory of %source%.
echo Finished copying %source% to %destination%
echo.
pause
4

4 回答 4

6
ECHO OFF
set source=C:\Users\xxxx\Dropbox\
set destination=C:\Backup\

for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set yyyymmdd1=%%j"."%%i"."%%k

if exist %destination% goto GO
:GO
mkdir %destination%%yyyymmdd1%
xcopy %source%* %destination% /s/d/y/c/v/r
echo. 
echo Finished copying %source% to %destination%
echo.
echo Created %destination%%yyyymmdd1%
pause
于 2012-07-01T09:08:00.137 回答
2

这是一个通用的备份脚本。

@echo off
REM get start time
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

REM set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
set "logtimestamp=%YYYY%.%MM%.%DD% %HH%:%Min%:%Sec%"

REM actual copy
set source=%1
set destination=%2

REM create the exclusion list
set exclusion=%3
set exclusion=%exclusion:"=%
(for %%i in (%exclusion%) do echo %%i) > exclusion.txt

REM set the file name for the logging data
set log=log-%fullstamp%.txt

REM start the backup process
echo // started backup at %logtimestamp% > %log%
echo // from %~f1 to %~f2\ >> %log%

echo ---- >> %log%
xcopy %source% %destination% /S /E /C /D /H /R /Y /V /I /EXCLUDE:exclusion.txt >> %log%
echo ---- >> %log%
del /f exclusion.txt

REM get finish time
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "logtimestamp=%YYYY%.%MM%.%DD% %HH%:%Min%:%Sec%"

echo // finished backup at %logtimestamp% >> %log%

REM move the logging
if not exist "%destination%\.backup_log" mkdir %destination%\.backup_log
move %log% %destination%\.backup_log

我会这样称呼它,假设批处理脚本称为backup.bat:

backup.bat MyFolder h:\MyFolder .metadata

MyFolder 的内容被备份到 h:\MyFolder 并且所有名为“.metadata”的文件夹都被忽略。如果 h:\ 驱动器上的文件夹“MyFolder”尚不可用,则会创建该文件夹。

该脚本的特点:

  • 创建所有必要的文件夹
  • 如果自上次备份以来修改了所有文件,则复制所有文件
  • 在目标文件夹数据中创建一个用于记录的文件夹:.backup_log
  • 创建一个日志文件,其中包含 xcopy 运行时的所有信息以及开始和停止时间的时间戳。

更新:如果您没有排除列表,请将“”作为第三个参数。

于 2017-07-01T11:54:07.020 回答
0

要回答最初的问题,为什么您的脚本只复制了文件夹结构......

由于 xcopy 上的参数,您只指定了这些 /m/e/y:

/M = 仅复制带有存档属性集的文件,关闭存档属性。

/E = 复制目录和子目录,包括空目录。

/Y = 禁止确认。

请注意,正如之前给出的答案,只需添加 /D 和删除 /M 就可以了,请检查帮助中关于 /D 的内容:

/D:mdy 复制在指定日期或之后更改的文件。如果未指定日期,则仅复制源时间比目标时间新的文件。

如果您需要在一次运行中备份多个文件夹并拥有一个包含其执行的日志文件,您可以使用以下内容:

@echo off

call:backUpFolder "C:\Users\XXX\folder1" "C:\Backup\folder1"
call:backUpFolder "C:\Users\XXX\folder2" "C:\Backup\folder2"
call:backUpFolder "C:\Users\XXX\folder3" "C:\Backup\folder3"

goto:eof
::--------------------------------------------------
::-- This is the function to back up one folder
::--------------------------------------------------
:backUpFolder
set source="%~1"
set destination="%~2"
echo copying from %source% to %destination%

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%

xcopy %source%"\*" %destination%"\*" /s/d/y/c/v/r

echo %ldt%: %source% to %destination% >> backHistory.log
echo Finished copying %source% to %destination%
goto:eof
于 2016-12-23T00:08:42.027 回答
0
set "source="F:\Projects""
set "destination=G:\backup"

for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
if not exist %destination%\%datestr% (mkdir %destination%\%datestr%)

for /f %%I in ('wmic os get localdatetime ^|find "20"') do set dt=%%I
set dt=%dt:~4,2%-%dt:~6,2%-%dt:~0,4%
echo %dt%

XCOPY %source% %destination%\%datestr% /S /d:%dt% /Y

REM XCOPY %source% %destination%\%datestr% /E /I /H /Y /S /d:%dt%

pause
"Press any key to continue . . ."
于 2018-04-12T08:34:21.093 回答