modify incremental_backup

Discussion in 'Scripting' started by Joe King, Feb 16, 2009.

  1. Joe King

    Joe King Guest

    this code (by Mark Minasi) creates a batch file to run ntbackup:

    Option Explicit

    Dim objFileSystem
    Dim objOutputFile
    Dim strOutputFile : strOutputFile = "C:\BatchFiles\NetworkBackup\Auto-
    Dim strDayOfWeekText
    Dim strMyDate
    Dim strstrMyMonth
    Dim oWSH
    Dim sCommand
    Dim strDelBackup
    Dim strMoveBackup
    Dim strBackup
    Dim strType : strType = "INCREMENTAL"
    Dim strIntHDD : strIntHDD = "D:\NetworkBackup"
    Dim strExtHDD : strExtHDD = "F:"

    strDayOfWeekText = WeekDayName(WeekDay(Date), True)

    If strDayOfWeekText = "Sat" Then
    strType = "NORMAL"
    strDayOfWeekText = "Week"
    End If

    strMyDate = Year(Date) & Right("0" &Month(Date),2) & Right("0" &Day

    If strDayOfWeekText = "Week" Then
    ' keep last three full backups
    strDelBackup = "del /F /S /Q " &strIntHDD&"\04-"
    strMoveBackup = "move "&strIntHDD&"\03-"&strDayOfWeekText&"\*.*
    strMoveBackup = "move "&strIntHDD&"\02-"&strDayOfWeekText&"\*.*
    strMoveBackup = "move "&strIntHDD&"\01-"&strDayOfWeekText&"\*.*
    ' keep last weeks incremental backup
    strDelBackup = "del /F /S /Q " &strIntHDD&"\02-"
    strMoveBackup = "move " &strIntHDD&"\01-" &strDayOfWeekText&"\*.*
    " &strIntHDD&"\02-" &strDayOfWeekText&"\"
    End If

    Set objFileSystem = CreateObject("Scripting.fileSystemObject")
    Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, True)
    objOutputFile.WriteLine("REM " &strDayOfWeekText&" - " &strMyDate&" - "
    &Time &" - " &Date)

    If strDayOfWeekText <> "Sun" And strDayOfWeekText <> "Mon" Then

    'backup 1
    'strBackup = "ntbackup backup \\act01\d$ /j "&strMyDate&" /f
    """&strIntHDD&"\01-"&strDayOfWeekText&"\act01-"&strMyDate&".bkf"" /L:S /M
    'backup 2
    'strBackup = "ntbackup backup \\nas01\c$\act /j "&strMyDate&" /f
    act-"&strMyDate&".bkf"" /L:S /M "&strType

    'copy backup to removable drive
    strBackup = "xcopy "&strIntHDD&"\01-"&strDayOfWeekText&"\*.*
    "&strExtHDD&"\01-"&strDayOfWeekText&"\*.* /F /H /R /V /Y"

    End If

    Set objFileSystem = Nothing

    Set oWSH = CreateObject("WScript.Shell")
    sCommand = strOutputFile
    Call oWSH.Run(sCommand)
    Set oWSH = Nothing

    While I like the idea in concept, I do not like its' execution

    I'd like to bypass ntbackup completely, and just call xcopy to backup to
    a folder (01-mmddyy), keep 4 (one per week) of these, then at week 5
    start overwriting week 1

    could you folks assist to get me started? (this will not scope-creep,


    Joe King, Feb 16, 2009
    1. Advertisements

  2. A couple of thoughts for starters:
    - Microsoft has officially deprecated xcopy.exe. Robocopy is the recommended
    replacement product.
    - Since you intend to invoke a console command (xcopy or robocopy) and since
    your logic is well within the grasp of a batch file, I suggest you drop the
    idea of using the hybrid solution of a script that invokes console commands.
    A batch file would require far fewer lines of code which means less
    debugging and easier maintenance.

    A few questions:
    - You mention "backup to a folder (01-mmddyy)". What's the "01" bit for?
    Does it vary? Wouldn't the date code uniquely identify the folder?
    - Do you back up each day or each week?
    - What do you get when you type the following command at a Command Prompt?
    echo %date%
    Pegasus \(MVP\), Feb 16, 2009
    1. Advertisements

  3. Joe King

    Joe King Guest

    xcopy has been just ducky in my context
    I just need to keep 3 additional increments, as opposed to the single
    weekly backup.

    the 01 would represent the first of 4 backups, 02, 03, 04 would follow. The
    fifth would be written into the 01 container


    then, 01-021609 would be deleted, and replaced by 01-031609

    yes, I suppose the datecode would accurately represent the folder.

    but - if I were to get hit by a bus, this may make it clearer for
    subsequent operators.

    i get: Mon 02/16/09
    Joe King, Feb 16, 2009
  4. I think if you get run over by a bus then your successors would be quite
    puzzled about the numbers 01..04. The folder 01-.. might be the most recent
    folder, or the oldest folder, or any one in between . . . Much better to
    stick to your original idea of using dates only.

    Try the following batch file:
    01. @echo off
    02. set Source=d:\My Data
    03. set BackupFolder=e:\My Backups
    05. for %%a in (%date%) do set MyDate=%%a
    06. set Dest=%BackupFolder%\%MyDate:/=-%
    07. if not exist "%BackupFolder%" md "%BackupFolder%"
    08. set MyDate=%date:/=%
    09. for /F "skip=4 delims=" %%a in ('dir /b /ad /o-d "%Folder%"') do (
    10. echo rd /s /q "%BackupFolder%\%%a")
    11. echo robocopy "%Source%" "%Dest%" *.*

    - Copy & paste the file into c:\MyBackup.bat. Do NOT retype it.
    - Adjust lines 02 and 03 to suit your environment.
    - Adjust line 11 by adding the appropriate switches for robocopy.
    - Remove the line numbers.
    - Open a Command Prompt.
    - Run the file from the Command Prompt as it is and check if it
    would do what you expect.
    - Remove the word "echo" in line 11 to activate the backup feature.
    - Run the file for 5 or six weeks.
    - Run the file from the Command Prompt and check if it would
    remove the appropriate backup folders.
    - Remove the word "echo" in line 10 to activate the folder deletion feature.
    And for fun: Compare the size of the above batch file with the size of your
    original VB Script file.
    Pegasus \(MVP\), Feb 16, 2009
  5. Joe King

    Joe King Guest

    line 4 is intentionally blank?
    Joe King, Feb 16, 2009
  6. Joe King

    Joe King Guest



    there's no removal of folders pre 4 backups.
    I simulated 7 weeks of running, and nothing was removed.
    Joe King, Feb 17, 2009
  7. This would be because you ignored my last instruction:
    "- Remove the word "echo" in line 10 to activate the folder deletion
    Pegasus \(MVP\), Feb 17, 2009
  8. Joe King

    Joe King Guest

    ummm - I'd like to concur, but I just can't.

    @echo off
    set Source=d:\test\misc
    set BackupFolder=H:\BU_test
    for %%a in (%date%) do set MyDate=%%a
    set Dest=%BackupFolder%\%MyDate:/=-%
    if not exist "%BackupFolder%" md "%BackupFolder%"
    set MyDate=%date:/=%
    for /F "skip=4 delims=" %%a in ('dir /b /ad /o-d "%Folder%"') do (rd /s /q
    robocopy "%Source%" "%Dest%" *.* /E /COPYALL

    I'll figger it out

    thnax for the assist
    Joe King, Feb 17, 2009
  9. To make it easy for you to see where each line starts I numbered each line.
    From the script above I am unable to tell if the following is a single long
    line, temporarily enclosed by three asterisks:

    ***for /F "skip=4 delims=" %%a in ('dir /b /ad /o-d "%Folder%"') do (rd /s
    /q "%BackupFolder%\%%a")***

    or if it is two lines, also temporarily enclosed by asterisks:

    *** for /F "skip=4 delims=" %%a in ('dir /b /ad /o-d "%Folder%"') do (rd /s
    /q ***
    *** "%BackupFolder%\%%a")***

    You're in an excellent position to judge! Putting the "echo" word back in
    again would give you a superb diagnostic tool.
    Pegasus \(MVP\), Feb 17, 2009
  10. Joe King

    Joe King Guest

    I understood the format change and it is one complete line.

    I don't understand the first component of the command.
    "for /F "skip=4 delims=""

    what command does the /F switch modify?

    it's not 'set', nor 'dir', nor 'rd'

    if I can isolate that, I'm on my way, and can finish this meself

    but you can see the 'echo' was removed, right?
    Joe King, Feb 17, 2009
  11. Yes, I can see that you removed the word "echo", and as I said before: You
    should restore it so that you can see with your own eyes which folder(s)
    would be deleted. This is the art of debugging: Making things visible that
    remain otherwise hidden.

    The /F switch allows you to add a number of options to the "for" command. I
    used three of them:
    for /F "skip=4 delims=" %%a in ('dir /b /ad /o-d "%Folder%"') . . .
    - skip=4: Skip the first four lines that are processed. In your case:
    Skip the four most recent folder names.
    - delims=: Treat each line as a single string. Do not break it up
    into space-delimited tokens. In your case: Process each folder
    name as a string, regardless of any embedded spaces.
    - ' (single quote): Execute the command between the single quotes and
    assign each line in turn as a string value to the loop variable %%a.

    Now put "echo" back again, then run the batch file from a Command Prompt and
    make a careful note of the "rd" commands you see on the screen. They are
    they key to your problem!
    Pegasus \(MVP\), Feb 17, 2009
  12. Joe King

    Joe King Guest

    after removing the 'echo' prior to the 'rd'

    it's trying to delete (captured) these default folders, none of which
    have been defined:

    rd /s /q "H:\BU\tmp"
    rd /s /q "H:\BU\Documents and Settings"
    rd /s /q "H:\BU\RECYCLER"
    rd /s /q "H:\BU\Intel"
    rd /s /q "H:\BU\DELL"
    rd /s /q "H:\BU\System Volume Information"
    Joe King, Feb 17, 2009
  13. Oh dear! The string
    ('dir /b /ad /o-d "%Folder%"')
    should actually read
    ('dir /b /ad /o-d "%BackupFolder%"')
    because %Folder% is not defined anywhere.

    Sorry for my oversight!
    Pegasus \(MVP\), Feb 17, 2009
  14. Joe King

    Joe King Guest

    all done

    replace "%Folder%" with "%BackupFolder%"

    and bobs yer uncle

    thnax for the patience

    Joe King, Feb 17, 2009
  15. Joe King

    Al Dunbar Guest

    /F modifies the FOR command.

    Al Dunbar, Feb 17, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.