Feeds:
Posts
Comments

Archive for October, 2013

Haloo … prenz … dah lama ane gak posting .. gak ada waktu dan kena penyakit M yaitu Malas 😀

dah gak perlu banyak cat cit cot dah … Ane mau bagiin scripting buat backup/restore pake mysqldump dan teman temannya, dan ini running hanya di WINDOWS environment ya ..

——code_begin——–

@echo off

echo +—————————————————–+
echo + MySQL Database Backup Script +
echo + +
echo + By (c) Man’z, 2011 +
echo +—————————————————–+
rem — DB name area —
set mysql_db=db_hrms
rem ——————–

rem — MySQL server location —
set mysql_pos=”E:\mysql-5.1.30-winx64\bin”
rem —————————–

rem — MySQL host, user and password DB —
set mysql_server=-h<<IP_HOST>> -uroot -p<<password_root>>
rem —————————————

rem — Don’t touch this area —
set mysql_table_option=–add-drop-table –complete-insert –extended-insert –hex-blob –skip-comments –tables
set db_tables=..\%mysql_db%_tables.txt
rem —————————–

rem — Where is 7zip program for compression —
set zip_pos=”C:\Program Files (x86)\7-Zip”
rem ———————————————

rem — Where is data will be extracted —
set data_pos=e:\DataLog
rem —————————————

rem — From this point — don’t touch this area, otherwise you know what you do, take a risks —
for /f “tokens=1-5 delims=/ ” %%d in (“%date%”) do set dbdump=%mysql_db%-%%g-%%e-%%f
set pos=%data_pos%\%dbdump%

echo Creating directory [ %pos% ]
mkdir %pos%

echo Extracting Tables [ %mysql_db%_tables.txt ]
%mysql_pos%\mysql %mysql_server% %mysql_db% –execute=”show tables” > %data_pos%\%mysql_db%_tables.txt

echo dump Table [ %db_tables% ]
for /f “tokens=1-5 delims=/ ” %%a IN (%pos%\%db_tables%) do (
if %%a neq Tables_in_db_hrms (
echo Extracting Table [ %%a ]
%mysql_pos%\mysqldump.exe %mysql_server% –result-file=%pos%\%%a.sql %mysql_db% %%a )
)

echo Extracting Routine [ %mysql_db% ]
%mysql_pos%\mysqldump.exe %mysql_server% -R –no-create_info –no-data –skip-comments –triggers=FALSE –result-file=%pos%\routines.sql %mysql_db%

echo Compressing [ %data_pos%\%mysql_db% == %pos% ]
%zip_pos%\7z.exe a -mx=9 -ms=on -r %data_pos%\%mysql_db%\%dbdump% %pos%

echo Removing database directory [ %pos% ]
rd /S /Q %pos%

echo Removing Tables References [ %mysql_db%_tables.txt ]
del %data_pos%\%mysql_db%_tables.txt

echo Done

——code_finish——–

Save script diatas dengan backup_mysql.bat

Cara pakainya bagaimana ?, nih cara pakainya :

tinggal running aja backup_mysql.bat di command prompt

 

dan bagaimana untuk restoring nya :

——code_begin——–

@echo off

echo +—————————————————–+
echo + MySQL Database Restoration +
echo + +
echo + By (c) Man’z, 2011 +
echo +—————————————————–+

if “%1″==”” GOTO EmptyString_DB
if “%2″==”” GOTO EmptyString_Password
if “%3″==”” GOTO EmptyString_Data
if NOT EXIST %3 GOTO No_Files

echo + +
echo + Get the environment variables +
set mysql_pos=”c:\program files\mysql\mysql server 5.1\bin”
set data_pos=%3
set uname=root
set passwd=%2
set db=%1
set cur_pos=%CD%

echo + Processing … please wait +
for /r %cur_pos%\%data_pos% %%I in (*.*) do (
mysql –user=%uname% –password=%passwd% %db% –execute=”\. %%I”
)
echo + Finish restoring all database tables into database +
echo +—————————————————–+
goto Done

:EmptyString_DB
echo + +
echo + Message: Database not specified +
echo + +
goto Usages

:EmptyString_Data
echo + +
echo + Message: Restored data location isn’t specified +
echo + +
goto Usages

:EmptyString_Password
echo + +
echo + Message: No Password database is supplied +
echo + +
goto Usages

:No_Files
echo + +
echo + Message: No such file(s)/directories +
echo + +

:Usages
echo +—————————————————–+
echo + Usage: +
echo + restore_db db_name db_password data_pos +
echo +—————————————————–+

: Done

echo Thank you

——code_finish——–

Save file diatas dengan restore_db.bat

Cara pakai:

server> restore_bat <<databasename>> <<password>> <<nama_file_hasil_extracting>>

Catatan:

untuk restoring harus dijalankan langsung pada server yang sudah jalan mysqlnya ..

Salam,
😎
Man’z

Advertisements

Read Full Post »