Guest User

Untitled

a guest
Nov 4th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. #!/usr/bin/php -q
  2. <?php
  3.  
  4. class Database extends PDO
  5. {
  6. public function __construct(array $params)
  7. {
  8. if(!extension_loaded('PDO'))
  9. {
  10. throw new LogicException();
  11. }
  12.  
  13. self::connect($params);
  14. }
  15.  
  16. private function connect(array $params)
  17. {
  18. $user = null;
  19. $password = null;
  20. $dns = array();
  21.  
  22. if( strpos(':', $params['host']) !== false)
  23. {
  24. $host = explode(':', $params['host'], 2);
  25. $params['host'] = $host[0];
  26. $params['port'] = $host[1];
  27. }
  28.  
  29. if(isset($params['host']))
  30. $dns[] = 'host=' . $params['host'];
  31.  
  32. if(isset($params['port']))
  33. $dns[] = 'port=' . $params['port'];
  34.  
  35. if(isset($params['database']))
  36. $dns[] = 'dbname=' . $params['database'];
  37.  
  38. if(isset($params['user']))
  39. $user = $params['user'];
  40.  
  41. if(isset($params['password']))
  42. $password = $params['password'];
  43.  
  44. try
  45. {
  46. parent::__construct('mysql:' . implode(';', $dns), $user, $password);
  47. }
  48. catch(PDOException $error)
  49. {
  50. throw new Exception('Can\'t connect to MySQL database named: ' . $params['database'] . '! :-(');
  51. }
  52. }
  53.  
  54. }
  55.  
  56. class Devices
  57. {
  58. private $debug = false;
  59. protected $db;
  60. private $config_panel = array(
  61. 'host' => 'localhost',
  62. 'user' => 'panel',
  63. 'password' => 'p4$$w0rd',
  64. 'database' => 'panel'
  65. );
  66. private $device_types = array(1 => 'Mikrotik', 2 => 'Ubiquiti');
  67.  
  68. public function __construct()
  69. {
  70. $this->db = new Database($this->config_panel);
  71. }
  72.  
  73. public function getDevices()
  74. {
  75. $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";
  76. return $this->db->query($query)->fetchAll();
  77. }
  78.  
  79. }
  80.  
  81. class Commander
  82. {
  83. private $devices;
  84. private $Mikrotik;
  85. private $Ubnt;
  86. private $backup_location = '/backup/devices';
  87. private $date;
  88.  
  89. public function __construct()
  90. {
  91. $this->date = date("d.m.Y");
  92. $this->devices = new Devices;
  93. $this->Mikrotik = new Mikrotik;
  94. $this->Ubnt = new UBNT;
  95.  
  96. foreach($this->devices->getDevices() as $dev)
  97. {
  98. if(!file_exists($this->backup_location.'/'.$dev['device_ip']))
  99. {
  100. mkdir($this->backup_location.'/'.$dev['device_ip']);
  101. }
  102. if($dev['device_type'] == 1)
  103. {
  104. $this->Mikrotik->prepare($dev['device_ip'], $dev['device_user'], $dev['device_password'], $this->backup_location.'/'.$dev['device_ip']);
  105.  
  106. }
  107. elseif($dev['device_type'] == 2)
  108. {
  109. $this->Ubnt->prepare($dev['device_ip'], $dev['device_user'], $dev['device_password'], $this->backup_location.'/'.$dev['device_ip']);
  110. }
  111. }
  112. 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);
  113. }
  114.  
  115. private function write_file($file_name, $file_content)
  116. {
  117. $fp = fopen($file_name, "w");
  118. if (!fwrite($fp, $file_content))
  119. return false;
  120.  
  121. fclose($fp);
  122. return true;
  123. }
  124. }
  125.  
  126. class Mikrotik
  127. {
  128.  
  129. private $methods = array(
  130. 'kex' => 'diffie-hellman-group1-sha1',
  131. 'hostkey' => 'ssh-dss',
  132. 'client_to_server' => array(
  133. 'crypt' => '3des-cbc',
  134. 'mac' => 'hmac-md5',
  135. 'comp' => 'none'),
  136. 'server_to_client' => array(
  137. 'crypt' => '3des-cbc',
  138. 'mac' => 'hmac-md5',
  139. 'comp' => 'none')
  140. );
  141.  
  142. public function prepare($ip, $user, $password, $location)
  143. {
  144. echo "Wykonuje backup urzadzenia: {$ip} \n";
  145. if (($connection = ssh2_connect($ip, 65512, $this->methods)))
  146. {
  147. if(ssh2_auth_password($connection, $user, $password))
  148. {
  149. echo "Polaczono do {$ip} \n";
  150. if($conn = ssh2_exec($connection, "export file={$ip}; /system backup save name={$ip}"))
  151. {
  152. stream_set_blocking($conn, true);
  153. echo stream_get_contents($conn);
  154. if(!$this->ssh2_sftp_recv($connection, "{$ip}.rsc", $location."/{$ip}.rsc"))
  155. echo "Nie mozna pobrac pliku {$ip}.rsc\n";
  156. if(!$this->ssh2_sftp_recv($connection, "{$ip}.backup", $location."/{$ip}.backup"))
  157. echo "Nie mozna pobrac pliku {$ip}.backup\n";
  158. }
  159. }
  160. }
  161. }
  162.  
  163. private function ssh2_sftp_recv($connection, $remote_file, $local_file)
  164. {
  165. if(!$sftp = ssh2_sftp($connection))
  166. return false;
  167.  
  168. if (!$remoteStream = @fopen("ssh2.sftp://$sftp/$remote_file", 'r'))
  169. return false;
  170.  
  171. if(file_exists($local_file))
  172. unlink($local_file);
  173.  
  174. if (!$localStream = @fopen($local_file, 'w'))
  175. return false;
  176.  
  177. $read = 0;
  178. $fileSize = filesize("ssh2.sftp://$sftp/$remote_file");
  179. while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) {
  180. $read += strlen($buffer);
  181. if (fwrite($localStream, $buffer) === FALSE)
  182. return false;
  183.  
  184. }
  185. ssh2_sftp_unlink($sftp, '/'.$remote_file);
  186. return true;
  187. }
  188.  
  189. }
  190.  
  191. class UBNT
  192. {
  193. public function prepare($ip, $user, $password, $location)
  194. {
  195. echo "Wykonuje backup urzadzenia: {$ip} \n";
  196. if($connection = ssh2_connect($ip, 65512))
  197. {
  198. if(ssh2_auth_password($connection, $user, $password))
  199. {
  200. echo "Polaczono do {$ip} \n";
  201. ssh2_scp_recv($connection, "/tmp/system.cfg", $location."/system.cfg");
  202. ssh2_scp_recv($connection, "/tmp/running.cfg", $location."/running.cfg");
  203. }
  204. }
  205. }
  206. }
  207.  
  208. final class Backup extends Commander{
  209. public function __construct()
  210. {
  211. parent::__construct();
  212. }
  213. }
  214.  
  215. $app = new Backup;
  216.  
  217. ?>
Add Comment
Please, Sign In to add comment