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 :(
 
I'd be tempted to change (but i dont think it will solve your problem) your set=date %***% to something different. ie backupdate

%date% is a parameter already set by the system, you dont really want to be making changes system set parameters.
 
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:
You can remove the Incremental backup command if you use robocopy instead. By default it only copies new and changed files.

Either way, you should echo everything to the screen for debugging purposes. It could be the path is not being created correctly for instance.
 
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.
 
You need to remove the quotes from
SET backupdate="%date:~3,2%-%date:~6,6%"
so it reads
SET backupdate=%date:~3,2%-%date:~6,6%

Otherwise the command gets parsed as
xcopy /e/w/h/v "C:\Users\James\My Documents" "c:\Backup\"10-2010"\"
And as such will give you the file not found.

Although the batch file doesn't work on Win7 due to and im guessing permissions. i dont see why it shouldnt work on XP.

Also i noticed if you add a training slash to
%backcmd% "%USERPROFILE%\My Documents" "%drive%\%backupdate%"
so its
%backcmd% "%USERPROFILE%\My Documents" "%drive%\%backupdate%\"
Xcopy shouldnt ask you if its a directory or file.

Now if you want to learn, rewrite it without the goto's ;)
 
Not sure if I've completely understood what you are trying to do but a few things to look into.
An easier way to do what you are doing with the date variable is to just use the replace string method.

SET backupdate=%DATE:/=-%

But if you are going to use dates as folder names it is much better to reverse the format so that they list by name order. Something like...

FOR /F "tokens=1-3 delims=/" %%A IN ('ECHO %DATE%') DO (SET backupdate=%%C-%%B-%%A)

So you should get YYYY-MM-DD. Just makes things a bit easier to look at in explorer if you end up with loads of folders as they should always be in order.
You should also be aware that these can break on other machines if the format of their date variable is not the same. You can put in checks etc for this but depends on how complete you want to be.
As for dealing with the last backup etc a couple of ways I can think of would be either to write a log file and use that. You could either just overwrite it each time or append a line each time using your %backupdate% variable.

To get the last line/date you could 'FOR' it and keep setting a variable until it drops out the loop set as the last line of the file

FOR /F %%A IN (backups.txt) DO (SET LastBackUp=%%A)

Though that would be slow if the backups.txt log file grew to be large.

Or use something like....

FOR /F "tokens=2 delims=:" %%A IN ('FIND /C "-" backups.txt') DO (SET LastBackUp=%%A)
SET LastBackup=%LastBackup: =%
SET /A LastBackup=%LastBackup% - 1
FOR /F "skip=%LastBackUp%" %%A IN (backups.txt) DO (SET LastBackUp=%%A)

... which should count the number of lines in the file and then skip to the last line line and grab that as the folder name. But then if all you are worried about is the last backup date and have no need for a log file,or are just overwriting the log each time with the one line containing the last date then the above is a bit pointless.

Another way to look at without the log file is to basically do the above but with the DIR command. You could use it to order by date and do a FOR loop on the results...

dir /ad /od /b

but if you use the YYYY-DD-MM naming method from above you can also do it by name...

dir /ad /on /b

So the out put of those would replace your log file in the above code.

Like I say I may have misunderstood and there are probably better/cleverer ways to do this but hopefully it gives you something to go on. And if you are learning the CMD line bookmark these two sites...

ss64
robvanderwoude

As they can be a great help.
 
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. :)
 
That looks greast mate, how bizzare tho i have been trying to create a batch file and its kinda on the same lines as yours. Can i just ask you tho why have you put "cls" underneath the sub "goto menu's" surely would that not just close the application down? You no more than i do so if im wrong i hold my hands up totally :D

I'll show you what i have created, its my own little backup batch just fancied playng around and trying to understand it really, the only thing i cant do is where mine has a lot of prompts to press things like "enter" for example, i am trying to find a way to get it to automatically run if i set it up with task schedular. What i mean is if i am not at my machine there is no way i can press "enter" to carry on the process so trying to find the right "commands" or "script" that will let it run, did look at a timer count down but couldn't get it to work right.

Anyways great little script you have there good work

Heres mine

@Echo OFF
ECHO WELCOME TO LIAM'S BACKUP WIZARD

GOTO MENU
:MENU
ECHO PLEASE SELECT A COMMAND BELOW BY PRESSING NUMBERS 1-4 FOR THE ITEMS THAT YOU WISH TO BACKUP

ECHO ===== BACKUP MENU LIST =====
ECHO 1. iTUNES MUSIC BACKUP
ECHO 2. MY PICTURES BACKUP
ECHO 3. FOOTBALL MANAGER 2011 BACKUP
ECHO 4. QUIT



SET /P N=TYPE 1, 2, 3 OR 4 THEN PRESS ENTER:
IF "%N%"=="1" GOTO iTUNES
IF "%N%"=="2" GOTO MY PICTURES
IF "%N%"=="3" GOTO FOOTBALL MANAGER 2011
IF "%N%"=="4" GOTO QUIT


