function handleHostActions($args) { // If we arent given any more parameters if (!isset($args[2])) { // Just output some helpful information and exit echo "Error: Action missing.\n\n"; $this->printHostHelpText($args); return; } // Handle actions switch ($args[2]) { // Handle add case 'add': // Check for parameters first if (!isset($args[3]) || !isset($args[4])) { throw new InputException("Error: The Hostname and Description of the Host to add are required.\n\n Example:\n\n\txbm " . $args[1] . ' add "db01.mydomain.com" "Production DB #1"'); } $hostname = $args[3]; $hostDesc = $args[4]; $hostGetter = new hostGetter(); $hostGetter->setLogStream($this->log); // Get the new Volume $host = $hostGetter->getNew($hostname, $hostDesc); echo "Action: New host created with hostname/description: " . $hostname . " -- " . $hostDesc . "\n\n"; break; // Handle list // Handle list case 'list': $hostGetter = new hostGetter(); $hostGetter->setLogStream($this->log); $hosts = $hostGetter->getAll(); echo "-- Listing all Hosts --\n\n"; foreach ($hosts as $host) { $hostInfo = $host->getInfo(); echo "Hostname: " . $hostInfo['hostname'] . " Description: " . $hostInfo['description'] . "\n"; echo "Active: " . $hostInfo['active'] . " Staging_path: " . $hostInfo['staging_path'] . " SSH Port: " . $hostInfo['ssh_port'] . "\n\n"; } if (sizeOf($hosts) == 0) { echo "\tNo hosts configured.\n\n"; } echo "\n"; break; // Handle edit // Handle edit case 'edit': if (!isset($args[3]) || !isset($args[4]) || !isset($args[5])) { $errMsg = "Error: Hostname of the host to edit must be given along with parameter and value.\n\n"; $errMsg .= " Parameters:\n\n"; $errMsg .= "\thostname - The hostname of the Host - May only be edited if no Scheduled Backups are configured for the host.\n"; $errMsg .= "\tdescription - The description of the Host - Can be edited at any time.\n"; $errMsg .= "\tstaging_path - The temporary dir to use on the host for staging incremental backups - Can be edited at any time.\n"; $errMsg .= "\tssh_port - The SSH port number to use to connect to the remote host - Can be edited at any time.\n"; $errMsg .= "\tactive - Whether or the Host is active Y or N - Can be edited at any time.\n\n"; $errMsg .= " Example:\n\n\txbm " . $args[1] . ' edit db01.mydomain.com staging_path /storage1/backuptmp'; throw new InputException($errMsg); } $hostname = $args[3]; $hostParam = $args[4]; $hostValue = $args[5]; $hostGetter = new hostGetter(); $hostGetter->setLogStream($this->log); if (!($host = $hostGetter->getByName($hostname))) { throw new ProcessingException("Error: No Host exists with name: " . $hostname); } $host->setParam($hostParam, $hostValue); echo "Action: Host with hostname: " . $hostname . " parameter '" . $hostParam . "' set to: " . $hostValue . "\n\n"; break; // Handle delete // Handle delete case 'delete': if (!isset($args[3])) { throw new InputException("Error: Hostname of Host to delete must be given."); } $hostname = $args[3]; $hostGetter = new hostGetter(); $hostGetter->setLogStream($this->log); if (!($host = $hostGetter->getByName($hostname))) { throw new ProcessingException("Error: No Host exists with hostname: " . $hostname); } $host->delete(); echo "Action: Host with hostname: " . $hostname . " deleted.\n\n"; break; // Handle unknown action // Handle unknown action default: echo "Error: Unrecognized action for " . $args[1] . " context: " . $args[2] . "\n\n"; $this->printHostHelpText($args); break; } return; }
function getNew($hostname, $name, $strategyCode, $cronExpression, $volumeName, $datadir, $mysqlUser, $mysqlPass) { $datadir = rtrim($datadir, '/'); // Validate inputs host::validateHostname($hostname); scheduledBackup::validateName($name); backupStrategy::validateStrategyCode($strategyCode); scheduledBackup::validateCronExpression($cronExpression); backupVolume::validateName($volumeName); scheduledBackup::validateDatadirPath($datadir); scheduledBackup::validateMysqlUser($mysqlUser); scheduledBackup::validateMysqlPass($mysqlPass); // Lookup host by name $hostGetter = new hostGetter(); $hostGetter->setLogStream($this->log); if (!($host = $hostGetter->getByName($hostname))) { throw new ProcessingException("Error: No Host could be found with a hostname matching: {$hostname}"); } // Lookup volume by name $volumeGetter = new volumeGetter(); $volumeGetter->setLogStream($this->log); if (!($volume = $volumeGetter->getByName($volumeName))) { throw new ProcessingException("Error: No Volume could be found with a name matching: {$volumeName}"); } // Lookup backup strategy by code $strategyGetter = new backupStrategyGetter(); $strategyGetter->setLogStream($this->log); if (!($strategy = $strategyGetter->getByCode($strategyCode))) { throw new ProcessingException("Error: No Backup Strategy could be found matching the code: {$strategyCode}"); } // Check for existing scheduled backups with the same name for this host if ($existing = $this->getByHostnameAndName($hostname, $name)) { throw new ProcessingException("Error: A Scheduled Backup already exists for host {$hostname} with a name matching: {$name}"); } // INSERT the row $conn = dbConnection::getInstance($this->log); // Create a new scheduledBackup entry // For now we just always create with "xtrabackup" binary used (mysql_type_id = 1).. // Maybe this can change later.. $sql = "INSERT INTO scheduled_backups \n\t\t\t\t\t( name, cron_expression, datadir_path, mysql_user, mysql_password, \n\t\t\t\t\t host_id, backup_volume_id, mysql_type_id, backup_strategy_id \n\t\t\t\t\t) VALUES ( \n\t\t\t\t\t\t'" . $conn->real_escape_string($name) . "',\n\t\t\t\t\t\t'" . $conn->real_escape_string($cronExpression) . "',\n\t\t\t\t\t\t'" . $conn->real_escape_string($datadir) . "',\n\t\t\t\t\t\t'" . $conn->real_escape_string($mysqlUser) . "',\n\t\t\t\t\t\t'" . $conn->real_escape_string($mysqlPass) . "',\n\t\t\t\t\t\t" . $host->id . ",\n\t\t\t\t\t\t" . $volume->id . ",\n\t\t\t\t\t\t1,\n\t\t\t\t\t\t" . $strategy->id . "\n\t\t\t\t\t)"; if (!$conn->query($sql)) { throw new DBException('scheduledBackupGetter->getNew: ' . "Error: Query: {$sql} \nFailed with MySQL Error: {$conn->error}"); } // Init the object $scheduledBackup = new scheduledBackup($conn->insert_id); $scheduledBackup->setLogStream($this->log); // Init the default scheduledBackup parameters for the strategy $sql = "INSERT INTO scheduled_backup_params (scheduled_backup_id, backup_strategy_param_id, param_value) \n\t\t\t\t\t\tSELECT " . $scheduledBackup->id . ", backup_strategy_param_id, default_value \n\t\t\t\t\t\tFROM backup_strategy_params\n\t\t\t\t\t\tWHERE backup_strategy_id=" . $strategy->id; if (!$conn->query($sql)) { throw new DBException('scheduledBackupGetter->getNew: ' . "Error: Query: {$sql} \nFailed with MySQL Error: {$conn->error}"); } return $scheduledBackup; }