Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 584
ilansch Avatar asked Feb 19 '23 23:02

ilansch


2 Answers

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
like image 55
ilansch Avatar answered Feb 22 '23 14:02

ilansch


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.

like image 29
Alessandro Giusa Avatar answered Feb 22 '23 14:02

Alessandro Giusa