:iTUNES
ECHO YOU HAVE CHOSEN TO BACKUP THE iTUNES MUSIC FOLDER
PAUSE
ECHO ------------------------------- >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
ECHO %DATE% %TIME% >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
ECHO ------------------------------- >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
ECHO THE FOLLOWING FILES WILL BE COPIED
XCOPY "E:\Music\iTunes\iTunes Media\Music" "I:\Liam\XCOPY Backup Files\iTunes Music" /L /E /D /Y
PAUSE
XCOPY "E:\Music\iTunes\iTunes Media\Music" "I:\Liam\XCOPY Backup Files\iTunes Music" /E /D /Y >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
ECHO FILE COPY COMPLETE
GOTO MENU


: MY PICTURES
ECHO YOU HAVE CHOSEN TO BACKUP THE MY PICTURES FOLDER
PAUSE
ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
ECHO %DATE% %TIME% >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
ECHO THE FOLLOWING FILES WILL BE COPIED
XCOPY "E:\Pictures" "I:\Liam\XCOPY Backup Files\My Pictures" /L /E /D /Y
PAUSE
XCOPY "E:\Pictures" "I:\Liam\XCOPY Backup Files\My Pictures" /E /D /Y >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
ECHO FILE COPY COMPLETE
GOTO MENU


:FOOTBALL MANAGER 2011
ECHO YOU HAVE CHOSEN TO BACKUP THE FOOTBALL MANAGER 2011 SAVE GAME FOLDER
PAUSE
ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT"
ECHO %DATE% %TIME% >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT
ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT"
ECHO THE FOLLOWING FILES WILL BE COPIED
XCOPY "C:\Users\Liam\Documents\Sports Interactive\Football Manager 2011 Demo\games" /L /E /D /Y
PAUSE
XCOPY "C:\Users\Liam\Documents\Sports Interactive\Football Manager 2011 Demo\games" "I:\Liam\XCOPY Backup Files\Football Manager 2011" /E /D /Y >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT"
ECHO FILE COPY COMPLETE
GOTO MENU


:QUIT
ECHO THANK YOU FOR USING LIAM'S BACKUP WIZARD
PAUSE
CLS
 
Glad the OP got his working :) There's a few tweaks you can do to make it shorter and neater, i'll let you figure them out, its all part of the learning curve :)

Liamcrane one suggestion that would allow you to run it without prompts are command line parameters.
If you run the batchfile with the parameter "Backup.bat itunes"
and have the following line in your code before ":menu"
IF "%1"=="itunes" goto itunes

That will automatically skip out your menu
CLS is clear screen ;)
 
Thanks for that mate, dont mean to sound silly but how do i run the batch file with a specific parameter? The file is called liam's backup wizard.bat

Also once this parameter has missed out the menu is there a way to get it to overide the need of prompts in the :itunes script as there is pause commands in it etc. What i have been really trying to do is get a timer command so when the timer runs out it will automatically continue instead of having to press enter cos if im not at the machine i cant press enter :)

Thanks so much for your help
 
To skip any pause you could change the pause to

If not "%1"=="itunes" pause

And to run the batch file with a specific parameter (i got the term wrong it should be argument;)), create a shortcut, open the shortcut properties and add itunes to the end of the target line so it would read something like

target = "c:\path to batch file\liams backup wizard.bat itunes"

Hope that makes sense :) its been a looooong day!

Edit - You may need to change either the itunes section name or the argument name so there different.
 
Last edited:
makes sense mate i will give it a go. One more thing tho what does the "%1" actually stand for in command line terms?

Thanks so much again for your help

SPARC im looking to get into college to study things like programing etc what course you taking at college?
 
Last edited:
Hi matey i have created a shortcup to my batch file and whenver i try and add "auto backup" to the end of the target line it keeps say the specified target line is not valid. This is what i have done

"C:\Users\Liam\Desktop\Backup Batch Files\Liam's Auto Backup Wizard.bat auto backup"



In the batch file at the top before the "goto menu" option i have put



IF "%1"=="AUTO BACKUP" GOTO AUTO BACKUP



I have added a auto backup menu at the bottom of the batch file with no prompts



:AUTO BACKUP
ECHO ------------------------------- >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
ECHO %DATE% %TIME% >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
ECHO ------------------------------- >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"
XCOPY "E:\Music\iTunes\iTunes Media\Music" "I:\Liam\XCOPY Backup Files\iTunes Music" /E /D /Y >> "I:\Liam\XCOPY Backup Files\iTunes Music\BACKUP LOG.TXT"

ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
ECHO %DATE% %TIME% >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"
XCOPY "E:\Pictures" "I:\Liam\XCOPY Backup Files\My Pictures" /E /D /Y >> "I:\Liam\XCOPY Backup Files\My Pictures\BACKUP LOG.TXT"

ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT"
ECHO %DATE% %TIME% >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT
ECHO ------------------------------ >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT"
XCOPY "C:\Users\Liam\Documents\Sports Interactive\Football Manager 2011 Demo\games" "I:\Liam\XCOPY Backup Files\Football Manager 2011" /E /D /Y >> "I:\Liam\XCOPY Backup Files\Football Manager 2011\BACKUP LOG.TXT"
CLS



I have also tried entering the following arguments in task schedular

%1

"%1"

Auto Backup



Im kinda new to this so still learning my way around but just cant get it to by pass my standard goto menu
 
Back
Top Bottom