Skip to content

Instantly share code, notes, and snippets.

@scharlal
Last active May 6, 2018 18:06
Show Gist options
  • Save scharlal/cb799847c802054440b2e9e50da95e21 to your computer and use it in GitHub Desktop.
Save scharlal/cb799847c802054440b2e9e50da95e21 to your computer and use it in GitHub Desktop.
Batch based TimeMachine-Backup for Windows
::
:: This backup script will allow you to copy ALL files with a set archive attribute.
:: This is specially helpful when you are working on text-files and let this script run periodically.
::
@ECHO OFF
:: CONFIG ::
:: Set your destination and the amount of unique months per year you want to keep in your destination
SET destination=_backup
SET keepMonths=3
:: This script will create a new folder and subfolders based on the current date and time in your backup folder
:: e.g.: 2016_07\21\18_17
SET year=%DATE:~-4%
SET month=%DATE:~3,2%
SET day=%DATE:~,2%
SET hour=%TIME:~,2%
SET minute=%TIME:~3,2%
:: Switch directory to the directory this script is located in
CD %~dp0
:: Delete the directories in the backup folder but only keep the last X
:: X is defined by the variable %keepMonths%
FOR /F "skip=%keepMonths%delims=" %%D IN ('DIR /B /O-N %destination%') DO CALL :remove_dir %%D
:: Set your folders you want to backup
CALL :backup "C:\[02] qSpace DEV"
CALL :backup "C:\xampp"
GOTO:EOF
:backup
SET source=%~1
SET source=%source:~3%
XCOPY "%~1" "%destination%\%year%_%month%\%day%\%hour%_%minute%\%source%\" /M /S /EXCLUDE:backupBlacklist.txt
:remove_dir
RMDIR /S /Q "%destination%\%~1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment