Android Tutorial MySQL: Backup/Recover Databases

So you have some nice MySQL databases but what about backups? Copying the db files is a very bad idea and will lead into corruption. Here's a small script (copy it to a Windows batch file = xxxxx.bat) which checks/optimizes the database, makes a backup, zips it and copies the *.zip file to the Google Drive folder to sync it.

B4X:
set ts=%date% %time:~0,2%-%time:~3,2%-%time:~6,2%
C:\xampp\mysql\bin\mysqlcheck -u root -pMYPW --auto-repair --optimize DBNAME
C:\xampp\mysql\bin\mysqldump -u root -pMYPW DBNAME > "C:\xampp\htdocs\enctrans\Dumps\DBNAME%ts%.sql"
C:\Programme\7-Zip\7z.exe a -r "C:\Users\Klaus\Google Drive\DBNAME%ts%.zip" "C:\xampp\htdocs\enctrans\Dumps\DBNAME%ts%.sql"
pause

Please change the path's to "mysqldump" and "mysqlcheck" and the other folders/names.

ts=just a timestamp so everytime the script runs a new file is created
DBNAME = name of the db
MYPW=root user pw (notice the -p as a parameter without space before it)

I have 7zip on my machine. So just change the path and/or name to your zip tool.

Hint: Browse the file. You can easily change some parameters before reloading it (like create the db/tables at the start or not). It can be used to move a database to another machine, too.

Recover:

B4X:
mysql -uroot -pMYPW DBNAME < FILENAME.sql

Another way is to use the MySql/MyphpAdmin surface but there could be a timeout when you dump/recover big databases.
 
Top