diabliyo

rsync_backup.php

Sep 12th, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. Name    BackupSystem with Rsync over Linux
  5. Version 1.0
  6. Autor   Angel Cantu Jauregui
  7.  
  8. This software use rsync to make backups for multipaths, use it for mantain safety your information :D
  9.  
  10. :: How to mount CIFS Shared on FSTAB ?
  11. //IP_SERVER/SHARED_DIR    /mnt/yourpath        cifs    username=USERNAME,password=KEY,vers=1.0   1 1
  12. */
  13.  
  14. $secfileonbackup= 'backup.txt'; # este archivo debe existir en la carpet destino de respaldos
  15. $tmpfile= "/tmp/rsynctmp.out"; # temporal para resguardo previo
  16. $dstlog=""; # log destino
  17. $files= array(
  18.     "documentos"=>array(
  19.         "src"=>"/home/angel/Documentos/", # imagen en el path real de tu equipo
  20.         "dst"=>"/mnt/net/" # nombre de la carpeta de respaldos
  21.         ),
  22.     "imagenes"=>array(
  23.         "src"=>"/home/angel/Imagánes/", # imagen en el path real de tu equipo
  24.         "dst"=>"/mnt/net/" # nombre de la carpeta de respaldos
  25.         )
  26.     );
  27.  
  28. echo "#########################\n##\tBackups System\t#\n#########################\n";
  29. $msg="## Backup date ". date( "d/m/Y, g:i\t", time() ). "\n";
  30. system( "touch ". $tmpfile ); # creamos archivo
  31.  
  32. foreach( $files as $key=>$val )
  33.     {
  34.     echo "\nBackup directory: ". $val["src"]. "\t";
  35.    
  36.     if( !file_exists($val["dst"].$secfileonbackup) ) # verificando archivo de seguridad
  37.         {
  38.         echo "[Error - The security file does not exists";
  39.         $msg .= "\nDirectory: ". $val["src"]. " [Error - No security file]";
  40.         }
  41.     else if( !file_exists($val["src"]) )
  42.         {
  43.         echo "[Error - Source directory does not exists]";
  44.         $msg .= "\nDirectory ". $val["src"]. " [Error - not exists]";
  45.         }
  46.     else # respaldando
  47.         {
  48.         # si no existe directorio especial de guardado DENTRO del compartido, lo creamos
  49.         if( !file_exists($val["src"].$key) )    mkdir($val["src"].$key);
  50.  
  51.         $dstlog .= ((!$dstlog && $val["dst"]) ? $val["dst"]:""); # si no tiene, guardamos una UNICA vez
  52.         $msg .= "\nDirectory: ". $val["src"]. " [Done]";
  53.         $cmd= "/usr/bin/rsync -rav ". $val["src"]. " ". $val["dst"].$key. " >> ". $tmpfile;
  54.         system($cmd);
  55.         echo "[OK]";
  56.         unset($cmd);
  57.         }
  58.     }
  59.  
  60. # escribimos log en el compartido
  61. echo "\n\nWriting result\t";
  62. if( !$dstlog )
  63.     echo "[Error]";
  64. else
  65.     {
  66.     $msg .= "\n\n---- Log rsync\n\n". file_get_content($tmpfile); # colocamos la salida RSYNC
  67.     $fp= fopen($dstlog.$secfileonbackup, "w"); # crea nuevo stream
  68.     fwrite($fp, $msg, strlen($msg)); # escribimos buffer
  69.     fclose($fp); # cerramos stream
  70.     echo "[Done]";
  71.     }
  72.  
  73. unlink($tmpfile);
  74. unset($msg, $files, $secfileonbackup);
  75. echo "\n\n";
  76. exit;
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment