chuckp2010
asked on
Help with Robocopy script
Hi
I have a Windows 2003 server where I have an application that logs daily files. The folder is C:\Logs, and each day, a folder with the date is created and, within this, a log file with the date too.
For example:
C:\Logs\20100812\Log_20100 812.txt
This process continues. So, after a week, I would have folders such as:
C:\Logs\20100813
C:\Logs\20100814
C:\Logs\20100815
etc, each with their own log file within.
I would like to copy each day's log file to another server and was going to use Robocopy via a scheduled task. Basically, each day, I would want to upload the previous day's log file. But I'm not sure how to upload the previous day's log file only, and leave the rest alone? Does anyone know what sort of switches I could use?
I have a Windows 2003 server where I have an application that logs daily files. The folder is C:\Logs, and each day, a folder with the date is created and, within this, a log file with the date too.
For example:
C:\Logs\20100812\Log_20100
This process continues. So, after a week, I would have folders such as:
C:\Logs\20100813
C:\Logs\20100814
C:\Logs\20100815
etc, each with their own log file within.
I would like to copy each day's log file to another server and was going to use Robocopy via a scheduled task. Basically, each day, I would want to upload the previous day's log file. But I'm not sure how to upload the previous day's log file only, and leave the rest alone? Does anyone know what sort of switches I could use?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So there is both a Wed and Thurs file, but you only want to grab Wed?
If that's the case then you would need to play with /MINAGE as well
I believe that you can set minage to 0 and it will exclude the last 24 hours, but you'd need to play with it to get your timestamps the way you want them.
If that's the case then you would need to play with /MINAGE as well
I believe that you can set minage to 0 and it will exclude the last 24 hours, but you'd need to play with it to get your timestamps the way you want them.
Yes, makes sense!
You should be able to also use the /minage:1 switch to say the folder/ file needs to be at a minimum 1 day old.
You should be able to also use the /minage:1 switch to say the folder/ file needs to be at a minimum 1 day old.
ASKER
Hmm, still not working. The problem is that for yesterday, the log could have been written to last at any point in that 24 hour period, depending on the application's activity.
Is there anyway with Robocopy to just say - "if the file was written to at any point yesterday, then copy it to xx"
Is there anyway with Robocopy to just say - "if the file was written to at any point yesterday, then copy it to xx"
ASKER
Actually
From testing, this seems to work:
robocopy \\server\c$\logs\\ \\server2\c$\copied\\ /maxage:2 /minage:1 /s
I just want to be sure though :) The previous day's log file can be written to any time in yesterday's 24 hour period, depending on the app usage, so would maxage:2 and minage:1 cover all bases?
From testing, this seems to work:
robocopy \\server\c$\logs\\ \\server2\c$\copied\\ /maxage:2 /minage:1 /s
I just want to be sure though :) The previous day's log file can be written to any time in yesterday's 24 hour period, depending on the app usage, so would maxage:2 and minage:1 cover all bases?
I just tested it:
MAXAGE:2 will give you the previous 24 hours (i.e. the 24 hours prior to the current 24 hour period)
MINAGE:1 will skip anything from the current 24 hours
MAXAGE:2 will give you the previous 24 hours (i.e. the 24 hours prior to the current 24 hour period)
MINAGE:1 will skip anything from the current 24 hours
ASKER
Thanks..but when you say 24 period, do you mean "day"?
So say I wanted the script to run at 01:00 every day.
On Thursday 01:00, the script runs with /maxage:2 and minage:1
The last time the Wed log file was updated was Wed 22:00
Would Wed's file still be copied over?
So say I wanted the script to run at 01:00 every day.
On Thursday 01:00, the script runs with /maxage:2 and minage:1
The last time the Wed log file was updated was Wed 22:00
Would Wed's file still be copied over?
If you know the exact folder name that you want to copy - which you do, it';s yesterdays date in yyyymmdd format - you could build a string variable in a batch file to consist of the year, month and day; then Robocopy from the folder of that name, without switches.
I've used a similar code snippet for a while in a similar situiation, creating folders for a specific day's backup. The batch file code is a bit obscure, but seems to work OK. Note that the details are sensitive to the date format that is configured on your system
Where I got it: http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/
I've used a similar code snippet for a while in a similar situiation, creating folders for a specific day's backup. The batch file code is a bit obscure, but seems to work OK. Note that the details are sensitive to the date format that is configured on your system
Where I got it: http://www.tech-recipes.com/rx/956/windows-batch-file-bat-to-get-current-date-in-mmddyyyy-format/
REM to test your date format - see what order things come out
For /f "tokens=1,2,3,4,5 delims=/. " %%a in ('date/T') do set CDate=%%a-%%b-%%c-%%d-%%e
@echo testdate = %CDate%
rem Once you know the order, Combine them in whatever way suits you - this works on mine but may be
different on yours
For /f "tokens=1,2,3,4,5 delims=/. " %%a in ('date/T') do set CDate=%%c%%b%%a
@echo date = %CDate%
md \\newserver\logs\%Cdate%
robocopy c:\logs\%Cdate% \\newserver\logs\%Cdate%
note - some line breaks have been corrupted in the code I posted: LINE 7 should be attached to the end of line 5. You can lose all the testing and ECHO commands after you know it iis working.
It's a rolling 24 hour period, so no, a file that was timestamped with 22:00 Wed would not get copied on Thurs 01:00 task as it's only 3 hours old.
ASKER
Thanks both -
Sheshiro - if I ran the task on Friday at 00:00, then I guess that would cover it? The last 24 hours would exclude all of Thursday, and the previous 24 hours would include all of Wednesday?
aflockhart - thanks, I will check that out!
Sheshiro - if I ran the task on Friday at 00:00, then I guess that would cover it? The last 24 hours would exclude all of Thursday, and the previous 24 hours would include all of Wednesday?
aflockhart - thanks, I will check that out!
"Sheshiro - if I ran the task on Friday at 00:00, then I guess that would cover it? The last 24 hours would exclude all of Thursday, and the previous 24 hours would include all of Wednesday?"
- Correct, the wed 22:00 file is 26 hours old and would be picked up on Friday 00:00
- Correct, the wed 22:00 file is 26 hours old and would be picked up on Friday 00:00
ASKER
Thanks Sheshiro.
Can you think of anyway that I can perform the same objective (i.e copy over the previous day's [Wed] file only), but have the Robocopy run at some point on the next day (Thurs) rather than Friday 00:00?
Can you think of anyway that I can perform the same objective (i.e copy over the previous day's [Wed] file only), but have the Robocopy run at some point on the next day (Thurs) rather than Friday 00:00?
If the file stops getting written to @ 22:00 Wed, then setting the robocopy to run @ 22:15 Thursday would do it.
Other than that, I'm sure there's a For loop that could do it... but I'm not a codemonkey :-)
Other than that, I'm sure there's a For loop that could do it... but I'm not a codemonkey :-)
ASKER
Regarding:
"
/MAXAGE:n :: MAXimum file AGE – exclude files older than n days/date.
If you want only the last days files:
Robocopy <source> <destination> /MAXAGE:1"
Let's say that I set that up as a scheduled task today (Wed). Remember that I want to upload the -previous- day's files every day, i.e. on a Thursday, I want to upload Wednesday's files etc.
So I set up the scheduled Robocopy command above to run at 01:00 every day. So on Thursday morning at 01:00, I want Wednesday's log file copied over.
But - because I specifying to exclude folders older than one day, am I not going to upload Thursday's file (i.e. a file that has an age of 0)?
Hope my q makes sense :)