Created
June 8, 2012 09:21
generate mysqldump scripts for backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo 'Mysql database scan'."\n"; | |
include_once(dirname(__FILE__)."/conf.php"); | |
// init db connection | |
echo 'init db connection'."\n"; | |
$dbconn = mysql_pconnect($server['host'], $server['username'], $server['password']); | |
// get all database names from server | |
echo 'get all database names from server'."\n"; | |
$arr_database_list = get_database_list($dbconn); | |
$arr_database_list = array_diff($arr_database_list,$static_skip_database); | |
// get all database information from db list | |
echo 'get all database information from db list'."\n"; | |
$arr_all_tables_list = get_all_tables_from_server($dbconn,$arr_database_list); | |
//init export folder | |
echo "init export folder\n"; | |
$str_remove_folder_script = remove_folders($export_path_prefix); | |
$str_init_folder_script = init_folders($export_path_prefix); | |
// generate export scripts | |
echo "generate export scripts\n"; | |
$arr_dump_script = array(); | |
foreach($arr_all_tables_list as $db_name => $tables){ | |
foreach($tables as $table_name){ | |
$str_echo_script = "echo '$db_name.$table_name'\n"; | |
$str_dump_script = $mysqldump_prefix.$db_name.' '.$table_name.' > '.$export_path_prefix.$db_name.'.'.$table_name.".sql\n"; | |
$str_import_script = 'echo "'.$mysql_input_prefix.' '.$db_name.' < ./'.$db_name.'.'.$table_name.'.sql" >> '.$script_import_path; | |
$arr_dump_script[] = $str_echo_script.$str_dump_script.$str_import_script; | |
} | |
} | |
$str_dump_scripts = implode("\n",$arr_dump_script); | |
// go to the dump dir and tar it | |
$str_script_change_dir = 'cd '.$export_path_prefix.'../'; | |
$str_script_tar_files = 'tar -czvf /tmp/mysql_backup_'.$date_today.'.tar.gz ./'.$date_today; | |
$str_scripts_to_file = $script_prefix."\n".$str_remove_folder_script."\n".$str_init_folder_script."\n".$str_dump_scripts."\n".$str_script_change_dir."\n".$str_script_tar_files; | |
//print scripts to file | |
echo "print scripts to file : $script_output_path\n"; | |
file_put_contents($script_output_path,$str_scripts_to_file); | |
function get_database_list($dbconn){ | |
$arr_database_list = array(); | |
if($dbconn != null){ | |
$sql='show databases;'; | |
$result = mysql_query($sql,$dbconn) or die('MySQL query error when SQL query - '.$sql); | |
while($row = mysql_fetch_array($result)){ | |
$arr_database_list[] = $row['Database']; | |
} | |
} | |
return $arr_database_list; | |
} | |
function get_table_list($dbconn,$db_name){ | |
$arr_table_list = array(); | |
if($dbconn != null && $db_name != null){ | |
$sql = 'show tables from `'.$db_name.'`;'; | |
$result = mysql_query($sql,$dbconn) or die('MySQL query error when SQL query - '.$sql); | |
while($row = mysql_fetch_array($result)){ | |
$arr_table_list[] = $row['Tables_in_'.$db_name]; | |
} | |
} | |
return $arr_table_list; | |
} | |
function get_all_tables_from_server($dbconn,$arr_database){ | |
$arr_table_list = array(); | |
if($dbconn != null && $arr_database != null){ | |
foreach($arr_database as $db_name){ | |
$arr_table_list[$db_name] = get_table_list($dbconn,$db_name); | |
} | |
} | |
return $arr_table_list; | |
} | |
function init_folders($export_path_prefix){ | |
if($export_path_prefix != null){ | |
mkdir($export_path_prefix,0777,true); | |
} | |
// still return the excute script for generate script | |
return 'mkdir -m 777 -p '.$export_path_prefix; | |
} | |
function remove_folders($export_path_prefix){ | |
if($export_path_prefix != null){ | |
rrmdir($export_path_prefix); | |
} | |
// still return the excute script for generate script | |
return 'rm -rf '.$export_path_prefix; | |
} | |
function rrmdir($dir) { | |
foreach(glob($dir . '/*') as $file) { | |
if(is_dir($file)){ | |
rrmdir($file); | |
}else{ | |
unlink($file); | |
} | |
} | |
rmdir($dir); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment