Backup to drive BATCH file (NDBackupScript v0.1)

Created for myself, but will share. Put this in a batch file or download the latest copy from here and run it to complete any of the following backups:

  • User profile (C:\Users\%username%)
  • Full HDD backup (C:\)
  • Custom backup (Specify own location and backup name)

It uses robocopy, also creating log files.

@ECHO OFF
GOTO script_init
:script_init
CLS
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET backup_started=NONE
SET backup_drive=W
SET backup_confirm=N
SET user_confirm_yes=N
SET final_confirm=N
SET path_created=N
SET path_create=1970-01-01_00-00-00
SET backup_src=C:\Users\%username%
SET backup_start=1970-01-01_00-00-00
SET backup_complete=1970-01-01_00-00-00
SET custom_src=C:\Users\%username%\Desktop
SET script_start=%fullstamp%
SET script_restart_prompt=N
SET backup_success=N
SET backup_logs=C:\Users\%username%\AppData\Local\Temp\NDBackupScriptLogs
SET backup_logs_created=N
SET backup_logs_create=1970-01-01_00-00-00
SET robocopy_log_date=%fullstamp%
SET robocopy_logs=%backup_logs%\%fullstamp%-Backupscript-robocopy.log
SET custom_backup_name=CUSTOM-%timestamp%
SET hdd_src=E
SET log_file=%backup_logs%\%fullstamp%-backupscript.log
IF NOT EXIST %backup_logs% (
ECHO Creating backup script logs directory %backup_logs%...
MKDIR %backup_logs%
IF ERRORLEVEL 0 (
SET backup_logs_created=Y
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET backup_logs_create=%fullstamp%
) ELSE (
GOTO script_restart
)
)
ECHO Backup script by Niall Davies (c) 2017
ECHO Script started at %fullstamp%
ECHO ------------------------------------
:backup_drive_prompt
ECHO ---------------------------------
ECHO Please confirm the drive letter that you will back up to. (selected is %backup_drive%)
SET /P backup_drive= Enter the drive letter [W]:
GOTO backup_drive_confirm
:backup_drive_confirm
ECHO -------------------------------------
ECHO Please confirm either Y or N, is the drive %backup_drive%:\ correct? (default is N)
SET backup_confirm=N
SET /P backup_confirm= Y/[N]:
:backup_init
IF "%backup_confirm%"=="N" (
GOTO backup_drive_prompt
) ELSE (
GOTO backup_choice
)
:backup_choice
SET backup_started=NONE
ECHO -------------------------------------
ECHO Please select one of the available following options...
ECHO 1. User profile backup.
ECHO 2. Full HDD backup.
ECHO 3. Custom backup.
ECHO 4. Restart script (clear CMD window).
ECHO 5. Exit script.
CHOICE /C 12345 /M "Enter your choice:"
:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 5 GOTO end
IF ERRORLEVEL 4 GOTO script_restart
IF ERRORLEVEL 3 GOTO custom_backup
IF ERRORLEVEL 2 GOTO full_hdd_backup
IF ERRORLEVEL 1 GOTO user_profile_backup
:user_profile_backup
ECHO ----------------------------------
ECHO User profile backup selected.
ECHO This will backup C:\Users\%username% and all contents to the %backup_drive%:\ drive.
ECHO Please confirm that you would like to start the backup of the %username% profile by typing "Y"
SET user_confirm_yes=N
SET /P user_confirm_yes= Y/[N]:
GOTO user_backup_process
:user_backup_process
IF %user_confirm_yes%==N (
GOTO backup_choice
) ELSE (
SET backup_started=USER_PROFILE_%username%
SET backup_src=C:\Users\%username%\
SET backup_appended=Profile-%username%
GOTO backup_process
)
:full_hdd_backup
ECHO ----------------------------------
ECHO Hard drive backup selected.
ECHO Please enter the drive letter of the HDD that you would like to backup. Default is %hdd_src%
SET /P hdd_src= Drive letter [%hdd_src%]:
ECHO This will backup %hdd_src%:\ and all contents to the %backup_drive%:\ drive.
ECHO Please confirm that you would like to start the backup of the %hdd_src%:\ drive?"Y"
SET user_confirm_yes=N
SET /P user_confirm_yes= Y/[N]:
GOTO full_hdd_backup_process
:full_hdd_backup_process
IF %user_confirm_yes%==N (
GOTO backup_choice
) ELSE (
SET backup_started=FULL_HDD_%hdd_src%
SET backup_src=%hdd_src%:\
SET backup_appended=HDD-%hdd_src%
GOTO backup_process
)
:custom_backup
ECHO ----------------------------------
ECHO Custom backup selected.
ECHO Please enter the path of the folder that you would like to backup. Default is %custom_src%
SET /P custom_src= Path:
ECHO This will backup %custom_src% and all contents to the %backup_drive%:\ drive.
ECHO Please confirm that you would like to start the backup of %custom_src% by typing "Y"
SET user_confirm_yes=N
SET /P user_confirm_yes= Y/[N]:
GOTO custom_backup_process
:custom_backup_process
IF %user_confirm_yes%==N (
GOTO backup_choice
) ELSE (
ECHO Please enter a name for the backup:
ECHO If none specified, name will be %custom_backup_name%
SET /P custom_backup_name= Name:
GOTO custom_backup_processed
)
:custom_backup_processed
SET backup_src=%custom_src%\
SET backup_appended=%custom_backup_name%
SET backup_started=CUSTOM_%custom_backup_name%
GOTO backup_process
:backup_process
ECHO ---------------------------------
ECHO Getting current date stamp...
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
ECHO datestamp: "%datestamp%"
ECHO timestamp: "%timestamp%"
ECHO fullstamp: "%fullstamp%"
ECHO ----------------------------------
ECHO Starting backup...
SET backup_date=%datestamp%
SET backup_start=%fullstamp%
SET backup_name=%backup_date%-%computername%-%backup_appended%-script\
SET backup_path=%backup_drive%:\%backup_name%
SET backup_logs=%backup_path%.scriptlogs
SET robocopy_logs=%backup_logs%\%fullstamp%-ROBOCOPY.log
IF NOT EXIST %backup_path% (
ECHO Creating backup directory %backup_path%...
MKDIR %backup_path%
IF ERRORLEVEL 0 (
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
ECHO Creating backup logs directory %backup_logs%...
MKDIR %backup_logs%
IF ERRORLEVEL 0 (
SET path_created=Y
SET path_create=%fullstamp%
SET backup_logs_created=Y
) ELSE (
GOTO backup_failed
)
) ELSE (
GOTO backup_failed
)
)
IF NOT EXIST %backup_logs% (
ECHO Creating backup logs directory %backup_logs%...
MKDIR %backup_logs%
IF ERRORLEVEL 0 (
SET backup_logs_created=Y
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET backup_logs_create=%fullstamp%
SET robocopy_logs=%backup_logs%\%fullstamp%-ROBOCOPY.log
) ELSE (
GOTO backup_failed
)
)
ECHO The script will now copy files from %backup_src% to %backup_path%
ECHO Final check. Are you sure you wish to continue?
SET /P final_confirm= Y\[N]:
GOTO backup_run
:backup_run
IF %final_confirm%==N (
GOTO script_restart
) ELSE (
ECHO Preparing to copy files from %backup_src% to %backup_path%
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET backup_start=%fullstamp%
SET robocopy_log_date=%fullstamp%
ECHO Running command: "ROBOCOPY %backup_src% %backup_path% * /E /COPY:DAT /Z /R:3 /W:15 /LOG:%robocopy_logs%"...
ROBOCOPY %backup_src% %backup_path% * /E /COPY:DAT /Z /R:3 /W:15 /LOG:%robocopy_logs%
IF ERRORLEVEL 0 (
SET backup_success=Y
GOTO backup_finished
) ELSE (
GOTO backup_failed
)
)
:backup_failed
ECHO -------------------------------------
ECHO Backup failed or there were errors in the backup.
ECHO Please check the log files and try again.
ECHO The log files can be found in %backup_logs%
ECHO Particulariliy %robocopy_logs% if it got that far.
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET backup_complete=%fullstamp%
IF %path_created%==Y (
ECHO Backup directory %backup_path% was created at %path_create%
)
IF %backup_logs_created%==Y (
ECHO Backup logs directory %backup_logs% was created at %backup_logs_create%
)
GOTO end
:script_restart_process
IF %script_restart_prompt%==N (
GOTO end_complete
) ELSE (
GOTO script_restart
)
:backup_finished
ECHO -------------------------------------
ECHO Backup was completed.
ECHO Backed up files and directories from %backup_src% to %backup_path%
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET backup_complete=%fullstamp%
ECHO ----------------------------------
IF %path_created%==Y (
ECHO Backup directory %backup_path% was created at %path_create%
)
IF %backup_logs_created%==Y (
ECHO Backup logs directory %backup_logs% was created at %backup_logs_create%
)
ECHO Backup started at %backup_start%
ECHO Backup finished at %fullstamp%
ECHO The log files can be found in %backup_logs%
MSG %username% ND Backup Script: Backup from %backup_src% to %backup_path% started at %backup_start% and completed at %fullstamp%
SET backup_success=Y
GOTO end
:script_restart
ECHO Script restarting...
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
ECHO ----------------------------------
ECHO Script started at %script_start%
ECHO Script finished at %fullstamp%
ECHO -------------------------------------
GOTO script_init
:end
ECHO ---------------------------------
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 "datestamp=%YYYY%-%MM%-%DD%" & set "timestamp=%HH%%Min%%Sec%"
SET "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
SET log_file=%backup_logs%\%fullstamp%-backupscript.log
ECHO Writing to log file %log_file%...
ECHO ND Backup script 2017 >> %log_file%
ECHO Running as %username% >> %log_file%
ECHO Script started at %script_start% >> %log_file%
ECHO Log files location: %backup_logs% >> %log_file%
ECHO Backup drive selected: %backup_drive%:\ >> %log_file%
ECHO Backup selected: %backup_started% >> %log_file%
IF %path_created%==Y (
ECHO Backup directory %backup_path% was created at %path_create% >> %log_file%
)
IF %backup_logs_created%==Y (
ECHO Backup logs directory %backup_logs% was created at %backup_logs_create% >> %log_file%
)
IF %backup_started%==NONE (
ECHO Backup not started. >> %log_file%
) ELSE (
ECHO Backup started at %backup_start% >> %log_file%
ECHO Backup finished at %backup_complete% >> %log_file%
ECHO Backup source: %backup_src% >> %log_file%
ECHO Backup location: %backup_path% >> %log_file%
ECHO Robocopy logfile: %robocopy_logs% >> %log_file%
ECHO Robocopy command: "ROBOCOPY %backup_src% %backup_path% * /E /COPY:DAT /Z /R:3 /W:15 /LOG:%robocopy_logs%" >> %log_file%
IF %backup_success%==Y (
ECHO Backup was successful! >> %log_file%
) ELSE (
ECHO Backup failed or completed with errors. Please check the Robocopy log file. >> %log_file%
)
)
ECHO Script finished running at %fullstamp% >> %log_file%
ECHO ---------------------------------- >> %log_file%
ECHO ----------------------------------
ECHO Script started at %script_start%
ECHO Script finished at %fullstamp%
ECHO ---------------------------------
ECHO Would you like to restart this script?
SET /P script_restart_prompt= Y\[N]:
GOTO script_restart_process
:end_complete
PAUSE

Leave a Reply

Your email address will not be published. Required fields are marked *