Jump to content

Recommended Posts

Posted

My boss today saw me playing around with Robocopy at work, I was using it to backup my laptop to one of our servers and he asked me if it was possible to create a script that could be used to create a folder on an external hard drive with the file name based upon the current date and time and then use Robocopy to backup specfic folders to. Not only that it would have to do a full backup initally followed an incremental back there after.

 

ie:

Backup1-1-11 Full backup

Backup2-1-11 Incremental backup

Backup2-1-11 Incremental backup

 

and so on...

 

Now my scripting knowledge is basically nil apart from doing logon scripts but I think it is possible but I need to find a definate answer.

Posted
Pretty straightforward. If no-one posts a solution in the meantime, I'll post a sample script tomorrow.

 

Thank you I've found a script online that I'm trying to modify as well but I think it is a bit on the complicated side. It seems to work but it uses a lot of variables and seems to have written to backup someones user profile which is useful but not what I'm really after

Posted (edited)

This should get you there.

 

You can remove the echos and comments if you like; they're for testing so you can see how it works.

 

 

:: script to robocopy files to a folder named after the current date

:: set the time & date into a variable
for /f "tokens=1,2" %%a in ('time/t') do set time=%%a
for /f "tokens=1,2" %%a in ('date/t') do set date=%%a


:: date has '/' characters which we can't use in a file or folder
:: use the 'set' command to substitute the / to a -
set date=%date:/=-%


:: time has ':' characters which we can't use in a file or folder
:: change to a '.'
set time=%time::=.%


echo Time=%time%
echo Date=%date%


:: you can use the date as is, or we can split it up into YYYYMMDD
:: to make file sorting more logical

for /f "tokens=1,2,3 delims=-" %%a in ('echo %date%') do (
set dd=%%a
set mm=%%b
set yyyy=%%c
)

echo DD=%dd%
echo MM=%mm%
echo YY=%yyyy%



:: set the destination folder

:: if you don't care about file sorting
set destfolder=X:\BACKUPS\%date%\FULL or whatever

:: if you want full sorting on YYYYMMDD
set destfolder=X:\BACKUPS\%yyyy%%mm%%dd%\FULL or whatever


:: if you want full sorting + time
set destfolder=X:\BACKUPS\%yyyy%%mm%%dd%\%time%\FULL or whatever





:: call robocopy
robocopy  %destfolder% /E

 

It assumes you're using the European date model (DD/MM/YYYY) not the crazy American date.

Edited by jinnantonnixx
  • Thanks 2
Posted

Neatly done, jinnantonnixx. I did not know the trick of using tokens, and it might help me with a script bug I need to fix.

 

But haven't you missed out part of what rshortland was asking for? He wanted the copies after the first to be incremental, with only changed files. The only way I can see to do that would be to use the archive attribute, which will not work if anything else is using it - ie if you do a separate incremental backup on those files that relies on the archive bit, then altering it when you do your robocopying will mean the files get missed out of that backup. You need to make sure the archive attribute is unset on all the files after the initial copy, I think the command for that is:

attrib -a  /S /D

Then for the incremental copies after the initial copy, use the /m switch with the robocopy command.

robocopy  %destfolder% /E /M

Posted

You're quite right, I did miss the requirement for incremental copies.

 

As you suggest, the /M option will work, copying files that changed since your last copy.

/M :: copy only files with the Archive attribute and reset

 

Also, the /MAXAGE option in Robocopy is a more absolute way of doing this.

/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.

/MINAGE:n : MINimum file AGE - exclude files newer than n days/date.

(If n < 1900 then n = no of days, else n = YYYYMMDD date).

 

Note the option which allows the use of YYYYMMDD. We've already calculated this in our script, so we can use this option quite easily.

Posted

/MAXAGE probably would be better than using /m. For a start it avoids the risk of problems with something else relying on the archive attribute.

 

However the YYYYMMDD date the script calculates is the current date, whereas you would want to give MAXAGE the date of the last backup. Perhaps saving the date in a file each time the script is run would work?

 

Another potential bug might be to rely on only the date, rather than date and time of the last backup. Example:

14/11/11 10:00 - save file X

14/11/11 11:00 - run the robocopy script - file X will be saved in the 20111114 folder

16/11/11 11:00 - run the robocopy script again with /MAXAGE 20111114. File X is saved in the 20111116 folder, even though it hasn't changed since we last ran the script.

 

Maybe that is not a problem, but it does mean the backups will not be strictly incremental. Oddly I cannot see an option for robocopy to exclude files older than a date and time.

  • 1 year later...
Posted
I have a batch file that can do this - it doesn't use the date modified bit - but does use the date from the file system, that usefull at all?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...