Advertisement
accluster3

Untitled

May 4th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <?php
  2. // 9. satırdaki linki kendinize göre değiştirebilirsiniz. ben yandex disk te "dbBackups" diye bi klasör açtım siz başka bir klasörde tutacaksanız yada anadizinde tutacaksanız değiştirin
  3. //10. satırda kullanıcı adı ve şifrenizi değiştirin
  4. function backup($host = "localhost",$user = "root",$pass = "defaultPassword" ,$dbName)
  5. {
  6.     //backup path
  7.     //$path = '/var/sqlBackups/';
  8.     $path = "backups/";
  9.     //webdav credentials
  10.     $webdavUrl = "https://webdav.yandex.com.tr/dbBackups/";
  11.     $webdavCredentials = array(
  12.             'username',
  13.             'passs'
  14.     );
  15.     // comm
  16.     $link = mysql_connect($host,$user,$pass) or die("could not connect");
  17.     mysql_select_db($dbName,$link);
  18.    
  19.    
  20.    
  21.         $tables = array();
  22.         $result = mysql_query('SHOW TABLES');
  23.         while($row = mysql_fetch_row($result))
  24.         {
  25.             $tables[] = $row[0];
  26.         }
  27.    
  28.    
  29.     $return = "";
  30.     //generate output
  31.     foreach($tables as $table)
  32.     {
  33.         $result = mysql_query('SELECT * FROM '.$table);
  34.         $numFields = mysql_num_fields($result);
  35.        
  36.         $return .= 'DROP TABLE '.$table.';';
  37.         $query = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
  38.         $return.= "\n\n".$query[1].";\n\n";
  39.        
  40.         for ($i = 0; $i < $numFields; $i++)
  41.         {
  42.             while($row = mysql_fetch_row($result))
  43.             {
  44.                 $return.= 'INSERT INTO '.$table.' VALUES(';
  45.                 for($j=0; $j<$numFields; $j++)
  46.                 {
  47.                     $row[$j] = addslashes($row[$j]);
  48.                     $row[$j] = str_replace("\n","\\n",$row[$j]);
  49.                     if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  50.                     if ($j<($numFields-1)) { $return.= ','; }
  51.                 }
  52.                 $return.= ");\n";
  53.             }
  54.         }
  55.         $return.="\n\n\n";
  56.     }
  57.    
  58.     //save to file
  59.     $saltFileName = 'sqlBackup-'.$dbName.'-'.date('D-m-y').'-'.time().'-.sql';
  60.     $fileName = $path . $saltFileName;
  61.    
  62.     $fileHandler = fopen($fileName, 'w+');
  63.     fwrite($fileHandler,$return);
  64.     fclose($fileHandler);
  65.    
  66.    
  67.     $fileHandler = fopen($fileName, 'r');
  68.  
  69.     // webdav thing
  70.     $fileSize = filesize($fileName);
  71.     $ch = curl_init($webdavUrl . $saltFileName);
  72.     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  73.     curl_setopt($ch, CURLOPT_USERPWD, implode(':', $webdavCredentials));
  74.     curl_setopt($ch, CURLOPT_PUT, true);
  75.     curl_setopt($ch, CURLOPT_INFILE, $fileHandler);
  76.     curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
  77.     curl_exec($ch);
  78.  
  79.     fclose($fileHandler);
  80.    
  81. }
  82. backup('localhost','root','kargagak','landing');
  83.  
  84.  
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement