PHP - Moved: Php And Mysql Backup/restore
This topic has been moved to Miscellaneous.
http://www.phpfreaks.com/forums/index.php?topic=314405.0 Similar TutorialsThis topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=352495.0 How would I go about using PHP to export all the data in my SQL database to a .sql file? Also how can I then import that .sql file back into the database? Thanks! I looked into this everywhere,but the code does not make sense, is not properly documented and is outdated. Ive been trying to achieve this for the past hour. Anybody know how I can do this? Can we create a backup of database entered data in PHPMYADMIN? As you know if we are using locally any database through php then in case of system crash or the like our all the enterred saved data in the database will gonna be lost i want to know that is it possible to create a back up for our saved data in the database and if is it possible then how can we do it???? Hello all, I need to be able to create file backups on different types of servers running php (windows or linux). I was hoping that there was a good php class out there that would already handle this for me but I have not come across anything as of yet. I can do it perfectly fine in the linux environment but was hoping to find something that would work on a variety of servers running php. I was hoping that it would support php 4 for older sites. If anyone has any good resources for this please let me know. Thanks I am using small script (with crone) to backup database every morning, zip and send me mail (with .sql/zip attached). But backup is not in UTF-8 format, so I get this: "Č" -> "C" "Š" -> "S" etc. I have tried various solution but nothing works. Also tried utf_encode() function, but then I get strange characters. Script looks like this: function backup_tables($host,$user,$pass,$name,$tables = '*',$date) { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //ALL TABLES if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } //GET DATA foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); $return.= 'DROP TABLE IF EXISTS '.$table.';'; $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = str_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; } } $return.="\n\n\n"; } //SAVE $handle = fopen('Backup-'.$date.'.sql','w+'); fwrite($handle,$return); fclose($handle); } Database Collation: utf8_binType: [/size]MyISAM [/size]How to save .sql file in proper UTF-8 Format?[/b][/size][/font] Ok. I managed to do something like this: I used CreateZipFile classs from php classes! Code: [Select] <?php include_once 'CreateZipFile.inc.php'; $createZip = new CreateZipFile; $createZip->zipDirectory('binary', ''); $zipFileName = 'backup.zip'; $handle = fopen($zipFileName, 'wb'); $out = fwrite($handle, $createZip->getZippedFile()); fclose($handle); $createZip->forceDownload($zipFileName); @unlink($zipFileName); ?> but there is still one small issue. The archive doesn't contain any empty folders within the binary folder. I have been searching the web and I can't fond anyone that has working code for backing up database with PHP. Most of them have the notorious depreciated ereg_replace function. Does anyone know of a simple solution? I want it to backup all the tables in the database Hi This is a really basic question but i am trying to understand the process of uploading to amazon S3, I dont want to just download a pre-wriiten script that i dont understand whats happen. I am using the s3.php class require_once('S3.php'); $s3 = new S3('[EDITED]', '[EDITED]'); $ourFileName = "files/test.txt"; $ourFileHandle = fopen($ourFileName, 'r') or die("can't open file"); fclose($ourFileHandle); if ($s3->putObjectFile("files/test.txt", "mybucket", "test/test.txt", S3::ACL_PUBLIC_READ)) { echo "<strong>We successfully uploaded your file.</strong>"; }else{ echo "<strong>Something went wrong while uploading your file... sorry.</strong>"; } Ok so i am just doing this to figure it out, so i create a file and then upload to amazon s3 this works fine, where i am confused is when you backup all files and directories in a site, i dont want all the backup files in the root off my website i want them to automatically upload to amazon. So how does the process work? if i upload a file it works great but i am still upload a file form my server. How does amazon automatically grab files from my server and upload them. Anyone no any basic step by step tutorials i have googled loads but they are all massive pre written classes and stuff which i can get working, but looking for a basic example. Thanks hi, im looking for a way to make a page that will backup my mysql when i click a button and i can save it locally on any machine i use. anybody able to help me out with what to search in google? thanks! hi, I am trying to restore a mysql db from a remote location below is the function it is working fine when i am passing local location, but gives error on passing remote location Code: [Select] function restore($path) { $f = fopen('restore.sql' , 'w+'); if(!$f) { echo "Error While Restoring Database"; return; } $zip = new ZipArchive(); if ($zip->open($path) === TRUE) { #Get the backup content $sql = $zip->getFromName('adobe4u_new-sqldump.sql'); #Close the Zip File $zip->close(); #Prepare the sql file fwrite($f , $sql); fclose($f); #Now restore from the .sql file //$command = "mysql --user=root --password=password --database=adobe4u < restore.sql"; //exec($command); // Name of the file $filename = 'restore.sql'; // Temporary variable, used to store current query $templine = ''; // Read in entire file $lines = file($filename); // Loop through each line foreach ($lines as $line) { // Skip it if it's a comment if (substr($line, 0, 2) == '--' || $line == '') continue; // Add this line to the current segment $templine .= $line; // If it has a semicolon at the end, it's the end of the query if (substr(trim($line), -1, 1) == ';') { // Perform the query mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />'); // Reset temp variable to empty $templine = ''; } } // Restoring END #Delete temporary files without any warning @unlink('restore.sql'); echo '<div class="success">Successfully Updated!</div>'; } else { echo '<div class="error">Some thing went wrong! Updation Failed</div>'; } } Please can anyone help me in it, so That if i pass a remote location of database it will extract it locally Hi all, Not sure if this is a browser feature kind of thing, or if it can be done with a script. I'm looking at my page: www.mysite.com/index.php?start=0 I scroll to the bottom of the page with the vertical scroll bar, and then click the pagation link to start on page 2, record #50: www.mysite.com/index.php?start=50 Of course, the vertical scroll bar jumps back to the it jumps to the top of my page. Is there a way to make it remember that the scroll bar was at the bottom (or in the middle)? Thanks. hi all i am looking for the quickest way to zip all files and folder of a website i am currently using the followning. <?php $date = date("F-j-Y-g-ia"); if(exec("cd /home/isd/public_html/bk/files;tar -cvpzf backup-$date.tar /home/isd/public_html")) { echo "done"; } ?> This works but was just wondering if anyone can give me and insight on a better solution?? Thanks Hi, I have a some online backup software call ahsay, I' am looing to create a API call with a form in PHP Please check out this link www.123-backup.com/obs-admin-guide.pdf this is the guide to create a api for the online backup (page 69) Would some be intrested in creating this api call and php form for me ? p.s please see attached image this is something i'am looking for. if so how much would it be please email me if you want more info! Hi Guys, My 2nd issue with my new blog is that the code used to limit the amount of content displayed from each post, is on occasion truncating the closing html tags along with the excess text! Very often this will result in any following posts being entirely italic or bold, which as you can imagine looks ridiculous. The Code that limits the content: # Limit Post function the_content_limit($max_char, $more_link_text = '', $stripteaser = 0, $more_file = '') { $content = get_the_content($more_link_text, $stripteaser, $more_file); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content, '<h1>,<h2>,<h3>,<strong>,<em>, '); if (strlen($_GET['p']) > 0) { I'd be very grateful if someone could help me out with this. Thanks Steve Here is function that backup mysql database. It's working fine, but I want to download file instead of creating it. /* backup the db OR just a table */ function backup_tables($host,$user,$pass,$name,$tables = '*') { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //get all of the tables if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } //cycle through foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $num_fields = mysql_num_fields($result); $return.= 'DROP TABLE '.$table.';'; $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $num_fields; $i++) { while($row = mysql_fetch_row($result)) { $return.= 'INSERT INTO '.$table.' VALUES('; for($j=0; $j<$num_fields; $j++) { $row[$j] = addslashes($row[$j]); $row[$j] = str_replace("\n","\\n",$row[$j]); if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } if ($j<($num_fields-1)) { $return.= ','; } } $return.= ");\n"; } } $return.="\n\n\n"; } //save file $date= date("d-M-Y_h-i"); $handle = fopen('Backup-'.$date.'.sql','w+'); fwrite($handle,$return); fclose($handle); } Usage: Quote backup_tables('HOST','USER','PASS','TABLE'); But this will create backup file on server. I also tried to add following code in function (at the end): echo $return; header('Content-type: text/plain'); header("Content-Disposition: attachment; filename=\"$filename\""); How to just download file, without showing it on screen instead of using fwrite/fclose function? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=316440.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308924.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=320124.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=353173.0 |