Loading ...
Sorry, an error occurred while loading the content.

Re: Daily Backup Without Overwrite

Expand Messages
  • GBL
    ... WeekDay IF ERRORLEVEL 7 GOTO Sat IF ERRORLEVEL 6 GOTO Fri IF ERRORLEVEL 5 GOTO Thu IF ERRORLEVEL 4 GOTO Wed IF ERRORLEVEL 3 GOTO Tue IF ERRORLEVEL 2 GOTO
    Message 1 of 10 , Jun 25, 2010
    • 0 Attachment
      --- In batchworld@yahoogroups.com, Aristos Vasiliou <aristos@...> wrote:
      >
      > I am currently using this batch file to daily backup my documents on my second hard disk. XCOPY /Y /E /D /C %USERPROFILE%\Documents\*.* D:\Backup\Documents
      >
      > What I need to do is modify the batch file so that it won't overwrite D:\Backup\Documents, but create a new folder named D:\Backup\Documents_Monday and then on Tuesday create a new folder named D:\Backup\Documents_Tuesday and so on.
      >
      > It should do this for the whole week, so that if I make a mistake today, and discover it after two days, I can restore from three days back. And then on Monday it can overwrite again.
      >
      > How can I do this?
      >
      > Thanks

      Many years ago, I wrote a very small command line program named Weekday.exe (see http://www.incodesystems.com/PRODUCTS/WEEKDAY.HTM) that sets the ERRORLEVEL to a number that corresponds to the day of the week. We use that to create batch files that behave differently based on the day of the week. All you do is:
      ----------- clip
      WeekDay
      IF ERRORLEVEL 7 GOTO Sat
      IF ERRORLEVEL 6 GOTO Fri
      IF ERRORLEVEL 5 GOTO Thu
      IF ERRORLEVEL 4 GOTO Wed
      IF ERRORLEVEL 3 GOTO Tue
      IF ERRORLEVEL 2 GOTO Mon
      IF ERRORLEVEL 1 GOTO Sun
      ----------- clip
      A simpler way would be to deference the ERRORLEVEL pseudo environment variable, like this:
      ----------- clip
      WeekDay
      GOTO Day%ERRORLEVEL%
      :Day1
      :Day2
      :Day3
      :Day4
      :Day5
      :Day6
      :Day7
      ----------- clip

      Here's another way to get a similar result, without the WeekDay.exe program.
      ----------- clip
      FOR /F "usebackq delims==" %%i IN (`DATE /T`) DO SET WeekDay=%%i
      SET WeekDay=%WeekDay:~0,3%
      GOTO %WeekDay%
      :Mon
      :Tue
      :Wed
      :Thu
      :Fri
      :Sat
      :Sun
      ----------- clip

      We have written a large number of helper programs that effectively extend the functionality of batch files (see http://www.incodesystems.com/PRODUCTS/BATCH.HTM). These programs can be used to create batch files that appear to the user like they are running a Windows program. A relatively new one, named Echox.exe (see http://www.incodesystems.com/PRODUCTS/ECHOX.HTM), can be used to echo text to the screen in color and at X,Y locations specified on the command line. It also allows you to echo text without ending with a carriage return, line feed sequence, so the cursor remains on the end of the text. We are always looking for suggestions to improve our offering of batch file helper programs.

      Glenn Lawler
      www.incodesystems.com
      mailto:gblawler@...
    • Aristos Vasiliou
      Hello, It s not really about saving space. I m just curious to see how it can be improved :) And I must say what you did to it, sure made it simpler. It seems
      Message 2 of 10 , Jun 29, 2010
      • 0 Attachment
        Hello,

        It's not really about saving space. I'm just curious to see how it can be improved :) And I must say what you did to it, sure made it simpler.

        It seems that MD "C:\webserver\wwwroot\%1-%dow2%\" 2>nul creates the local folder based on the database1 name instead of the website1 name. Then winscp tries to download files based on the website1 name, and since the folder does not exist, it stops. How can I fix this?

        Thanks
        ________________________________
        From: batchworld@yahoogroups.com [batchworld@yahoogroups.com] on behalf of foxidrive@... [foxidrive@...]
        Sent: Wednesday, June 30, 2010 5:00 AM
        To: batchworld@yahoogroups.com
        Subject: Re: [BATCH WORLD] Daily Backup Without Overwrite



        On Wed, 30 Jun 2010 11:37:38 +1000, foxidrive@...<mailto:foxidrive%40lavabit.com> wrote:

        >On Tue, 29 Jun 2010 00:48:17 +0000, Aristos Vasiliou
        ><aristos@...<mailto:aristos%40aristos.net>> wrote:
        >
        >>So what can this batch file do?
        >>
        >>First it downloads the MySQL databases from Bluehost and saves them in a folder. Then it takes those downloaded database dumps and imports them on the local database. After that, it starts downloading each website folder placing it in wwwroot, adding to the folder name, the current day, for seven days, so that I can go back to previous backups. It also creates log files for each website downloaded, just to make sure there were no errors.
        >>
        >>I run this batch file every night, so now I know that I have all my websites locally, and I know that they are working because I can look at them through my browser and everything is working perfectly.
        >>It�s working, but I know it can be improved by more experienced people than me, so I�m posting it below. Any suggestions/modifications are welcome.
        >
        >The old saying applies here hehe
        >"If it's not broken then don't try to fix it."
        >
        >You could put the steps in a single routine and call the routine with a
        >single forindo command, but it will only save you a kilobyte on your
        >HDD. :)

        If you want to explore this though, here's something that should work
        but I haven't tested it.

        The four lines in the :routine at the bottom have a > at the start of
        the line which needs to be removed.

        @echo off
        ::Create Day Variable
        ::----------------------------------------------------------
        set TmpFile="%temp%.\tmp.vbs"
        echo> %TmpFile% n=Now
        echo>>%TmpFile% With WScript
        echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
        echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
        echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
        echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
        echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
        echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
        echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
        echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
        echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
        echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
        echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
        echo>>%TmpFile% End With
        cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
        call "%temp%.\tmp.bat"
        del "%temp%.\tmp.bat"
        del %TmpFile%
        set TmpFile=
        set stamp=%year%-%month%-%day%_%hour%.%min%.%sec%

        ::----------------------------------------------------------

        ::Set Database Variables
        ::----------------------------------------------------------
        set mysqldump="C:\webserver\mysql\bin\mysqldump.exe"
        set mysql="C:\webserver\mysql\bin\mysql.exe"
        set pth="C:\webserver\custom\dbasedump"
        set sqlswitches=--opt

        set remotehost=remotehost.com
        set remoteuser=myremoteusername
        set remotepass=myremotepassword
        set localuser=mylocalusername
        set localpass=mylocalpassword

        ::----------------------------------------------------------

        ::Set WinSCP Variables
        ::----------------------------------------------------------
        set winscp=C:\webserver\custom\files\winscp\winscp.exe
        set switches=option batch on
        set credentials=open username:password@...<mailto:password%40remotehost.com>
        set execute=synchronize local
        set logpath=C:\webserver\custom\logs\winscp
        set lfolder=C:\webserver\wwwroot
        set rfolder=/home1/remotehostusername/public_html

        call :routine database1 website1
        call :routine database2 website2
        call :routine database3 website3
        call :routine database4 website4
        call :routine database5 website5
        call :routine database6 website6
        call :routine database7 website7
        call :routine database8 website8
        call :routine database9 website9
        call :routine database10 website10
        call :routine database11 website11
        goto :EOF

        :routine
        > %mysqldump% %sqlswitches% -h%remotehost% -u%remoteuser% -p%remotepass% %1 > %pth%\%1
        > %mysql% -u%localuser% -p%localpass% %1 < %pth%\%1
        > MD "C:\webserver\wwwroot\%1-%dow2%\" 2>nul
        > %winscp% /log=%logpath%\%1.log /console /command "%switches%" "%credentials%" "%execute% %lfolder%\%2-%dow2% %rfolder%/%2" "close" "exit"





        [Non-text portions of this message have been removed]
      • foxidrive@lavabit.com
        On Tue, 29 Jun 2010 00:48:17 +0000, Aristos Vasiliou ... The old saying applies here hehe If it s not broken then don t try to fix it. You could put the
        Message 3 of 10 , Jun 29, 2010
        • 0 Attachment
          On Tue, 29 Jun 2010 00:48:17 +0000, Aristos Vasiliou
          <aristos@...> wrote:

          >So what can this batch file do?
          >
          >First it downloads the MySQL databases from Bluehost and saves them in a folder. Then it takes those downloaded database dumps and imports them on the local database. After that, it starts downloading each website folder placing it in wwwroot, adding to the folder name, the current day, for seven days, so that I can go back to previous backups. It also creates log files for each website downloaded, just to make sure there were no errors.
          >
          >I run this batch file every night, so now I know that I have all my websites locally, and I know that they are working because I can look at them through my browser and everything is working perfectly.
          >It’s working, but I know it can be improved by more experienced people than me, so I’m posting it below. Any suggestions/modifications are welcome.

          The old saying applies here hehe
          "If it's not broken then don't try to fix it."

          You could put the steps in a single routine and call the routine with a
          single forindo command, but it will only save you a kilobyte on your
          HDD. :)
        • foxidrive@lavabit.com
          ... If you want to explore this though, here s something that should work but I haven t tested it. The four lines in the :routine at the bottom have a at the
          Message 4 of 10 , Jun 29, 2010
          • 0 Attachment
            On Wed, 30 Jun 2010 11:37:38 +1000, foxidrive@... wrote:

            >On Tue, 29 Jun 2010 00:48:17 +0000, Aristos Vasiliou
            ><aristos@...> wrote:
            >
            >>So what can this batch file do?
            >>
            >>First it downloads the MySQL databases from Bluehost and saves them in a folder. Then it takes those downloaded database dumps and imports them on the local database. After that, it starts downloading each website folder placing it in wwwroot, adding to the folder name, the current day, for seven days, so that I can go back to previous backups. It also creates log files for each website downloaded, just to make sure there were no errors.
            >>
            >>I run this batch file every night, so now I know that I have all my websites locally, and I know that they are working because I can look at them through my browser and everything is working perfectly.
            >>It’s working, but I know it can be improved by more experienced people than me, so I’m posting it below. Any suggestions/modifications are welcome.
            >
            >The old saying applies here hehe
            >"If it's not broken then don't try to fix it."
            >
            >You could put the steps in a single routine and call the routine with a
            >single forindo command, but it will only save you a kilobyte on your
            >HDD. :)

            If you want to explore this though, here's something that should work
            but I haven't tested it.

            The four lines in the :routine at the bottom have a > at the start of
            the line which needs to be removed.



            @echo off
            ::Create Day Variable
            ::------------------------------------------------------------------------
            set TmpFile="%temp%.\tmp.vbs"
            echo> %TmpFile% n=Now
            echo>>%TmpFile% With WScript
            echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
            echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
            echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
            echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
            echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
            echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
            echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
            echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
            echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
            echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
            echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
            echo>>%TmpFile% End With
            cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
            call "%temp%.\tmp.bat"
            del "%temp%.\tmp.bat"
            del %TmpFile%
            set TmpFile=
            set stamp=%year%-%month%-%day%_%hour%.%min%.%sec%

            ::------------------------------------------------------------------------

            ::Set Database Variables
            ::------------------------------------------------------------------------
            set mysqldump="C:\webserver\mysql\bin\mysqldump.exe"
            set mysql="C:\webserver\mysql\bin\mysql.exe"
            set pth="C:\webserver\custom\dbasedump"
            set sqlswitches=--opt

            set remotehost=remotehost.com
            set remoteuser=myremoteusername
            set remotepass=myremotepassword
            set localuser=mylocalusername
            set localpass=mylocalpassword

            ::------------------------------------------------------------------------

            ::Set WinSCP Variables
            ::------------------------------------------------------------------------
            set winscp=C:\webserver\custom\files\winscp\winscp.exe
            set switches=option batch on
            set credentials=open username:password@...
            set execute=synchronize local
            set logpath=C:\webserver\custom\logs\winscp
            set lfolder=C:\webserver\wwwroot
            set rfolder=/home1/remotehostusername/public_html

            call :routine database1 website1
            call :routine database2 website2
            call :routine database3 website3
            call :routine database4 website4
            call :routine database5 website5
            call :routine database6 website6
            call :routine database7 website7
            call :routine database8 website8
            call :routine database9 website9
            call :routine database10 website10
            call :routine database11 website11
            goto :EOF

            :routine
            > %mysqldump% %sqlswitches% -h%remotehost% -u%remoteuser% -p%remotepass% %1 > %pth%\%1
            > %mysql% -u%localuser% -p%localpass% %1 < %pth%\%1
            > MD "C:\webserver\wwwroot\%1-%dow2%\" 2>nul
            > %winscp% /log=%logpath%\%1.log /console /command "%switches%" "%credentials%" "%execute% %lfolder%\%2-%dow2% %rfolder%/%2" "close" "exit"
          • foxidrive@lavabit.com
            On Tue, 29 Jun 2010 17:27:39 +0000, Aristos Vasiliou ... %1 is replaced with the database name and %2 is replaced with the website name I think it was a case
            Message 5 of 10 , Jun 30, 2010
            • 0 Attachment
              On Tue, 29 Jun 2010 17:27:39 +0000, Aristos Vasiliou
              <aristos@...> wrote:

              >It seems that MD "C:\webserver\wwwroot\%1-%dow2%\" 2>nul creates the local folder based on the database1 name instead of the website1 name. Then winscp tries to download files based on the website1 name, and since the folder does not exist, it stops. How can I fix this?


              %1 is replaced with the database name and %2 is replaced with the
              website name

              I think it was a case of using this as below (remove the > again)
              "MD "C:\webserver\wwwroot\%2-%dow2%\" 2>nul"


              :routine
              > %mysqldump% %sqlswitches% -h%remotehost% -u%remoteuser% -p%remotepass% %1 > %pth%\%1
              > %mysql% -u%localuser% -p%localpass% %1 < %pth%\%1
              > MD "C:\webserver\wwwroot\%2-%dow2%\" 2>nul
              > %winscp% /log=%logpath%\%1.log /console /command "%switches%" "%credentials%" "%execute% %lfolder%\%2-%dow2% %rfolder%/%2" "close" "exit"
            Your message has been successfully submitted and would be delivered to recipients shortly.