Advertisement
teknoraver

php backup

Apr 9th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2. if(!isset($_SERVER['HTTPS'])) {
  3.     header('HTTP/1.1 301 Moved Permanently');
  4.     header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
  5.     exit();
  6. }
  7.  
  8. $mimes = [
  9.     'tar'       => 'application/x-tar',
  10.     'tar.gz'    => 'application/x-gzip',
  11.     'tar.bz2'   => 'application/x-bzip2',
  12.     'tar.xz'    => 'application/x-xz',
  13.     'zip'       => 'application/zip'
  14. ];
  15.  
  16. $cmds = [
  17.     'tar'       => 'tar c .',
  18.     'tar.gz'    => 'tar c . |gzip -9',
  19.     'tar.bz2'   => 'tar c . |bzip2 -9',
  20.     'tar.xz'    => 'tar c . |xz -9',
  21.     'zip'       => 'zip -qr9 - .'
  22. ];
  23.  
  24. if(isset($_REQUEST['fmt'])) {
  25.     $fmt = $_REQUEST['fmt'];
  26.     if(!isset($mimes[$fmt]) || !isset($cmds[$fmt]))
  27.         exit("Invalid format $fmt");
  28.  
  29.     $bkdate = date('Ymd_Hi');
  30.     header("Content-Type: {$mimes[$fmt]}");
  31.     header("Content-Disposition: attachment; filename=\"backup-{$_SERVER['HTTP_HOST']}-$bkdate.$fmt\"");
  32.  
  33.     set_time_limit(3600);
  34.     passthru($cmds[$fmt]);
  35.     exit();
  36. }
  37.  
  38. ?>
  39. <html>
  40.     <head>
  41.         <title>Tar backup</title>
  42.     </head>
  43.     <body>
  44.         <form action="<?php echo $_SERVER['REQUEST_URI']?>" method="POST">
  45.             Archive format:
  46.             <select name="fmt">
  47.                 <?php
  48.                 foreach($mimes as $type => $mime)
  49.                     echo "<option>$type</option>";
  50.                 ?>
  51.  
  52.             </select>
  53.             <br>
  54.             <input type="submit">
  55.         <form>
  56.     </body>
  57. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement