. */ /* version 2021/04/01 */ /* * ***************************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' => false, '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 ($a['datetime'] == $b['datetime']) { return 0; } return $a['datetime'] > $b['datetime'] ? -1 : 1; } public static function ls($_type = 'backup') { $cmd = repo_ssh::makeSshCommand('ls -l --time-style="+%s" ' . config::byKey('ssh::backup::folder')); $result = explode("\n", com_shell::execute($cmd)); $return = array(); foreach ($result as $line) { $token = mb_split("\s+", $line); if (count($token) < 7) { continue; } $file_info = array(); $file_info['filename'] = $token[6]; $file_info['datetime'] = $token[5]; $return[] = $file_info; } usort($return, 'repo_ssh::sortByDatetime'); return $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() { echo "Suppression des anciens backups plus vieux que " . config::byKey('ssh::keepDays') ." jours...\n"; $timelimit = time() - config::byKey('ssh::keepDays') * 86400; foreach (self::ls() as $file) { if($file['filename'] == '..' || $file['filename'] == '.'){ continue; } if ($timelimit > $file['datetime']) { echo " Suppression de " . $file['filename'] . "\n"; $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() 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::makeScpCommand( config::byKey('ssh::backup::folder') . "/" . $_backup, $_backup, 'get'); com_shell::execute($cmd); com_shell::execute(system::getCmdSudo() . 'chmod 777 -R ' . $backup_dir . '/*'); } public static function downloadCore($_path) { } public static function versionCore() { } /* * *********************Methode d'instance************************* */ /* * **********************Getteur Setteur*************************** */ }