Backup/Restore MySQL data when build BAT file

Backup/Restore MySQL data when build BAT file

Postby richard-service » Sun Sep 03, 2017 2:16 am

Hi All
I use this code below:
Code: Select all  Expand view  RUN
 
METHOD RunDatabaseBackup() CLASS TWinCraneErp
LOCAL fp
LOCAL chost      := GetServer()
LOCAL names      := 'utf8'
LOCAL nport      := 3306
LOCAL db         := GetLoginDataBase()
LOCAL cDriver    := CurDrive()
LOCAL cDriverDir := GetCurDir()
LOCAL cdir       := cDriverDir + "\BackUpData\"      // cDriver + ":\" + cDriverDir + "\BackUpData\"
LOCAL cfile      := 'BackupDatabase.sql'

          fp:=FCREATE("
bak.bat")
      WRITE(fp,"
@ECHO OFF")
          FWRITE(fp,CRLF+"
mysqldump.exe --host "+AllTrim(chost)+" --default-character-set="+names+" --port="+cvaltochar(nport)+" -u root -p1234 "+AllTrim(db)+" > "+alltrim(cdir)+iif(right(alltrim(cdir),1)=="\","","\")+alltrim(cFile))
          FCLOSE(fp)
          WaitRun("
call bak.bat",0)

RETURN NIL

Image

WaitRun("call bak.bat",0) => run it not work. If I windows file work fine.
Any solution be welcome!!
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 804
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: Backup/Restore MySQL data when build BAT file

Postby Horizon » Sun Sep 03, 2017 8:54 am

Hi Richard,

Can you try it without "call"

WaitRun("bak.bat",0)
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Backup/Restore MySQL data when build BAT file

Postby Marc Vanzegbroeck » Sun Sep 03, 2017 9:00 am

Richard,

I use ShellExecute.
In your case it will be someting like this :
Code: Select all  Expand view  RUN
ShellExecute( 0, 'OPEN', "cmd" ,"/c .\mysqldump.exe --host "+AllTrim(chost)+" --default-character-set="+names+" --port="+cvaltochar(nport)+" -u root -p1234 "+AllTrim(db)+" > "+alltrim(cdir)+iif(right(alltrim(cdir),1)=="\","","\")+alltrim(cFile), , 1 )
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Backup/Restore MySQL data when build BAT file

Postby richard-service » Sun Sep 03, 2017 3:22 pm

Horizon wrote:Hi Richard,

Can you try it without "call"

WaitRun("bak.bat",0)


HI Hakan,

You're right, work fine.
Thaks a lot.
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 804
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: Backup/Restore MySQL data when build BAT file

Postby richard-service » Sun Sep 03, 2017 3:23 pm

Marc Vanzegbroeck wrote:Richard,

I use ShellExecute.
In your case it will be someting like this :
Code: Select all  Expand view  RUN
ShellExecute( 0, 'OPEN', "cmd" ,"/c .\mysqldump.exe --host "+AllTrim(chost)+" --default-character-set="+names+" --port="+cvaltochar(nport)+" -u root -p1234 "+AllTrim(db)+" > "+alltrim(cdir)+iif(right(alltrim(cdir),1)=="\","","\")+alltrim(cFile), , 1 )


Hi Marc,
you suggestion it and work fine.
Thanks a lot.
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 804
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: Backup/Restore MySQL data when build BAT file

Postby nageswaragunupudi » Tue Sep 05, 2017 4:56 am

FWH oCn:BackUp() is simple to use and works fast and well.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10681
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Backup/Restore MySQL data when build BAT file

Postby richard-service » Tue Sep 05, 2017 5:29 am

nageswaragunupudi wrote:FWH oCn:BackUp() is simple to use and works fast and well.


Mr.Rao,

Yes, I think so. But now I use TMySQL 3rd
I have a idea. I can use TMySQL and FWH MariaDb/MySql Backup and Restore Together run it?

Code: Select all  Expand view  RUN

//TMySQL

     oServer:= TMySQLServer():New( GetServer(), GetLoginId(), GetLoginPass() )
     IF oServer:NETERR()
         oServer:End()
         oServer:= TMySQLServer():New( GetServer(), "mysql", "" )
         IF oServer:NETERR()
            MsgStop( "MySQL伺服器未開或者伺服器有防火牆或網路不通!", "停止" )
            lOK := .F.
            RETURN lOK
         ELSE
            MsgStop( "MySQL伺服器已有啟動中,但有其它問題導致無法連上!", "停止" )
            lOK := .F.
            RETURN lOK
         ENDIF
      ENDIF
      cLoginTable := GetLoginDataBase()
      oServer:SelectDB(cLoginTable)

//FWH MariaDb/MySql Backup and Restore

   oCn := mysql_Connect( cHost, cUsr, cPsw)
   ? cResult:=oCn:Backup('prova','c:\pp\prova.sql')
   ? oCn:Restore("c:\pp\prova.sql",NIL,NIL,"prueba")
 
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 804
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan

Re: Backup/Restore MySQL data when build BAT file

Postby nageswaragunupudi » Tue Sep 05, 2017 5:55 am

I can use TMySQL and FWH MariaDb/MySql Backup and Restore Together run it?

Yes, you can.

In fact you can use FWH mariadb functionality in addition to TMySql in your programs without any conflict with TMySql.

Your above program works.

However you can totally simplify the backup program and run it silently in the nights as a scheduled task and forget about it.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10681
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Backup/Restore MySQL data when build BAT file

Postby nageswaragunupudi » Tue Sep 05, 2017 6:22 am

You can use a simple program like this:
Code: Select all  Expand view  RUN

#include "fivewin.ch"

function Main()

   local oCn
   local cBackUpFolder  := "c:\pp\"  // should end with a backslash
   
   FWCONNECT oCn HOST GetServer() USER GetLoginId() PASSWORD GetLoginPass() DATABASE GetLoginDataBase()
   if oCn == nil
      FWLOG "
Connect fail"
   else
      oCn:BackUp( nil, cBackUpFolder )
      oCn:Close()
      oCn   := nil
   endif
   
return nil

// functions like GetServer() etc.  

Make sure that the program does not contain any screen I/O.
Now add this to the scheduled tasks on the server (with user as SYSTEM) to run every night at your convenient time.

Assuming that your database name is "prova", this program will save backups each day in a different folder with weekday's name.
On Sunday night it saves backup as "c:\pp\Sunday\prova.sql". On Monday night it saves backup as "c:\pp\Monday\prova.sql" and so on. After one week, it overwrites that weekdays backup. This way we have 7 backups at any time.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10681
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Backup/Restore MySQL data when build BAT file

Postby richard-service » Tue Sep 05, 2017 7:18 am

nageswaragunupudi wrote:You can use a simple program like this:
Code: Select all  Expand view  RUN

#include "fivewin.ch"

function Main()

   local oCn
   local cBackUpFolder  := "c:\pp\"  // should end with a backslash
   
   FWCONNECT oCn HOST GetServer() USER GetLoginId() PASSWORD GetLoginPass() DATABASE GetLoginDataBase()
   if oCn == nil
      FWLOG "
Connect fail"
   else
      oCn:BackUp( nil, cBackUpFolder )
      oCn:Close()
      oCn   := nil
   endif
   
return nil

// functions like GetServer() etc.  

Make sure that the program does not contain any screen I/O.
Now add this to the scheduled tasks on the server (with user as SYSTEM) to run every night at your convenient time.

Assuming that your database name is "prova", this program will save backups each day in a different folder with weekday's name.
On Sunday night it saves backup as "c:\pp\Sunday\prova.sql". On Monday night it saves backup as "c:\pp\Monday\prova.sql" and so on. After one week, it overwrites that weekdays backup. This way we have 7 backups at any time.


Mr.Rao
Nice good job. Try it and add my plan code.
Best Regards,

Richard

Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
User avatar
richard-service
 
Posts: 804
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 31 guests