I use some Windows *.bat files to do backups, etc. Some things are quite difficult or crap to do (like error handling, messaging, etc.) so I decided to convert one of my scripts to a non-ui B4J program. With jShell you can start almost every windows executable program with parameters in both directions. So it seems that I don't use *.bat files no more
What it does:
- start MySqlDump (with parameters) to dump a DB to a file
- zip the dump file
- FTP upload the file (with parameters)
What you can add is some messaging if some of the functions should fail. You could start a HttpUtils job to insert a status in a remote db or send a mail.
What it does:
- start MySqlDump (with parameters) to dump a DB to a file
- zip the dump file
- FTP upload the file (with parameters)
What you can add is some messaging if some of the functions should fail. You could start a HttpUtils job to insert a status in a remote db or send a mail.
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
Sub Process_Globals
Public FTP As FTP
Public DBName, DBUser, DBPw, DumpFilePath, DumpFilename, ServerFTPPath, ServerFTPAddress, ServerFTPUser,ServerFTPPw As String
End Sub
Sub AppStart (Args() As String)
DBName="xxx"
DBUser="user"
DBPw="password"
DumpFilePath="C:\xampp\htdocs\xxxx\Dumps\"
DumpFilename=DBName & DateTime.Now & ".sql"
ServerFTPAddress="MyFTPServer"
ServerFTPUser="MyFTPUser"
ServerFTPPw="MyFTPUserPw"
ServerFTPPath="xxxxx/" ' note: simple directory UNDER the FTP path where you get to when you login with the user. Example: After login you are in "MyServer/somefolder" so with "xxxxx/" you store it in "MyServer/somefolder/xxxxx/"
'Dump DB
Log("Dump of db " & DBName & " initiated...")
Dim DumpDB As Shell
DumpDB.Initialize("DumpDB", "C:/xampp/mysql/bin/mysqldump.exe", Array As String("-u", DBUser, "-p" & DBPw, DBName,"--result-file="& DumpFilePath & DumpFilename))
DumpDB.WorkingDirectory = "C:/xampp/mysql/bin/"
DumpDB.Run(120000)
StartMessageLoop
End Sub
Sub ZIP_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
Log("ZIP: OK")
Log("Uploading " & DumpFilename & ".zip to server " & ServerFTPAddress)
FTP.Initialize("FTP", ServerFTPAddress, 21, ServerFTPUser, ServerFTPPw)
FTP.UploadFile(DumpFilePath, DumpFilename & ".zip", False, ServerFTPPath & DumpFilename & ".zip")
Else
Log("Error: " & StdErr & StdOut)
End If
'ExitApplication
End Sub
Sub DumpDB_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
Log("Dump: OK")
'ZIP dump file
Log("Zipping dumpfile " & DumpFilename)
Dim Zip As Shell
Zip.Initialize("Zip", "C:\Programme\7-Zip\7z.exe", Array As String("a", "-r",DumpFilePath & DumpFilename &".zip", DumpFilePath&DumpFilename))
Zip.WorkingDirectory = "C:\Programme\7-Zip\"
Zip.Run(120000)
Else
Log("Error: " & StdErr & StdOut)
End If
'ExitApplication
End Sub
Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
Dim s As String
s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
'Log(s)
End Sub
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
Log("FTP-Upload: OK")
If Success = False Then Log(LastException.Message)
FTP.Close
ExitApplication
End Sub