<?php require_once './_common.php'; $tables = []; $exclude = [ 'g5_autosave' 'g5_visit', 'g5_visit_sum' ]; $result = sql_query("SHOW TABLES"); for ($i = 0; $row = sql_fetch_array($result); $i++) { $tables[] = array_pop($row); } $userInfo = posix_getpwuid(fileowner(__FILE__)); $owner = $userInfo['name']; $dir = '/home/dbbackup/'.$owner.'/'.date('Ymdhi'); mkdir($dir, 0755, true); // 7일 초과 경과된 data 삭제 $basedir = dirname($dir); $basetime = time() - 86400 * 7; foreach (scandir($basedir) as $val) { if (in_array($val, array('.', '..'))) continue; $d = $basedir.'/'.$val; if (filemtime($d) < $basetime) { if (is_dir($d)) { foreach (scandir($d) as $v) { if (in_array($v, array('.', '..'))) continue; if (is_file($d.'/'.$v)) @unlink($d.'/'.$v); } rmdir($d); } else { @unlink($d); } } } // DB 백업 $host = G5_MYSQL_HOST; $user = G5_MYSQL_USER; $pass = G5_MYSQL_PASSWORD; $database = G5_MYSQL_DB; foreach ($tables as $table) { if (in_array($table, $exclude)) continue; $file = $dir.'/'.$table.'.sql'; exec("mysqldump --user={$user} --password={$pass} --host={$host} {$database} {$table} --result-file={$file} 2>&1", $output); }