User:Ned Scott/Sandbox: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
(Blanked the page)
No edit summary
Line 1: Line 1:
=== Windows ===
This will allow you to make a regular backup of your MySQL database using the schedule of your choosing and saving the backups for however many days you choose. Recommend performing daily backups and saving for a minimum of 7 days. This will give you enough time to discover a problem with the DB and restore to a previous version that did not have problems.


To begin, go ahead and download the [http://www.7-zip.org/download.html command line version of 7zip]. We will use this to compress our MySQL dumps. Create a new folder in your C directory named KODISQLBACKUP. So you should have a C:\KODISQLBACKUP\ location now. In this location, create another new folder and name it zip. So you should now have C:\KODISQLBACKUP\zip. Extract the contents of the 7zip download to this location. Once complete you should have:
<br>
<pre>C:\KODISQLBACKUP\zip\7za.exe
C:\KODISQLBACKUP\zip\7-zip.chm
C:\KODISQLBACKUP\zip\license.txt
C:\KODISQLBACKUP\zip\readme.txt</pre>
We're going to create a down and dirty batch file to perform the actual backup, then we'll execute the batch script with the task scheduler built into Windows. For the purpose of these instructions, it is assumed that you are running MySQL 5.5 installed to the default location. If not, be sure to adjust your path(s) accordingly. It's also a good idea to save your backups to a network location to protect against local drive failure, so the paths here will reflect this. So here are the batch file contents:
<syntaxhighlight lang="winbatch" enclose="div">
:: All lines that start with 2 colons are comments, they do not affect the script itself. These comments are explanations of each command, as well as instructions for the things that you will need to edit. Pay close attention to all comments.
:: These lines do not NEED to be edited
set year=%DATE:~10,4%
set day=%DATE:~7,2%
set mnt=%DATE:~4,2%
set hr=%TIME:~0,2%
set min=%TIME:~3,2%
IF %day% LSS 10 SET day=0%day:~1,1%
IF %mnt% LSS 10 SET mnt=0%mnt:~1,1%
IF %hr% LSS 10 SET hr=0%hr:~1,1%
IF %min% LSS 10 SET min=0%min:~1,1%
set backuptime=%mnt%-%day%-%year%-%hr%-%min%
:: User name for DB - NOTE that root credentials are needed for this script.
set dbuser=root
:: User password - NOTE that the root credentials are needed for this script.
set dbpass=password
:: Path to location where you would like to save the errors log file. For simplicity, I keep mine in the same location as the backups.
set errorLogPath="\\REMOTE\KODI_Database\backups\dumperrors.txt"
:: We need to switch to the data directory to enumerate the folders
pushd "C:\ProgramData\MySQL\MySQL Server 5.5\data"
:: We will dump each database to it's own .sql so you can easily restore ONLY what is needed in the future. We're also going to skip the performance_schema db as it is not necessary.
FOR /D %%F IN (*) DO (
IF NOT [%%F]==[performance_schema] (
SET %%F=!%%F:@002d=-!
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --user=%dbuser% --password=%dbpass% --databases --routines --log-error=%errorLogPath%  %%F > "\\REMOTE\KODI_Database\backups\%%F.%backuptime%.sql"
) ELSE (
echo Skipping DB backup for performance_schema
)
)
::Now to zip all of the .sql files in this folder and move the resulting .zip files to our network location.
"c:\KODISQLBACKUP\zip\7za.exe" a -tzip "\\REMOTE\KODI_Database\backups\FullBackup.%backuptime%.zip" "\\REMOTE\KODI_Database\backups\*.sql"
::Now we'll delete the unzipped .sql files
del "\\REMOTE\KODI_Database\backups\*.sql"
::Now we'll delete all zip files older than 30 days. You can adjust the number of days to suit your needs, simply change the -30 to whatever number of days you prefer. Be sure you enter the path to your backup location.
Forfiles -p \\REMOTE\KODI_Database\backups\ -s -m *.* -d -30 -c "cmd /c del /q @path"
popd
</syntaxhighlight>
Save this as Kodibackup.bat at C:\KODISQLBACKUP
Make ''sure'' you include the .bat at the end.

Revision as of 16:43, 11 June 2016

Windows

This will allow you to make a regular backup of your MySQL database using the schedule of your choosing and saving the backups for however many days you choose. Recommend performing daily backups and saving for a minimum of 7 days. This will give you enough time to discover a problem with the DB and restore to a previous version that did not have problems.

To begin, go ahead and download the command line version of 7zip. We will use this to compress our MySQL dumps. Create a new folder in your C directory named KODISQLBACKUP. So you should have a C:\KODISQLBACKUP\ location now. In this location, create another new folder and name it zip. So you should now have C:\KODISQLBACKUP\zip. Extract the contents of the 7zip download to this location. Once complete you should have:

C:\KODISQLBACKUP\zip\7za.exe

C:\KODISQLBACKUP\zip\7-zip.chm

C:\KODISQLBACKUP\zip\license.txt

C:\KODISQLBACKUP\zip\readme.txt

We're going to create a down and dirty batch file to perform the actual backup, then we'll execute the batch script with the task scheduler built into Windows. For the purpose of these instructions, it is assumed that you are running MySQL 5.5 installed to the default location. If not, be sure to adjust your path(s) accordingly. It's also a good idea to save your backups to a network location to protect against local drive failure, so the paths here will reflect this. So here are the batch file contents:

:: All lines that start with 2 colons are comments, they do not affect the script itself. These comments are explanations of each command, as well as instructions for the things that you will need to edit. Pay close attention to all comments.

:: These lines do not NEED to be edited
set year=%DATE:~10,4%
set day=%DATE:~7,2%
set mnt=%DATE:~4,2%
set hr=%TIME:~0,2%
set min=%TIME:~3,2%

IF %day% LSS 10 SET day=0%day:~1,1%
IF %mnt% LSS 10 SET mnt=0%mnt:~1,1%
IF %hr% LSS 10 SET hr=0%hr:~1,1%
IF %min% LSS 10 SET min=0%min:~1,1%

set backuptime=%mnt%-%day%-%year%-%hr%-%min%


:: User name for DB - NOTE that root credentials are needed for this script.
set dbuser=root

:: User password - NOTE that the root credentials are needed for this script.
set dbpass=password

:: Path to location where you would like to save the errors log file. For simplicity, I keep mine in the same location as the backups.
set errorLogPath="\\REMOTE\KODI_Database\backups\dumperrors.txt"

:: We need to switch to the data directory to enumerate the folders
pushd "C:\ProgramData\MySQL\MySQL Server 5.5\data"


:: We will dump each database to it's own .sql so you can easily restore ONLY what is needed in the future. We're also going to skip the performance_schema db as it is not necessary.

FOR /D %%F IN (*) DO (

IF NOT [%%F]==[performance_schema] (
SET %%F=!%%F:@002d=-!
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --user=%dbuser% --password=%dbpass% --databases --routines --log-error=%errorLogPath%  %%F > "\\REMOTE\KODI_Database\backups\%%F.%backuptime%.sql"
) ELSE (
echo Skipping DB backup for performance_schema
)
)

::Now to zip all of the .sql files in this folder and move the resulting .zip files to our network location.

"c:\KODISQLBACKUP\zip\7za.exe" a -tzip "\\REMOTE\KODI_Database\backups\FullBackup.%backuptime%.zip" "\\REMOTE\KODI_Database\backups\*.sql"

::Now we'll delete the unzipped .sql files

del "\\REMOTE\KODI_Database\backups\*.sql"

::Now we'll delete all zip files older than 30 days. You can adjust the number of days to suit your needs, simply change the -30 to whatever number of days you prefer. Be sure you enter the path to your backup location.
Forfiles -p \\REMOTE\KODI_Database\backups\ -s -m *.* -d -30 -c "cmd /c del /q @path"

popd

Save this as Kodibackup.bat at C:\KODISQLBACKUP Make sure you include the .bat at the end.