TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   How to create back up ? (http://www.talkphp.com/advanced-php-programming/4009-how-create-back-up.html)

digitak 02-27-2009 08:15 PM

How to create back up ?
 
Hi ,

i want to create script that create backup from my database !

i cannot use PHP my admin or other programs ! i need a little script

can you help me ?

(i cannot speak English very well)

Innocentus 03-06-2009 08:01 PM

You could use the following OpenSource-Tools for PHP:
MySQLDumper
phpMyBackupPro

Good luck with your project.
With best regards
Innocentus

Sakakuchi 03-07-2009 01:44 PM

Could look something like this?!
!!! THIS CODE IS NOT TESTED !!!
(just written right out of my head - it could be filled with bugs :-P)
PHP Code:

include("db_connection.php");

function 
CreateBackup($db,$table)
{
  
//select database where table is saved
  
$backup "USE $db;\n";
  
$result mysql_query(SHOW CREATE TABLE '$table') or die(mysql_error());
  while(
$row mysql_fetch_array($result))
  {
    for (
$i=1;$i<count($row)-1;$i++)
      {
         
$backup .= $row[$i];
      }
  }
    
$backup .= "\n;";
  
$result mysql_query("SELECT * FROM '$table'") or die (mysql_error());
 WHILE (
$row mysql_fetch_array($result))
  {
    
$backup .= "INSERT INTO $table VALUES(";
    for (
$i=0;$i<count($row);$i++)
      {
        
$cont $row[$i];
        
$backup .= "'$cont',";
      }
      
//we need 2 cut the last "," away
      
$backup substr($backup,0,strlen($backup)-1);
  }
  
$backup .= ");";
  return 
$backup;


Now you just need to write the backup to an file like:
PHP Code:

$content CreateBackup("mydatabase""mytable");
file_put_contents($filename$content); 

Hope this was helpfull :-D

Edit: When anyone finds mistakes/errors - please drop a reply - I'm always willing to learn...

sketchMedia 03-07-2009 07:18 PM

If you have shell access to your server, you can issue a mysqldump command
Code:

  mysqldump database -u username -p password -h host > backup.sql

CoryMathews 03-08-2009 03:14 AM

Quote:

Originally Posted by sketchMedia (Post 22117)
If you have shell access to your server, you can issue a mysqldump command
Code:

  mysqldump database -u username -p password -h host > backup.sql

Linux only for that. So if you have a ms server that would not work.

Salathe 03-08-2009 11:29 AM

Quote:

Originally Posted by CoryMathews (Post 22124)
Linux only for that. So if you have a ms server that would not work.

That will work fine in Windows environment. I just ran mysqldump -h localhost -u backup -pbackup talkphp > w:/talkphp.sql on my WAMP test server (Virtualised Windows XP with WAMP 2.0f) without any problems.

sketchMedia 03-08-2009 10:52 PM

Should work fine in windows, if you set your paths right.

digitak 03-16-2009 06:26 PM

and how about restore ?

sketchMedia 03-16-2009 09:53 PM

like thus:
Code:

mysql -u user -p password -D database < backup.sql
Not sure if mysqldump dumps the db create statement, so you may need to create that first:
Code:

mysql -u user -p password

mysql> create database databasename;

then you can run source:
Code:

mysql> use databasename;
mysql> source backup.sql;

should do it.

Seraskier 03-23-2009 01:08 AM

The way I do it is backup my data server-side with a command prompt.

Of course, I am running Windows Server with root access, if you do have these requirements I will me more than happy to give you a command prompt in order to do it.

Jordan

iflashlord 05-03-2009 02:03 PM

sample back up database
 
PHP Code:

//$conn = your connection handle
//$dbase = your database name
// back up save in file on server
// your backup folder must safe by .htaccess (for security)
// 100% tested ok
$Backup ="";
$Tbl mysql_list_tables($dbase ,$conn);
    while(
$R_t mysql_fetch_row($Tbl)){ 
    
        
$Create_tbl mysql_query("SHOW CREATE TABLE `$R_t[0]`");
        while(
$R_c_t mysql_fetch_array($Create_tbl)){
            
$Backup .= " ".$R_c_t['Create Table'];
        }
        
        
$Backup .= ";\r\n";

        
$Q_t mysql_query("SELECT * FROM `$R_t[0]`");

        while(
$R_c_f mysql_fetch_row($Q_t)) {
            
$Backup .= "INSERT INTO `$R_t[0]` VALUES('$R_c_f[0]' ";

            for(
$i=1;$i<sizeof($R_c_f);$i++){
                
$Backup .= ", '$R_c_f[$i]' ";
            }

        
$Backup .= ");\r\n";

        }

        
$Backup .= "\r\n";
    }
$file_name date('myd').time();
$H fopen("./backup/$file_name.sql","w+");

fwrite($H,$Backup);

fclose($H); 



All times are GMT. The time now is 06:29 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0