Kodi Community Forum

Full Version: Simplest Way to Backup and restore MySQL database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am currently running two MySQL databases for two seperate profiles on my WHS 2011 machine i want to upgrade the OS but i want to be able to make a backup of my Databases and be able to restore them after the OS installtion

what is the simplest way to do this it would be a benefit to backup the complete database and be able to restore it the scanning in of content is not too much of a hassle i would prefer not to but could rescan everything in again if i had to what i mostly care about is watched statuses and making sure i don't lose them
Using built in tools via commandline: http://blog.winhost.com/using-mysqldump-...asetables/

or

Download MySQL Workbench and make a backup with it. http://dev.mysql.com/downloads/tools/workbench/
On window 7 I was able to do it by backing up the database from the programdata folder, going through the initial mysql setup installing and creating the xbmc user on the new machine stop mysql service copy databases and restart mysql.
(2013-05-20, 14:00)bilbonvidia Wrote: [ -> ]On window 7 I was able to do it by backing up the database from the programdata folder, going through the initial mysql setup installing and creating the xbmc user on the new machine stop mysql service copy databases and restart mysql.

Because this could overly complicate things I did not mention it.

I know that works, but it is not the recommended way to do it.
Also there is no guarantee that it will work when you have a newer version of MySQL. It would however never hurt to make an additional backup of the actual MySQl file structure on disk.
(2013-05-20, 15:48)Kibje Wrote: [ -> ]Because this could overly complicate things I did not mention it.

I know that works, but it is not the recommended way to do it.
Also there is no guarantee that it will work when you have a newer version of MySQL. It would however never hurt to make an additional backup of the actual MySQl file structure on disk.
Agreed, it will work for the same version of MySQL, but usually gets borked when going between versions.

Not that that won't happen with a proper backup too... Confused
There is much less chance of something happening with a proper SQL dump of the database. Of course it happens sometimes, but yeah.
(2013-05-19, 11:42)Kibje Wrote: [ -> ]Download MySQL Workbench and make a backup with it. http://dev.mysql.com/downloads/tools/workbench/

They removed that function

what i use

Code:
@echo off
::Set Variables:
set mySqlPath=C:\Program Files\MySQL\MySQL Server 5.5
set dbUser=root
set dbPassword=XXXXX
set dbName=myvideos75
set dbName2=sissi_xbmc75

::File names:
set file=%dbName%.%DATE%_%time:~0,2%%time:~3,2%%time:~6,2%.sql
set file2=%dbName2%.%DATE%_%time:~0,2%%time:~3,2%%time:~6,2%.sql

:: Error log path - Important in debugging your issues:
set errorLogPath="I:\Administrator\Documents\Logs\SQL\dumperrors.txt"

:: Backup folder path:
set path=I:\MySQL\backups

:: Number of days to retain backup files:
set retaindays=5

::Execution of backup:
echo Running dump for database %dbName% ^> ^%path%\%file%
"%mySqlPath%\bin\mysqldump.exe" -u %dbUser% -p%dbPassword% --log-error=%errorLogPath% %dbName% > "%path%\%file%"

echo Running dump for database %dbName2% ^> ^%path%\%file2%
"%mySqlPath%\bin\mysqldump.exe" -u %dbUser% -p%dbPassword% --log-error=%errorLogPath% %dbName2% > "%path%\%file2%"

::Delete backups older then X days
echo "Deleting zip files older than %retaindays% days"
Forfiles.exe -p %path% -s -m *.sql -d -%retaindays% -c "cmd /c del /q @path"

echo Done!
Output: myvideos75.17-05-2013_ 80000.sql and sissi_xbmc75.17-05-2013_ 80000.sql

If the deletion doesnt work, find Forfiles.exe in your system32 folder and place in the same directory as the script. I have 2 dbs that why i have 2 backups created.
Nice batch file, thanks for sharing.
My backup method is very similar to saitoh

I have a scheduled backup on my WIN7 server that exports each database separate from a Batch script every 2 days. It then uses the 7zip command line zipper to create a zipped backup and then adds the date to the backup. This allows a complete backup of my Music, Video and Media Portal (TV Schedules) to be backed up.

I have gone through 5 server rebuilds and never lost a database. There are some tricks though but it's all simple (I know little about MYSQL structure but google helps a lot):

Code:
@ECHO OFF

ECHO===============================================
ECHO.        XBMC SQL BACKUP UTILITY
ECHO===============================================


SET BACKUPPATH=D:\HTPC_DATA\SQL_Backup\
SET MYSQLPATH=C:\Program Files\MySQL\MySQL Server 5.5\bin
IF NOT EXIST %BACKUPPATH%\ (
MD %BACKUPPATH%
)

