Backup & Restore batch file help

Soldato
Joined
7 Jun 2010
Posts
2,859
Location
Scotland
I'm trying to create a batch file which will backup data to a date stamped folder and then when needed will be able to restore the data from the most recent backup. I can get the .bat to create the folder and backup the data needed from the set variables. The problem is when I want to do an incremental backup or restore that data from the previous day. I get an error telling me the date cannot be found. I can't for the life of me figure out how to make it look for the most recent backup and then do an incremental or restore. I was searching online for ages yesterday and found nothing of use.

Code:
@ECHO OFF
REM These are the variables for the backup drive
SET drive=c:\Backup
SET backcmd=xcopy /e/w/h/v
SET incrcmd=xcopy /e/w/h/d/v
SET restcmd=xcopy /e/w/h/v
SET date="%date:~0,2%-%date:~3,2%-%date:~6,6%"

GOTO MENU
:MENU
ECHO Please choose a command by pressing numbers 1-4

ECHO 1 Full backup of "My Documents"
ECHO 2 Incremental backup of "My Documents"
ECHO 3 Restore "My Documents"
ECHO 4 Quit

set /P N=Type 1, 2, 3 or 4 then press ENTER:
IF "%N%"=="1" GOTO BACK
IF "%N%"=="2" GOTO INCR
IF "%N%"=="3" GOTO REST
IF "%N%"=="4" GOTO QUIT
IF NOT "%N%"=="1,2,3,4" GOTO STOP		

:BACK
echo ## Backup of the "My Documents" folder to "C:\Backup"
%backcmd% "%USERPROFILE%\My Documents" "%drive%\%date%"
ECHO You will now be returned to the menu screen
pause
cls
goto menu

:INCR
echo ## Incremental backup of the "My Documents" folder to "C:\Backup"
%incrcmd% "%USERPROFILE%\My Documents" "%drive%\%date%"
ECHO You will now be returned to the menu screen
pause
cls
goto menu

:REST
echo ## Restore files from "C:\Backup" folder to "My Documents"
%restcmd% "%drive%\%date%" "%USERPROFILE%\My Documents"
pause
cls
goto menu

:QUIT
ECHO The program will now close
pause
exit

:STOP
ECHO **This is not a valid command**
ECHO you will now be returned to the menu screen
pause
cls
GOTO MENU

I hope that makes some sense. :( It's been doing my nut in for 2 days now and all I need it to do is look for the latest backup and let me do an incremental to it or restore from it. Any help or ideas would be great.

Edit: I've hard coded it to backup/restore My Documents so if you run this .bat please remember to change it to something more suitable to you.
 
Thanks for the suggestion but I really need to learn how to use the Windows cmd line for block 2 of my college course in November. My lecturer has hinted that some of the scripts which we will be making will be for backup purposes, so I though I would get a jump on it early to try and learn as much as possible. Turns out it's harder than I thought.

The problem definitely lies with the %date% part for incremental and restore. I need to figure out the code to tell it that IF NOT EXIST %date% then go to the most recent backup and restore or continue with the incremental from that folder. The script works fine if you are backing up/restoring on the same date, as soon as the system date changes then it's broken because it can't see the backup folder :(
 
Thanks LizardKing (Feels strange typing that :p) but the end result is just the same.

What I have come up with is a short term solution :) I've simply taken the date=DD-MM-YYYY and made it date=MM-YYYY. Of course it will be broken again the following month but all I would have to day is create a new full backup and delete the old one and continue from there ... until the following month again.

Slight change to the code ...

Code:
@ECHO OFF
REM These are the variables for the backup drive
SET drive=c:\Backup
SET backcmd=xcopy /e/w/h/v
SET incrcmd=xcopy /e/w/h/d/v
SET restcmd=xcopy /e/w/h/v
SET backupdate="%date:~3,2%-%date:~6,6%"

GOTO MENU
:MENU
ECHO Please choose a command by pressing numbers 1-4

ECHO 1 Full backup of "My Documents"
ECHO 2 Incremental backup of "My Documents"
ECHO 3 Restore "My Documents"
ECHO 4 Quit

set /P N=Type 1, 2, 3 or 4 then press ENTER:
IF "%N%"=="1" GOTO BACK
IF "%N%"=="2" GOTO INCR
IF "%N%"=="3" GOTO REST
IF "%N%"=="4" GOTO QUIT
IF NOT "%N%"=="1,2,3,4" GOTO STOP		

:BACK
echo ## Back up of the "My Documents" folder to "C:\Backup"
%backcmd% "%USERPROFILE%\My Documents" "%drive%\%backupdate%"
ECHO You will now be returned to the menu screen
pause
cls
goto menu

:INCR
echo ## Incremental backup of the "My Documents" folder to "C:\Backup"
%incrcmd% "%USERPROFILE%\My Documents" "%drive%\%backupdate%"
ECHO You will now be returned to the menu screen
pause
cls
goto menu

:REST
echo ## Restore files from "C:\Backup" folder to "My Documents"
%restcmd% "%drive%\%backupdate%" "%USERPROFILE%\My Documents"
pause
cls
goto menu

:QUIT
ECHO The program will now close
pause
exit

:STOP
ECHO **This is not a valid command**
ECHO you will now be returned to the menu screen
pause
cls
GOTO MENU

It's not perfect but it's the best I can do :)
 
