Solved

daily MSDOS batch script (.bat/.cmd) to do xcopy/e/c/h/D:d-m-y wrt 1st Sunday of the month

Posted on 2008-10-14
3
2,467 Views
Last Modified: 2012-05-05
Hi,

I need a daily .bat/.cmd MSDOS script that runs at 1am that will :

a)If it's the first Sunday of the month, do :
xcopy/E/C/H/Y  n:\folder1  n:\backup\folder1\yyyymmdd  > n:\log\yyyymmdd_folder1.txt 2>> n:\log\yyyymmdd
xcopy/E/C/H/Y  n:\folder2  n:\backup\folder2\yyyymmdd  > n:\log\yyyymmdd_folder2.txt 2>> n:\log\yyyymmdd
     (ie take a full backup of folder1 & folder2;   yyyymmdd is the date the script run
      & n:  is a shared drive/folder)

b)If it's other days, do :
xcopy/E/C/H/Y/D:d-m-y  n:\folder1  n:\backup\folder1\yyyymmdd  > n:\log\yyyymmdd_folder1.txt 2>> n:\log\yyyymmdd
xcopy/E/C/H/Y/D:d-m-y  n:\folder2  n:\backup\folder1\yyyymmdd  > n:\log\yyyymmdd_folder2.txt 2>> n:\log\yyyymmdd
(this is a differential backup   to backup files that have been modified or  created since the
  last first Sunday of the month)

c)
in step (a) above, if it's the first Sunday of the month, it would delete away (using "deltree ?")
the folders that are 5 months old & subsequent differential backups' folders with respect to
that full backup's date.


Purpose of the >> and 2>>  is to create logs for review next morning if there's
errors with backup.

If you need to break this up into multiple scripts, it's fine with me.
Single script would be nice but not essential


0
Comment
Question by:sunhux
  • 2
3 Comments
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 500 total points
ID: 22713799
In a command prompt can you paste the output of

echo %date%
0
 

Author Comment

by:sunhux
ID: 22719573
On my PC's MSDOS prompt, the output is :
15-10-2008
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 500 total points
ID: 22722199
OK. Give this a try. You'll need to remove the 6 ECHO in captials for the script to actually perform the operation.

The removing of the 5 month old backup folders will only remove differentials only in that month. Differentials that spill over to the next month will be removed next month's first Sunday.
FOR /f "Tokens=2,*" %%a in ('REG QUERY "HKCU\Control Panel\International" /v sShortDate ^| Find /i "sShortDate"') do Set CurrentsShortDate=%%b
REG ADD "HKCU\Control Panel\International" /v sShortDate /d "d-M-yyyy" /f
Set DayOfTheWeek=%date:~0,3%
Set Day=%date:~4,2%
Set Month=%date:~7,2%
Set Year=%date:~10,4%
REG ADD "HKCU\Control Panel\International" /v sShortDate /d "%CurrentsShortDate%" /f
Echo Day of the week=%DayOfTheWeek%
Echo Day=%Day%
Echo Month=%Month%
Echo Year=%Year%
 
Set MonthsAGoYear=%Year%
Set /a MonthsAGo=1%Month%-5
 
If %MonthsAGo% lss 101 Set /a MonthsAGoYear-=1
If %MonthsAGo% lss 101 Set /a MonthsAGo+=12
 
Set MonthsAGo=%MonthsAGo:~-2%
 
IF /i "%DayOfTheWeek%" NEQ "sun" GOTO DIFFERENTIAL
IF %Day% GTR 7 GOTO DIFFERENTIAL
 
:FULL
ECHO    xcopy/E/C/H/Y  n:\folder1  n:\backup\folder1\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder1.txt 2>> n:\log\%Year%%Month%%Day%
ECHO    xcopy/E/C/H/Y  n:\folder2  n:\backup\folder2\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder2.txt 2>> n:\log\%Year%%Month%%Day%
 
FOR /f "Tokens=*" %%a in ('DIR /ad /b "n:\backup\folder1\%MonthsAGoYear%%MonthsAGo%*"') DO ECHO RD /q /s "n:\backup\folder1\%%a"
FOR /f "Tokens=*" %%a in ('DIR /ad /b "n:\backup\folder2\%MonthsAGoYear%%MonthsAGo%*"') DO ECHO RD /q /s "n:\backup\folder2\%%a"
GOTO :EOF
 
:DIFFERENTIAL
ECHO    xcopy/E/C/H/Y/D:%Day%-%Month%-%Year%  n:\folder1  n:\backup\folder1\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder1.txt 2>> n:\log\%Year%%Month%%Day%
ECHO    xcopy/E/C/H/Y/D:%Day%-%Month%-%Year%  n:\folder2  n:\backup\folder2\%Year%%Month%%Day%\  > n:\log\%Year%%Month%%Day%_folder2.txt 2>> n:\log\%Year%%Month%%Day%

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Scheduled task not running 5 81
call multiple sqlcmd's within one bat file - with logging 9 52
Combine DSQuery result into loop script 6 71
Recursively Delete Files 5 95
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question