"%MYSQLPATH%\mysqldump" -u xbmc -pxbmc xbmc_video75 > %BACKUPPATH%\XBMC_VIDEO75.sql

"%MYSQLPATH%\mysqldump" -u xbmc -pxbmc xbmc_music32 > %BACKUPPATH%\XBMC_MUSIC32.sql

"%MYSQLPATH%\mysqldump" -u xbmc -pxbmc mptvdb > %BACKUPPATH%\MPTVDB.sql



7za.exe a %BACKUPPATH%\%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%_XBMC.SQL.7z "%BACKUPPATH%\*.SQL" -mx1

DEL %BACKUPPATH%\*.SQL

What you will see is pretty simple. It is a .BAT file that makes the backup and you simply need to fill in the path to SQL, the user and passwords and change the name of the tables to backup in the commands. It then exports to the directory set and then runs the 7zip utility to compress (just needs to be in the same folder as the script).

You can then setup an automated schedule to make a backup in case you ever screw your database.

So, how do you reload a backup?

This is where I always use to get stuck because after installing MYSQL I would try and restore the tables but it would always fail so here is what I do to restore on a NEW SQL install:

- After installing MYSQL you need to run XBMC and let it create a new SQL database. Don't import anything using XBMC, just let it create the tables.

I use this script to import the backups:


Code:
@ECHO OFF
ECHO===============================================
ECHO.        XBMC SQL RESTORE UTILITY
ECHO===============================================

::BACKUP FILE LOCATION
SET BACKUPLOCATION=D:\HTPC_DATA\SQL_Backup
SET FILENAME=XBMC_MUSIC18.SQL

:SQL DETAILS
SET SQLEXEPATH=C:\Program Files\MySQL\MySQL Server 5.5
SET IMPORTDBNAME=xbmc_music18
SET IMPORTUSER=xbmc
SET IMPORTPASS=xbmc



:: "BACKUPLOCATION" IS THE PATH TO THE SQL BACKUP FILE. NO TRAIL SLASH.
:: "FILENAME" IS THE NAME OF THE SQL BACKUP FILE WITH EXTENSION.
:: "SQLEXEPATH" IS THE PATH TO THE SQL DIRECTORY ONLY. NO TRAIL SLASH
:: "IMPORTDBNAME" IS THE NAME OF THE DATABASE NAME TO IMPORT FILE TO.
::  **DB MUST BE CREATED BEFORE IMPORTING
:: "IMPORTUSER" IS THE USERNAME TO USETO IMPORT THE DATA
:: "IMPORTPASS" IS THE DATABASE PW FOR THE USER

:: Edit the details above to import an SQL backup file into mySQL database.



:: PROGRAM-----------------------------------------------------------------------------
ECHO.
ECHO This utility will restore an SQL Database backup for the following:
ECHO.
ECHO BACKUP FILE:     %BACKUPLOCATION%\%FILENAME%
ECHO TARGET DATABASE: %IMPORTDBNAME%
ECHO.
ECHO If this is not correct, please close this window now and edit the details.

PAUSE
ECHO.
ECHO Are you Sure?
ECHO.
PAUSE


IF NOT EXIST %BACKUPLOCATION%\ (
ECHO ERROR (config): Backup Location Not Found. Please Check Path In File!
PAUSE
)

IF NOT EXIST %SQLPATH%\ (
ECHO ERROR (config): SQL Location Not Found. Please Check Path In File!
PAUSE
)

IF NOT EXIST "%BACKUPLOCATION%\%FILENAME%" (
ECHO ERROR (config): Backup FILE Not Found. Please Check Path In File!
PAUSE
)

"%SQLEXEPATH%\bin\mysql" -h localhost -u %IMPORTUSER% -p%IMPORTPASS% %IMPORTDBNAME% < "%BACKUPLOCATION%\%FILENAME%"

ECHO.
ECHO If no errors are displayed, the import should be a success!
ECHO If any errors are displayed above, these need to be resolved.
ECHO Check SQL Database for imported data.
ECHO.
PAUSE
EXIT

As you can see you just fill in the same details for the table to import too and the file to read from and the locations then run the file. If there is an issue with something you will get some form of error but if all goes well you should get nothing but the last little blurb at the end there and checking MYSQL with HeidiSQL or something you should see the table size reflects your library and XBMC should work as normal.

I am no expert here so can only assist to a basic degree but I aways make sure I know what I am doing and how it all works for WHEN I need to do it all. No point having an SQL database if you cannot recover or care for it when things qo wrong. Hope that helps.