Last edited:
Change the @echo to on and see what format the command is being parsed as :)

Comes out as date=23-10-2010

use robocopy instead.

If I ever need to I will. I'm fed up with cmd prompt now. :p

I set out to find out as much as I could about backup batch files to get a head start in November when block 2 of my college course starts. Considering I knew very very little about creating batch filse I think I've done pretty well to get this far, even though it only half works :p

I'm going to give up now and hope that what I learned will be put into practice in November, knowing my luck we will probably be renaming and deleting directories hahaha. Cheers for the help lads.
 
Wow, thank you both very much. Lots of info to take away here and those two sites look very nice. I've clearly got a way to go because I don't really understand much of what is said above :p I'll be doing more work on it during the week I think. Thanks again.
 
Code:
@echo off
goto menu
:menu
cls

echo What would you like to do?

echo Choice

echo 1 Full Backup
echo 2 Incremental
echo 3 Restore
echo 4 Quit


:choice
set /p N=Please choose your command:
If "%N%"=="1" goto full
If "%N%"=="2" goto incremental
If "%N%"=="3" goto restore
If "%N%"=="4" goto quit

If Not "%N%"=="1,2,3,4" goto fail


:full
cls
echo Are you sure you would like a full backup? Type "Yes" or "No"
set /p N=[Yes,No]
If "%N%"=="Yes" goto fullbackup
If "%N%"=="No" goto menu

If Not "%N%"=="Yes,No" goto fail


:incremental
cls
echo Are you sure you would like a incremental backup? Type "Yes" or "No"
set /p N=[Yes,No]
If "%N%"=="Yes" goto incbackup
If "%N%"=="No" goto menu

If Not "%N%"=="Yes,No" goto fail


:restore
cls
echo Are you sure you would like to restore from a backup? Type "Yes" or "No"
set /p N=[Yes,No]
If "%N%"=="Yes" goto resback
If "%N%"=="No" goto menu

If Not "%N%"=="Yes,No" goto fail


:fullbackup
set mydate=%date:/=.%
md "C:\Backup\full_backup\%mydate%"
set /p from= Where would you like to backup from?
xcopy /e/y/w/h/v %from% C:\Backup\full_backup\%mydate%
pause
cls
goto menu


:incbackup
set mydate=%date:/=.%
md "C:\Backup\incremental\%mydate%"
set /p from= Where would you like to backup from?
xcopy /e/y/w/h/d/m/v %from% C:\Backup\incremental\%mydate%
pause
goto menu


:resback
set /p from= Where would you like to restore from?
set /p to= Where would you like to restore to?
xcopy /e/y/w/h/v %from% %to%
pause
goto menu


:quit
 pause
 exit


:fail
echo Invalid number or command
echo You will now be returned to the menu screen
pause
cls
goto menu

Finally got the correct solution. Me and a class mate had a brain storm session and we used a few bits of code from you guys to get this to work.

Now the user will be asked where the source file will come from instead of being hard coded for "My documents".

So if you run it, choose to backup and enter your source path is now creates a backup directory on c: called "Backup" with a dated folder inside that.

When you choose an incremental it creates another dated folder inside the Backup which will contain the files changed since last backup.

I hope that kinda makes sense. Thanks to some of your code I finally got it working. Feel free to try it out. :)
 
Back
Top Bottom