Skip to content

Instantly share code, notes, and snippets.

@ccritchfield
Last active December 5, 2019 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccritchfield/442ef9bfdafbb0eaf0301efe980a5eeb to your computer and use it in GitHub Desktop.
Save ccritchfield/442ef9bfdafbb0eaf0301efe980a5eeb to your computer and use it in GitHub Desktop.
DOS - Backup Folders (XCOPY)
----------------------------------------------
Windows Command-line Folder Copy Automation
----------------------------------------------
DOS / Windows Commandline script to automate folder copies / backups.
Can task schedule to do folder archiving after-hours.
IT would have a nightly backup of things, but created some backup
scripts to copy whole project folders into backups. This let me
quickly restore something if I was working on things or someone
borked something up.
@ECHO OFF
REM Turn ECHO OFF, except for what we explicitly ECHO
REM Doing some commands twice, once to DOS cmd screen and again to a log file
REM ">> <file>" appends results of command to log file
REM CALL command runs another batch file/script without stopping this main one
REM Let's get started...
REM -----------------------------------------------------
REM Set intial variables we'll be using, and make backup folder
SET VAR_PATH=_archive\backup\
SET VAR_LOG=%VAR_PATH%backup_folders.log
IF NOT EXIST %VAR_PATH% MKDIR %VAR_PATH%
REM Log start of backups, put blank line after title
SET VAR_TEXT="Backing Up RoboHelp Projects..."
CALL backup_folders_log.bat %VAR_TEXT% %VAR_LOG%
ECHO.
ECHO. >> %VAR_LOG%
REM Use "backup_folders_xcopy" batch file to copy each project to %VAR_PATH% backup folder, logging results as we go
CALL backup_folders_xcopy.bat cares %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat customerrelations %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat customerservice %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat operations %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat provisioning %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat revenuerecovery %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat sales %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat shared %VAR_PATH% %VAR_LOG%
CALL backup_folders_xcopy.bat tag %VAR_PATH% %VAR_LOG%
REM Log that we're done
SET VAR_TEXT=%TIME%...Done!
CALL backup_folders_log.bat %VAR_TEXT% %VAR_LOG%
REM Using this batch file like a sub-procedure to print/append text passed to it
REM onto the screen and into a log file if the arguments passed call for such
REM Batch file is called using...
REM backup_folders_log.bat %1 %2
REM ...where...
REM %1 = VAR_TEXT = Text to print/log/display
REM %2 = (ECHO FILE) = Print text to log file if a file path/name is given (IE: not "") otherwise don't
REM 1st line below prints to screen, 2nd prints to file
ECHO %1
ECHO %1 >> %2
REM Next two lines insert a blank line into the screen and log file, respectively, to space out the feedback
REM ECHO.
REM ECHO. >> %2
REM Using XCOPY instead of COPY, because...
REM /E switch lets us copy whole folders, including subfolders & files
REM /Q "quiets" the copy, preventing it from listing the files moving
REM /I forces the copy to make the destination folder if it doesn't exist
REM ">> <file>" appends results of command to log file (IE: # of files copied)
REM I'm using this batch file as a "sub-procedure", passing arguements to it to use when running
REM Batch files can use %1, %2, etc to represent user-provided variables on the command-line
REM So, this (the executable command)...
REM XCOPY /E /Q /I %1 %2%1 >> %3
REM ...represents this...
REM XCOPY /E /Q /I %VAR_PROJ% %VAR_PATH%%VAR_PROJ% >> %VAR_LOG%
REM ...where...
REM %1 = VAR_PROJ = Project Folder we want to copy/backup
REM %2 = VAR_PATH = Backup Folder we're dumping stuff to
REM %3 = VAR_LOG = Log file we're logging results to
REM When done copying/logging, add a blank line to the log when done to space out the next log entry
REM -------------------------------------------
REM Start by logging project we're backing up, then do copy, logging results, then add space to end of log
SET VAR_TEXT=%TIME%...%1...
CALL backup_folder_log.bat %VAR_TEXT% %3
XCOPY /E /Q /I %1 %2%1 >> %3
ECHO.
ECHO. >> %3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment