批量脚本制作备份文件夹。只有新的和修改过的文件

时间:2012-07-01 06:41:34

标签: batch-file backup xcopy

我需要一个脚本,只将已更改/修改过的文件从我的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 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

这是一个通用备份脚本。

@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'的所有文件夹。被忽略了。文件夹' MyFolder'在h:\驱动器上创建(如果尚未可用)。

此脚本的功能:

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

更新: 如果您没有排除列表""作为第三个参数。

答案 2 :(得分:0)

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

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

  

/ M =仅复制设置了存档属性的文件,关闭存档属性。

     

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

     

/ Y =取消确认。

请注意,作为之前给出的答案,只需添加/ D并删除/ M就可以了解诀窍,请查看有关/ D的帮助说明:

  

/ D:m-d-y复制在指定日期或之后更改的文件。                  如果没有给出日期,则仅复制那些文件                  来源时间比目的地时间新。

如果您需要在一次运行中备份多个文件夹并拥有执行日志文件,您可以这样:

@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

答案 3 :(得分: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 . . ."