Skip to content

Instantly share code, notes, and snippets.

@aurmil
Last active February 26, 2022 07:47
Show Gist options
  • Save aurmil/81c573c583a2f2389d2f99e5597a4e77 to your computer and use it in GitHub Desktop.
Save aurmil/81c573c583a2f2389d2f99e5597a4e77 to your computer and use it in GitHub Desktop.
Backup batch script using cwRsync
@ECHO OFF
REM *****************************************************************
REM
REM CWRSYNC.CMD - Batch file template to start your rsync command (s).
REM
REM *****************************************************************
REM Make environment variable changes local to this batch file
SETLOCAL
REM Specify where to find rsync and related files
REM Default value is the directory of this batch file
:: SET CWRSYNCHOME=%~dp0
SET CWRSYNCHOME=%PROGRAMFILES(x86)%\cwrsync
REM Make cwRsync home as a part of system PATH to find required DLLs
SET PATH=%CWRSYNCHOME%\bin;%PATH%
REM Windows paths may contain a colon (:) as a part of drive designation and
REM backslashes (example c:\, g:\). However, in rsync syntax, a colon in a
REM path means searching for a remote host. Solution: use absolute path 'a la unix',
REM replace backslashes (\) with slashes (/) and put -/cygdrive/- in front of the
REM drive letter:
REM
REM Example : C:\WORK\* --> /cygdrive/c/work/*
REM
REM Example 1 - rsync recursively to a unix server with an openssh server :
REM
REM rsync -r /cygdrive/c/work/ remotehost:/home/user/work/
REM
REM Example 2 - Local rsync recursively
REM
REM rsync -r /cygdrive/c/work/ /cygdrive/d/work/doc/
REM
REM Example 3 - rsync to an rsync server recursively :
REM (Double colons?? YES!!)
REM
REM rsync -r /cygdrive/c/doc/ remotehost::module/doc
REM
REM Rsync is a very powerful tool. Please look at documentation for other options.
REM
REM ** CUSTOMIZE ** Enter your rsync command(s) here
:: rsync --version
:: ssh -V
REM Usage: backup_cwrsync.cmd [<backup-type>] [<backup-dest> | <dest-drive-letter>]
:: https://ss64.com/nt/delayedexpansion.html
SETLOCAL EnableDelayedExpansion
:: ===== CONFIGURATION =====
SET source_data_drive=/cygdrive/d
SET backup_folders[light]=%source_data_drive%/Folder1 %source_data_drive%/Folder2 "%source_data_drive%/Folder 3"
:: Full backup includes folders of the light one
SET backup_folders[full]=!backup_folders[light]! %source_data_drive%/Folder4 %source_data_drive%/Folder5
SET allowed_types=light full
SET allowed_dests=key ssd
SET default_dest_folder=backup
SET dest_paths[key]=/cygdrive/h/%default_dest_folder%
SET dest_paths[ssd]=/cygdrive/s/%default_dest_folder%
:: ===== ARGUMENTS =====
SET backup_type=%~1
SET backup_dest=%~2
SET dest_path=
:: Check backup type argument
SET backup_type_arg_valid=1
:check_backup_type
if defined backup_type (
for %%i in (%allowed_types%) do (
if "%backup_type%" == "%%i" (
goto valid_backup_type
)
)
)
SET backup_type_arg_valid=0
SET /p backup_type="Backup type (%allowed_types%): "
goto check_backup_type
:valid_backup_type
echo Backup type: !backup_type!
:: Check backup destination argument
if not defined backup_dest (
if %backup_type_arg_valid% == 0 (
SET backup_dest=%~1
)
)
:check_backup_dest
if defined backup_dest (
for %%i in (%allowed_dests%) do (
if "%backup_dest%" == "%%i" (
if not defined dest_paths[%backup_dest%] (
echo Backup destination path not found for %backup_dest%
exit /b
)
SET dest_path=!dest_paths[%backup_dest%]!
goto valid_backup_dest
)
)
echo %backup_dest%| findstr /r "^[a-z]$" >nul 2>&1
if !errorlevel! == 0 (
call :LoCase backup_dest
SET dest_path=/cygdrive/!backup_dest!/%default_dest_folder%
goto valid_backup_dest
)
)
SET /p backup_dest="Destination name (%allowed_dests%) or drive letter: "
goto check_backup_dest
:valid_backup_dest
echo Destination path: %dest_path%
echo:
:: ===== SYNC =====
echo Dry run
rsync -aniv --no-perms --omit-dir-times --modify-window=2 --stats --progress !backup_folders[%backup_type%]! !dest_paths[%backup_dest%]!
echo:
SET /p do_sync="Continue sync (y/N)? "
if %do_sync% equ y (
rsync -a --no-perms --omit-dir-times --modify-window=2 --stats --progress !backup_folders[%backup_type%]! !dest_paths[%backup_dest%]!
)
:: https://www.robvanderwoude.com/battech_convertcase.php
:LoCase
:: Subroutine to convert a variable VALUE to all lower case.
:: The argument for this subroutine is the variable NAME.
FOR %%i IN ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO CALL SET "%1=%%%1:%%~i%%"
GOTO:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment