Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php -q
- <?php
- class Database extends PDO
- {
- public function __construct(array $params)
- {
- if(!extension_loaded('PDO'))
- {
- throw new LogicException();
- }
- self::connect($params);
- }
- private function connect(array $params)
- {
- $user = null;
- $password = null;
- $dns = array();
- if( strpos(':', $params['host']) !== false)
- {
- $host = explode(':', $params['host'], 2);
- $params['host'] = $host[0];
- $params['port'] = $host[1];
- }
- if(isset($params['host']))
- $dns[] = 'host=' . $params['host'];
- if(isset($params['port']))
- $dns[] = 'port=' . $params['port'];
- if(isset($params['database']))
- $dns[] = 'dbname=' . $params['database'];
- if(isset($params['user']))
- $user = $params['user'];
- if(isset($params['password']))
- $password = $params['password'];
- try
- {
- parent::__construct('mysql:' . implode(';', $dns), $user, $password);
- }
- catch(PDOException $error)
- {
- throw new Exception('Can\'t connect to MySQL database named: ' . $params['database'] . '! :-(');
- }
- }
- }
- class Devices
- {
- private $debug = false;
- protected $db;
- private $config_panel = array(
- 'host' => 'localhost',
- 'user' => 'panel',
- 'password' => 'p4$$w0rd',
- 'database' => 'panel'
- );
- private $device_types = array(1 => 'Mikrotik', 2 => 'Ubiquiti');
- public function __construct()
- {
- $this->db = new Database($this->config_panel);
- }
- public function getDevices()
- {
- $query = "SELECT `device_ip`, `device_user`, `device_password`, `device_type` FROM `devices` WHERE `device_user` != '' AND `device_password` != '' AND `online` = 1 AND `device_backup` = 1";
- return $this->db->query($query)->fetchAll();
- }
- }
- class Commander
- {
- private $devices;
- private $Mikrotik;
- private $Ubnt;
- private $backup_location = '/backup/devices';
- private $date;
- public function __construct()
- {
- $this->date = date("d.m.Y");
- $this->devices = new Devices;
- $this->Mikrotik = new Mikrotik;
- $this->Ubnt = new UBNT;
- foreach($this->devices->getDevices() as $dev)
- {
- if(!file_exists($this->backup_location.'/'.$dev['device_ip']))
- {
- mkdir($this->backup_location.'/'.$dev['device_ip']);
- }
- if($dev['device_type'] == 1)
- {
- $this->Mikrotik->prepare($dev['device_ip'], $dev['device_user'], $dev['device_password'], $this->backup_location.'/'.$dev['device_ip']);
- }
- elseif($dev['device_type'] == 2)
- {
- $this->Ubnt->prepare($dev['device_ip'], $dev['device_user'], $dev['device_password'], $this->backup_location.'/'.$dev['device_ip']);
- }
- }
- shell_exec('cd '.$this->backup_location.' && git add -A && git commit -m "Automatyczny Backup z dnia: '.$this->date.' " && git push && git tag -a '.$this->date.' -m "Tag z dnia '.$this->date.' " && git push origin '.$this->date);
- }
- private function write_file($file_name, $file_content)
- {
- $fp = fopen($file_name, "w");
- if (!fwrite($fp, $file_content))
- return false;
- fclose($fp);
- return true;
- }
- }
- class Mikrotik
- {
- private $methods = array(
- 'kex' => 'diffie-hellman-group1-sha1',
- 'hostkey' => 'ssh-dss',
- 'client_to_server' => array(
- 'crypt' => '3des-cbc',
- 'mac' => 'hmac-md5',
- 'comp' => 'none'),
- 'server_to_client' => array(
- 'crypt' => '3des-cbc',
- 'mac' => 'hmac-md5',
- 'comp' => 'none')
- );
- public function prepare($ip, $user, $password, $location)
- {
- echo "Wykonuje backup urzadzenia: {$ip} \n";
- if (($connection = ssh2_connect($ip, 65512, $this->methods)))
- {
- if(ssh2_auth_password($connection, $user, $password))
- {
- echo "Polaczono do {$ip} \n";
- if($conn = ssh2_exec($connection, "export file={$ip}; /system backup save name={$ip}"))
- {
- stream_set_blocking($conn, true);
- echo stream_get_contents($conn);
- if(!$this->ssh2_sftp_recv($connection, "{$ip}.rsc", $location."/{$ip}.rsc"))
- echo "Nie mozna pobrac pliku {$ip}.rsc\n";
- if(!$this->ssh2_sftp_recv($connection, "{$ip}.backup", $location."/{$ip}.backup"))
- echo "Nie mozna pobrac pliku {$ip}.backup\n";
- }
- }
- }
- }
- private function ssh2_sftp_recv($connection, $remote_file, $local_file)
- {
- if(!$sftp = ssh2_sftp($connection))
- return false;
- if (!$remoteStream = @fopen("ssh2.sftp://$sftp/$remote_file", 'r'))
- return false;
- if(file_exists($local_file))
- unlink($local_file);
- if (!$localStream = @fopen($local_file, 'w'))
- return false;
- $read = 0;
- $fileSize = filesize("ssh2.sftp://$sftp/$remote_file");
- while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) {
- $read += strlen($buffer);
- if (fwrite($localStream, $buffer) === FALSE)
- return false;
- }
- ssh2_sftp_unlink($sftp, '/'.$remote_file);
- return true;
- }
- }
- class UBNT
- {
- public function prepare($ip, $user, $password, $location)
- {
- echo "Wykonuje backup urzadzenia: {$ip} \n";
- if($connection = ssh2_connect($ip, 65512))
- {
- if(ssh2_auth_password($connection, $user, $password))
- {
- echo "Polaczono do {$ip} \n";
- ssh2_scp_recv($connection, "/tmp/system.cfg", $location."/system.cfg");
- ssh2_scp_recv($connection, "/tmp/running.cfg", $location."/running.cfg");
- }
- }
- }
- }
- final class Backup extends Commander{
- public function __construct()
- {
- parent::__construct();
- }
- }
- $app = new Backup;
- ?>
Add Comment
Please, Sign In to add comment