Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.36 KB | None | 0 0
  1. <?php
  2.  
  3. require '/PHPMailer/PHPMailerAutoload.php';
  4.  
  5. ini_set('max_execution_time', 4 * 60);
  6.  
  7. /***SUBSTITUA COM AS INFORMAÇÕES CORRETAS **/
  8.  
  9. $host_mysql       = "localhost";
  10. $database         = 'database';        // NOME DO BANCO DE DADOS PARA FAZER DUMP
  11. $username         = 'user_database';   // NOME DE UM USUÁRIO DO BANCO DE DADOS
  12. $password         = 'senha_database';  // SENHA DO USÚARIO DO BANCO DE DADOS
  13. $source           = "../";             // PASTA DE ORIGEM (NÃO PRECISA ALTERAR SE O BACKUP É DA PASTA RAIZ)
  14. $backup           = "backup.zip";      // NOME DO ARQUIVO DE BACKUP (SE QUISER NÃO PRECISA ALTERAR)
  15. $dump             = "dump.sql";        // NOME DO DUMP DO BANCO DE DADOS (SE QUISER NÃO PRECISA ALTERAR)
  16. $user_email       = "user@gmail.com";  // EMAIL DA SUA CONTA DO GMAIL (USUÁRIO)
  17. $user_senha_email = "senha_email";     // SENHA DA CONTA DE EMAIL
  18.  
  19. $destino1 = "destino1@hotmail.com";    // MUDE PARA O ENDEREÇO DO DESTINATARIO
  20. $destino2 = "destino2@hotmail.com";    // MUDE PARA O ENDEREÇO DO DESTINATARIO
  21.  
  22. /**************************************************************************/
  23.  
  24. function backup($source, $output)
  25. {
  26.     $zip = new ZipArchive();
  27.  
  28.     if ($zip->open($output, ZIPARCHIVE::CREATE) !== TRUE)
  29.     {
  30.         throw new Exception("Nao foi possivel abrir o arquivo zip");
  31.     }
  32.  
  33.     $all = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source));
  34.  
  35.     foreach ($all as $f=>$value)
  36.     {
  37.         if(!strcmp($f, '.')) continue;
  38.         if(!strcmp($f, '..')) continue;
  39.  
  40.         $pos = strrpos($f, '\\'); // Windows
  41.         if($pos)
  42.         {
  43.             $name = substr($f, $pos + 1);
  44.  
  45.             if(!strcmp($name, '.')) continue;
  46.             if(!strcmp($name, '..')) continue;
  47.         }
  48.  
  49.         $pos = strrpos($f, '/'); // Windows
  50.         if($pos)
  51.         {
  52.             $name = substr($f, $pos + 1);
  53.  
  54.             if(!strcmp($name, '.')) continue;
  55.             if(!strcmp($name, '..')) continue;
  56.         }
  57.  
  58.         if(strncmp('../../..\relatorios_por_email\backup_sistema\script-CDE8F967', $f, strlen('../../..\relatorios_por_email\backup_sistema\script-CDE8F967')) == 0) continue;
  59.         if(strncmp('..\..\..\relatorios_por_email\backup_sistema\script-CDE8F967', $f, strlen('..\..\..\relatorios_por_email\backup_sistema\script-CDE8F967')) == 0) continue;
  60.  
  61.  
  62.         $extentions = array('ade', 'adp', 'bat', 'chm', 'cmd', 'com', 'cpl', 'exe', 'hta', 'ins', 'isp', 'jar',
  63.                             'jse', 'lib', 'lnk', 'mde', 'msc', 'msp', 'mst', 'pif', 'scr', 'sct', 'shb', 'sys', 'vb',
  64.                             'vbe', 'vbs', 'vxd', 'wsc', 'wsf', 'wsh', 'zip');
  65.  
  66.         $extention = pathinfo($f, PATHINFO_EXTENSION);
  67.  
  68.         for($i = 0; $i < count($extentions); $i++)
  69.         {
  70.             if(strcmp($extentions[$i], $extention) == 0) break;
  71.         }
  72.  
  73.         if($i != count($extentions)) continue;
  74.  
  75.        
  76.         if(!$zip->addFile(realpath($f), $f))
  77.         {
  78.             throw new Exception("Erro ao adicionar o arquivo: $f");
  79.         }
  80.        
  81.         //echo "$f</BR>";
  82.     }
  83.     $zip->close();
  84. }
  85.  
  86. function dump_database($host_mysql, $database, $username, $password, $file)
  87. {
  88.     try
  89.     {
  90.         include_once(dirname(__FILE__) . '/Ifsnop/Mysqldump/Mysqldump.php');
  91.         $dump = new Ifsnop\Mysqldump\Mysqldump($database, $username, $password);
  92.         $dump->host = $host_mysql;
  93.         $dump->start($file);
  94.  
  95.         $zip = new ZipArchive();
  96.  
  97.         if($zip->open($file.'.zip', ZIPARCHIVE::CREATE) !== TRUE)
  98.         {
  99.             throw new Exception("Nao foi possivel abrir o arquivo zip: $file");
  100.         }
  101.  
  102.         if(!$zip->addFile(realpath($file), $file))
  103.         {
  104.             throw new Exception("Erro ao adicionar o arquivo: $file");
  105.         }
  106.  
  107.         $zip->close();
  108.     }
  109.     catch (\Exception $e)
  110.     {
  111.         throw $e;
  112.     }
  113. }
  114.  
  115. $file = fopen("log.txt", "w");
  116.  
  117. try
  118. {
  119.     echo "Dump do banco de dados iniciado. Aguade ...</br>";
  120.     fwrite($file, "Dump do banco de dados iniciado. Aguade ...\n");
  121.     dump_database($host_mysql, $database, $username, $password, 'temp/'.$dump);
  122.     echo "Dump do banco de dados executado com sucesso</br>";
  123.     fwrite($file, "Dump do banco de dados executado com sucesso\n");
  124.  
  125.     echo "Os arquivos estao sendo zipados. Aguarde ...</br>";
  126.     fwrite($file, "Os arquivos estao sendo zipados. Aguarde ...\n");
  127.     backup($source, 'temp/'.$backup);
  128.     echo "Todos os arquivos foram zipados</br>";
  129.     fwrite($file, "Todos os arquivos foram zipados\n");
  130.  
  131.     $mail = new PHPMailer;
  132.     //$mail->SMTPDebug  = 2;
  133.     $mail->isSMTP();                       // Set mailer to use SMTP
  134.     $mail->Host       = 'smtp.gmail.com';  // Specify main and backup SMTP servers
  135.     $mail->SMTPAuth   = true;              // Enable SMTP authentication
  136.     $mail->Username   = $user_email;       // SMTP username
  137.     $mail->Password   = $user_senha_email; // SMTP password
  138.     $mail->SMTPSecure = 'tls';             // Enable TLS encryption, `ssl` also accepted
  139.     $mail->Port       = 587;               //587;
  140.  
  141.     $mail->From = $user_email;
  142.     $mail->addAddress($destino1);
  143.     $mail->addAddress($destino2);
  144.  
  145.     $mail->addAttachment('temp/'.$dump.'.zip');
  146.     $mail->addAttachment('temp/'.$backup);
  147.  
  148.     $mail->Subject = 'backup';
  149.     $mail->Body    = 'backup';
  150.  
  151.     echo "Tentando enviar o email. Aguarde ...</br>";
  152.     fwrite($file, "Tentando enviar o email. Aguarde ...\n");
  153.  
  154.     if(!$mail->send())
  155.     {
  156.         echo 'O email não pode ser enviado.';
  157.         fwrite($file, "O email não pode ser enviado.");
  158.         echo 'Mailer Error: ' . $mail->ErrorInfo. '</br>';
  159.         fwrite($file, "Mailer Error: " . $mail->ErrorInfo. "\n");
  160.     }
  161.     else
  162.     {
  163.         echo 'O email foi enviado' .'</br>';
  164.         fwrite($file, "O email foi enviado\n");
  165.  
  166.         echo 'Script executado com sucesso </br>';
  167.         fwrite($file, "Script executado com sucesso\n");
  168.     }
  169.  
  170.    
  171.     $files = glob(__DIR__.'/temp/*');
  172.  
  173.    
  174.     foreach($files as $file1)
  175.     {
  176.         if(is_file($file1))  unlink($file1);
  177.     }
  178.  
  179.     fclose($file);
  180. }
  181. catch (\Exception $e)
  182. {
  183.     echo 'Error: ' . $e->getMessage().'</br>';
  184.     fwrite($file, "Error: " . $e->getMessage()."\n");
  185.  
  186.     $files = glob(__DIR__.'/temp/*');
  187.  
  188.    
  189.     foreach($files as $file1)
  190.     {
  191.         if(is_file($file1))  unlink($file1);
  192.     }
  193.    
  194.     fclose($file);
  195. }
  196.  
  197. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement