How? DOS *.bat to move files to ...\%date%\%time%\

Discussion in 'Software' started by ghostcorps, Nov 19, 2008.

  1. ghostcorps

    ghostcorps Private E-2

    Hi Guys,


    I have been trying to make a simple batch script to create database backups and move then into a folder named by date, and a sub folder by time.

    This is what I have so far:

    Code:
    @echo off
    
    @echo Backing Up Server1
    mysqldump -A -Q -R -c -e --lock-tables=FALSE -uXXXX -pXXXX -hX.X.X.1 > c:\backup\01.sql
    
    @echo Backing Up Server2
    mysqldump -A -Q -R -c -e --lock-tables=FALSE -uXXXX -pXXXX -hX.X.X.2  > c:\backup\02.sql
    
    @echo Backing Up Server3
    mysqldump -A -Q -R -c -e --lock-tables=FALSE-uXXXX -pXXXX -hX.X.X.3  > c:\backup\03.sql
    
    set folderdate=%date:~7,2%-%date:~4,2%-%date:~10,4%
    mkdir c:\backup\%folderdate%
    set foldertime=%time:~0,2%-%time:~3,2%
    mkdir c:\backup\%folderdate%\%foldertime%
    move c:\backup\*.sql c:\backup\%folderdate%\%foldertime%\


    I have this scheduled to run every 12hours from 8PM. However, while the batch that runs at 8PM is successful, the 8AM files remain unmoved. In fact in the AM, the 'foldertime' folder is created outside the 'folderdate' folder and sites next to the files that have been created but not moved.

    And yet, at 8Pm everything works like a charm. Presumably this has to do with using 24hour time, as the folders are names in 24hr, while XP uses AM/PM in the 'modified' column. I do not see any logic that would cause this to happen the way it does, but I hope someone can point me in the right direction.


    thanks

    =^_^=
     
  2. PEBKAC

    PEBKAC Private First Class

    Two troubleshooting thoughts...

    1.) I'm assuming that you are running the batch file using Task Scheduler. If so, is it one job or two jobs per day? If two jobs, check to make sure that both scheduled tasks are running with credentials (preferably the same credentials) that have access to the backup folder.

    2.) Consider adding to the batch file "echo" statements which dump your variables to a log file so that it's easier for you to do troubleshooting when the failure occurs...

    Code:
    echo "%folderdate%","%foldertime%">>c:\backup\BackupLog.txt
    When the job fails, look at the log file to see if the values would be considered valid folder/file names. If you place it at the end of the script, it will also tell you if everything above that ran and that the script didn't crash somewhere before reaching the end.
     
  3. K1ngSteve

    K1ngSteve Guest

    do you have to choose which type of back up to use? like normal, incremental, differential or is that only for ntbackup?
     
  4. ghostcorps

    ghostcorps Private E-2

    Thanks PEBKAC,

    I am only using one schedule, set to run every 12 hours.

    I ran successful tests using this script:
    Code:
    set folderdate=%date:~7,2%-%date:~4,2%-%date:~10,4%
    set foldertime=%time:~0,2%-%time:~3,2%
    mkdir c:\backup\%folderdate%
    mkdir c:\backup\%folderdate%\%foldertime%
    copy c:\backup\test.sql c:\backup\%folderdate%\%foldertime%\
    echo "%folderdate%","%foldertime%">>c:\backup\BackupLog.txt
    The log shows the following:
    --------------------
    "20-11-2008","10-05"
    --------------------

    But this is what I have noticed all along, and yet if I leave it to run on its own, one run will work and one will not.

    Is it worth setting it to run every 12.05 hours, to negate any time conflicts perhaps?
     
  5. PEBKAC

    PEBKAC Private First Class

    Right... But what you want to see is what the variables look like when it actually fails. After the scheduled job fails at midnight, look at the log again and make sure that the variables look the way you expect them. If they look valid, then the problem may lie somewhere else.

    It wouldn't hurt.
     
  6. ghostcorps

    ghostcorps Private E-2

    Thanks :)

    Upon checking last nights record, I can see that there is a space in the AM time where there should not be a space.

    "20-11-2008","20-06"
    Vs
    "21-11-2008"," 8-06"


    I can not see where the space is coming from, but at least I know the problem!

    Thanks again


    =^_^=
     
  7. ghostcorps

    ghostcorps Private E-2

    I have found that DOS, in it's infinite wisdom, does not enter a leading Zero in times pre-10AM.

    To get around this, I have added the line in bold to add a '0':

    set foldertime=%time:~0,2%
    if "%time:~0,1%"==" " set foldertime=0%hh:~1,1%

    nb, I have decided I don't need the minutes.

    Thanks again for your help :)
     
  8. PEBKAC

    PEBKAC Private First Class

    Glad you were able to track the problem down. Two other quick tips for you...

    If you have a variable (which does not contain spaces in the actual string you want), and you'd like to trim off any leading or trailing spaces, you can do so with a "FOR"...

    for %%a in (%Variable1%) do set Variable2=%%a

    "Variable2" would contain the data of "Variable1" without any leading or trailing spaces.

    ...Or you could add quotes to your names/paths to just create the folders with the spaces...

    Code:
    set folderdate=%date:~7,2%-%date:~4,2%-%date:~10,4%
    mkdir "c:\backup\%folderdate%"
    set foldertime=%time:~0,2%-%time:~3,2%
    mkdir "c:\backup\%folderdate%\%foldertime%"
    move c:\backup\*.sql "c:\backup\%folderdate%\%foldertime%"
    
    Regardless, I'm glad to hear you resolved it.
     
  9. ghostcorps

    ghostcorps Private E-2

    ahhh ok, thanks!! :)

    The workaround still failed, so I will try your suggestions. I would prefer to not have the spaces in the names, but it is a nice simple solution.

    =^_^=
     
  10. PEBKAC

    PEBKAC Private First Class

    Along the lines of what you had come up with; how about something like this?...

    Code:
    set Hr=%time:~0,2%
    if "%time:~0,1%"==" " set Hr=0%time:~1,1%
    set FolderTime=%Hr%%time:~3,2%%time:~6,2%
    
     
  11. ghostcorps

    ghostcorps Private E-2

    Thanks :)

    The quotation marks didn't fix it unfortunately.

    However, your suggestion above seems to work so far:) I added dashes between the time elements just for looks, but I have a good feeling that I will find it all tip-top tomorrow morning. :cool

    Thanks for all your help

    =^_^=
     
  12. ghostcorps

    ghostcorps Private E-2

    Thanks again for your help :)

    It now works a treat

    =^_^=
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds