/** * Launcher to request Mediboard * * @param string $rootURL Host to request (ie, https://localhost/mediboard) * @param string $username Username requesting * @param string $password Password of the user * @param string $params Parameters to send (ie, "m=dPpatients&tab=vw_medecins") * @param int $times How many repeats * @param int $delay Delay (in seconds) between repeats * @param string $file Output file * * @return void */ function request($rootURL, $username, $password, $params, $times, $delay, $file) { announce_script("Mediboard request launcher"); if ($times === "") { $times = 1; } if ($delay === "") { $delay = 1; } $times = intval($times); $delay = intval($delay); $login = "******"; $username = "******" . $username; $password = "******" . $password; $url = $rootURL . "/index.php?" . $login . "&" . $username . "&" . $password . "&" . $params; // Make mediboard path $MEDIBOARDPATH = "/var/log/mediboard"; force_dir($MEDIBOARDPATH); $log = $MEDIBOARDPATH . "/jobs.log"; force_file($log); if ($times > 1) { while ($times > 0) { $times--; mediboard_request($url, $log, $file); sleep($delay); } } else { mediboard_request($url, $log, $file); } }
/** * Log a Ping request * * @param string $hostname Host to ping * @param string $fileLog Filename for the output * @param string $dirLog Directory where to create the filelog * * @return void */ function logPing($hostname, $fileLog, $dirLog) { announce_script("Ping logger"); // Make the log line $dt = date('Y-m-d\\TH:i:s'); if ($hostname === "") { $hostname = "localhost"; } $ping = shell_exec("ping " . $hostname . " -c 4 | tr -s '\n' | tail -n 1"); if (check_errs($ping, null, "Failed to ping", "Ping successful!")) { // Log the line if ($dirLog === "") { $dirLog = "/var/log/ping"; } if ($fileLog === "") { $file = $dirLog . "/" . $hostname . ".log"; } else { $file = $dirLog . "/" . $fileLog; } force_dir($dirLog); $fic = fopen($file, "a+"); if (check_errs($fic, false, "Failed to open log file", "Log file opened at " . $file . "!")) { fwrite($fic, $dt . " " . $ping); fclose($fic); } } }
/** * Rotate MySQL Binlogs * * @param string $userAdminDB MySQL username allowed to connect, ie admin * @param string $passAdminDB Password of the MySQL user * @param string $binLogsDir Directory where binlogs are stored, ie /var/log/mysq * @param string $binLogIndexFilename Name of the binlog-index file, ie log-bin.index * @param string $backup Backup directory, ie /mbbackup/binlogs * * @return void */ function rotateBinlogs($userAdminDB, $passAdminDB, $binLogsDir, $binLogIndexFilename, $backup) { $currentDir = dirname(__FILE__); announce_script("Rotate binlogs"); if ($userAdminDB === "") { $userAdminDB = "mbadmin"; } if ($binLogsDir === "") { $binLogsDir = "/var/log/mysql"; } if ($binLogIndexFilename === "") { $binLogIndexFilename = "log-bin.index"; } if ($backup === "") { $backup = "/mbbackup/binlogs"; } // Backup destination dir force_dir($backup); // Flush logs to start a new one if ($passAdminDB === "") { echo shell_exec("mysqladmin -u " . $userAdminDB . " flush-logs"); } else { echo shell_exec("mysqladmin -u " . $userAdminDB . " -p" . $passAdminDB . " flush-logs"); } // Move all logs but latest to backup $binLogFiles = file($binLogsDir . "/" . $binLogIndexFilename); check_errs($binLogFiles, false, "Impossible d'ouvrir le fichier " . $binLogsDir . "/" . $binLogIndexFilename, "Fichier " . $binLogsDir . "/" . $binLogIndexFilename . " ouvert !"); $lastBinLogFile = trim(end($binLogFiles)); $binPrefixPOS = strpos(basename($lastBinLogFile), "."); $binPrefix = substr(basename($lastBinLogFile), 0, $binPrefixPOS + 1); foreach (glob(dirname($lastBinLogFile) . "/" . $binPrefix . "*") as $oneBinLogFile) { if ($oneBinLogFile != $lastBinLogFile) { rename($oneBinLogFile, $backup . "/" . basename($oneBinLogFile)); } } // Move binlog indexes to binlog backup copy($binLogsDir . "/" . $binLogIndexFilename, $backup . "/" . $binLogIndexFilename); // Archive the binlogs exec("tar -cjf " . $backup . "/binlogs_" . date('Y-m-d\\TH:i:s') . ".tar.bz2 -C " . $backup . " " . $binPrefix . "*", $result, $returnVar); // Rotate binlogs foreach (glob($backup . "/" . $binPrefix . "*") as $aBinLogFile) { unlink($aBinLogFile); } // Rotate binlogs and indeces for a week shell_exec("find {$backup} -name \"*bin.*\" -mtime +7 -exec rm -f {} \\;"); shell_exec("find {$backup} -name \"binlog-*.index\" -mtime +7 -exec rm -f {} \\;"); shell_exec("find {$backup} -name \"binlogs_*.tar.bz2\" -mtime +7 -exec rm -f {} \\;"); }
/** * Enable you to create a database backup * * @param string $method Hotcopy or Dump method * @param string $username Access database * @param string $password Authenticate user * @param string $hostname Database server * @param string $port MySQL port * @param string $database Database to backup, ie mediboard * @param string $backupPath Backup path, ie /var/backup * @param string $time Time in days before removal of files * @param string $binary To create mysql binary log index * @param string $loginUsername Username used to send a mail when diskfull is detected * @param string $loginPassword Password for the username used to send a mail when diskfull is detected * * @return void */ function baseBackup($method, $username, $password, $hostname, $port, $database, $backupPath, $time, $binary, $loginUsername, $loginPassword, $lockFilePath = null) { $currentDir = dirname(__FILE__); $event = $currentDir . "/../tmp/monitevent.txt"; announce_script("Database daily backup"); if ($hostname === "") { $hostname = "localhost"; } if ($port === "") { $port = "3306"; } if ($time === "") { $time = "7"; } $binary = false; if ($binary === "y") { $binary = true; } if ($loginUsername === "") { $loginUsername = ""; $loginPassword = ""; } info_script("Backuping " . $database . " database"); // Make complete path // // Make shell path $SHELL_PATH = $currentDir; // Create the lock file if ($lockFilePath) { if (!check_errs(touch($lockFilePath), false, "Unable to create lock file", "Lock file created")) { return; } } // Make backup path force_dir($backupPath); // Make database path $BASE_PATH = $backupPath . "/" . $database . "-db"; force_dir($BASE_PATH); chdir($BASE_PATH); // If no enough free disk space (1.5 * size of database), send mail if provided and quit $mysql_conf = shell_exec("find /etc -name my.cnf 2>/dev/null|head -n 1"); $mysql_conf = trim($mysql_conf); $mysql_data_root = ""; $lines = file($mysql_conf); foreach ($lines as $line) { if (preg_match("/^(datadir)/m", $line)) { $datadirPOS = strpos($line, "="); $mysql_data_root = trim(substr($line, $datadirPOS + 1)); } } $dir = opendir($mysql_data_root); check_errs($dir, false, "Unable to determine MySQL data root", "MySQL data root found!"); closedir($dir); $mysql_data_base = $mysql_data_root . "/" . trim($database); $database_size = 0; $database_files = getFiles($mysql_data_base . "/"); if ($database_files) { foreach ($database_files as $one_database_file) { $database_size += filesize($one_database_file); } } // Expanded size (database + tar) $needed_size = $database_size * 3 / 2; $available_size = disk_free_space($backupPath); if ($available_size < $needed_size) { if ($loginUsername != "") { info_script("Send a mail using " . $loginUsername . " login"); // Name of the instance of mediboard $instance = basename(dirname($currentDir)); file_get_contents("http://localhost/" . $instance . "/?login="******":" . $loginPassword . "&m=system&a=ajax_send_mail_diskfull"); } } check_errs($available_size < $needed_size, 1, "Needed space " . formatSize($needed_size) . " exceeds available space " . formatSize($available_size), "Enough available space!"); // Male MySQL method // // removes previous hotcopy/dump if something went wrong rrmdir($database); $DATETIME = date('Y-m-d\\TH-i-s'); switch ($method) { case "hotcopy": $result = $database . "/"; $mysqlhotcopy = shell_exec("mysqlhotcopy -h " . $hostname . " -P " . $port . " -u " . $username . " -p " . $password . " " . $database . " " . $BASE_PATH); check_errs($mysqlhotcopy, null, "Failed to create MySQL hot copy", "MySQL hot copy done!"); if ($binary) { $link = mysql_connect($hostname . ":" . $port, $username, $password); check_errs($link, false, "Could not connect : " . mysql_error(), "Connected!"); if (!$link) { if ($lockFilePath) { unlink($lockFilePath); } return 0; } $query = "SHOW MASTER STATUS"; $res = mysql_query($query); mysql_close($link); $row = 0; if ($res) { $row = mysql_fetch_object($res); } $a = 0; if ($row) { $file = fopen($backupPath . "/binlog-" . $DATETIME . ".index", "w"); if ($file) { fwrite($file, "File Position\tBinlog_Do_DB\tBinlog_Ignore_DB\n "); fwrite($file, $row->File . "\t" . $row->Position . "\t " . $row->Binlog_Do_DB . "\t" . $row->Binlog_Ignore_DB . "\n"); fclose($file); $a = 1; } } check_errs($a, 0, "Failed to create MySQL Binary log index", "MySQL Binary log index done!"); } break; case "dump": $result = $database . ".sql"; $mysqldump = shell_exec("mysqldump --opt -u " . $username . " -p" . $password . " -h " . $hostname . " -P " . $port . " " . $database); check_errs($mysqldump, null, "Failed to create MySQL dump", "MySQL dump done!"); $file = fopen($result, "w"); $a = 0; if ($file) { fwrite($file, $mysqldump); fclose($file); $a = 1; } check_errs($a, 0, "Failed to save MySQL dump file", "MySQL dump file saved!"); break; default: echo "Choose hotcopy or dump method\n"; if ($lockFilePath) { unlink($lockFilePath); } return 0; } // Rotating files older than n days, all files if 0 if (!$time) { exec("find " . $BASE_PATH . " -name '" . $database . "*.tar.gz'", $find, $return_var); foreach ($find as $one_file) { unlink($one_file); } check_errs($return_var, true, "Failed to rotate files", "Files rotated"); } else { $filter = "-ctime +" . $time; exec("find " . $BASE_PATH . " -name '" . $database . "*.tar.gz' " . $filter, $find, $return_var); foreach ($find as $one_file) { unlink($one_file); } check_errs($return_var, true, "Failed to rotate files", "Files rotated"); } // Compress archive and remove file // // Make the tarball $tarball = $database . "-" . $DATETIME . ".tar.gz"; exec("tar cfz " . $tarball . " " . $result, $tar, $return_var); check_errs($return_var, true, "Failed to create backup tarball", "Tarball packaged!"); // Create a symlink @unlink($database . "-latest.tar.gz"); $res = symlink($tarball, $database . "-latest.tar.gz"); check_errs($res, false, "Failed to create symlink", "Symlink created!"); // Remove temporay files $a = 0; if (is_dir($result)) { if (rrmdir($result)) { $a = 1; } } else { if (unlink($result)) { $a = 1; } } check_errs($a, false, "Failed to clean MySQL files", "MySQL files cleaning done!"); if ($lockFilePath) { unlink($lockFilePath); } // Write into event file if (file_exists($event)) { $fic = fopen($event, "a"); } else { $fic = fopen($event, "w"); } fwrite($fic, "\n#" . date('Y-m-d H:i:s')); fwrite($fic, "\n<strong>" . $database . "</strong> base backup: <strong>" . $method . "</strong> method"); fclose($fic); }