. */ /* * ***************************Includes********************************* */ require_once __DIR__ . '/../../core/php/core.inc.php'; class repo_ssh { /* * *************************Attributs****************************** */ public static $_name = 'Ssh'; public static $_scope = array( 'plugin' => false, 'backup' => true, 'hasConfiguration' => true, 'core' => true, 'hasRetentionDay' => true, 'test' => true ); /* * ***********************Méthodes statiques*************************** */ public static function getConfigurationOption(){ return array( 'parameters_for_add' => array( 'path' => array( 'name' => __('Chemin',__FILE__), 'type' => 'input', ), ), 'configuration' => array( 'backup::ip' => array( 'name' => __('[Backup] IP',__FILE__), 'type' => 'input', ), 'backup::username' => array( 'name' => __('[Backup] Utilisateur',__FILE__), 'type' => 'input', ), 'backup::folder' => array( 'name' => __('[Backup] Chemin',__FILE__), 'type' => 'input', ), ), ); } public static function checkUpdate(&$_update) { } public static function deleteObjet($_update) { } public static function downloadObject($_update) { } public static function objectInfo($_update) { return array( 'doc' => '', 'changelog' => '', ); } public static function makeScpCommand($_src, $_dst, $_action = '', $_type = 'backup') { if ($_action == 'put'){ $_dst = config::byKey('ssh::' . $_type . '::username') . '@' . config::byKey('ssh::' . $_type . '::ip') . ':' . $_dst; } elseif ($_action == 'get') { $_src = config::byKey('ssh::' . $_type . '::username') . '@' . config::byKey('ssh::' . $_type . '::ip') . ':' . $_src; } return system::getCmdSudo() . "scp $_src $_dst"; } public static function makeSshCommand($_cmd, $_type = 'backup') { return system::getCmdSudo() . 'ssh ' . config::byKey('ssh::' . $_type . '::username') . "@" . config::byKey('ssh::' . $_type . "::ip") . " $_cmd"; } public static function sortByDatetime($a, $b) { if (strtotime($a['datetime']) == strtotime($b['datetime'])) { return 0; } return (strtotime($a['datetime']) < strtotime($b['datetime'])) ? -1 : 1; } public static function ls($_dir = '', $_type = 'backup') { $cmd = repo_ssh::makeSshCommand('ls -l ' . config::byKey('ssh::backup::folder'), $_type); $result = explode("\n", com_shell::execute($cmd)); $return = array(); for ($i = 0; $i < count($result); $i++) { $line = array(); foreach (mb_split("\s+", $result[$i]) as $value) { if (trim($value) == '') { continue; } $line[] = $value; } $file_info = array(); $file_info['filename'] = $line[8]; $file_info['size'] = $line[4]; $file_info['datetime'] = date('Y-m-d H:i:s', strtotime($line[5] . ' ' . $line[6] . ' ' . $line[7] )); $return[] = $file_info; } usort($return, 'repo_ssh::sortByDatetime'); return array_reverse($return); } public static function test() { $cmd = repo_ssh::makeSshCommand('ls ' . config::byKey('ssh::backup::folder'), 'backup'); try { $result = explode("\n", com_shell::execute($cmd)); return True; } catch (Exception $e) { throw new Exception($e->getMessage()); } } public static function cleanBackupFolder() { $timelimit = strtotime('-' . config::byKey('ssh::keepDays') . ' days'); foreach (self::ls(config::byKey('ssh::backup::folder')) as $file) { if($file['filename'] == '..' || $file['filename'] == '.'){ continue; } if ($timelimit > strtotime($file['datetime'])) { echo 'Delete backup too old : '.json_encode($file); $cmd = self::makeSshCommand('rm ' . config::byKey('ssh::backup::folder') . '/' . $file['filename']); com_shell::execute($cmd); } } } public static function backup_send($_path) { $pathinfo = pathinfo($_path); $cmd = 'cd ' . $pathinfo['dirname'] . ';'; $cmd .= self::makeScpCommand($pathinfo['basename'], config::byKey('ssh::backup::folder') . "/" . $pathinfo['basename'], 'put'); com_shell::execute($cmd); self::cleanBackupFolder(); } public static function backup_list() { $return = array(); foreach (self::ls(config::byKey('ssh::backup::folder')) as $file) { if (strpos($file['filename'],'.tar.gz') !== false) { $return[] = $file['filename']; } } return $return; } public static function backup_restore($_backup) { $backup_dir = calculPath(config::byKey('backup::path')); $cmd = 'cd ' . $backup_dir . ';'; $cmd .= self::makeSshCommand('cd ' . config::byKey('ssh::backup::folder') . ';get ' . $_backup); com_shell::execute($cmd); com_shell::execute(system::getCmdSudo() . 'chmod 777 -R ' . $backup_dir . '/*'); jeedom::restore('backup/' . $_backup, true); } public static function downloadCore($_path) { } public static function versionCore() { } /* * *********************Methode d'instance************************* */ /* * **********************Getteur Setteur*************************** */ }