Batch script to make backup folder. only new and modified files
Asked Answered
J

4

4

I need a script to copy only the changed/modified and new files from my C:\Dropbox to my C:\backup. Why does this copy only the folder structure:

@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
Johnie answered 1/7, 2012 at 6:41 Comment(1)
For someone who wants a free tool, there is rubocopy for windows/*nix, and a nice tool with gui named SyncToy (part of power toys)Johnie
J
8
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
Johnie answered 1/7, 2012 at 9:8 Comment(0)
S
2

This is a generic backup script.

@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

The way I would call it, assuming the batch script is called backup.bat:

backup.bat MyFolder h:\MyFolder .metadata

Where the content of MyFolder gets backuped to the h:\MyFolder and all folders called '.metadata' are ignored. The Folder 'MyFolder' on the h:\ drive gets created if not already available.

Features of this script:

  • Creates all necessary folders
  • Copies all files if they were modified since last backup
  • Creates a folder for logging within the destination folder data called: .backup_log
  • Creates a log-file containing all info while xcopy is running and timestamp of starting and stopping time.

Update: If you have no exclusion list put "" as third parameter.

Spinule answered 1/7, 2017 at 11:54 Comment(3)
And How would one restore from the backup?Micropathology
you are right. just replace rm with del. rm is from cygwin.Spinule
I'd recommend del /f /q so there is no output.Micropathology
M
0

TO answer the original question, why your script copied just the folder structure...

It was due the parameters on the xcopy, you specified just these /m/e/y:

/M = Copies only files with the archive attribute set, turns off the archive attribute.

/E = Copies directories and subdirectories, including empty ones.

/Y = Suppress confirmations.

Notice that as the answer given before, just by adding /D and removing /M it will make the trick, please check what the help says about /D:

/D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.

In case you need to back up multiple folders in one run and have a log file with their executions, you can have something like this:

@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
Muir answered 23/12, 2016 at 0:8 Comment(0)
U
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 . . ."
Unmeaning answered 12/4, 2018 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.