SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql backup via php?

    can I backup an entire mysql DB with PHP then maybe save it as a notepad file?

    thanks
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  2. #2
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use mysqldump

  4. #4
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys.

    I tried this:

    PHP Code:
    mysql_connect($host$user$pass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    $backupFile "web_db_backup_" date("Y-m-d-H-i-s") . '.gz';
    $command "mysqldump --opt -h $host -u $user -p $pass $dbname | gzip > $backupFile";
    if(
    system($command)){
        echo 
    'Backup successful';
    }else{
        echo 
    'error could not backup';

    but im getting 'error could not backup' everytime....am I missing something?

    Thanks.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  5. #5
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do I need to use chmod? if so how would I do that?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  6. #6
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I thought maybe it wasnt working because of a permisions thing to write the file so I added chmod($backupFile,0755); just before i call the system() but then I get:

    Warning: chmod(): No such file or directory in /home/httpd/vhosts/kranichs.com/httpsdocs/admin/web_db_backup.php on line 13
    error could not backup

    Any ideas?
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to set write permissions to the directory you're trying to create file in.
    Saul

  8. #8
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    You need to set write permissions to the directory you're trying to create file in.
    thanks, any idea on how? is this something I can do myself or I need to have my hosting provider do?

    Thanks again.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  9. #9
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few things:

    1. You should be able to set the permissions of the directory either in SSH if you have it, or in FTP.
    2. You don't need to use mysql-connect and mysql_select_db, because it's a Unix command and doens't take a mysql connection resource
    3. There should be no space between -p and $pass
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  10. #10
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try adding this just before you CHMOD:

    PHP Code:
    fopen($backupFilew);
    fclose($backupFile); 
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  11. #11
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks devbanna ..i changed the permissions via FTP it was much easier than I thought it would be....i commented out the functions you said I did not need and i removed the space so my code now looks like this:
    PHP Code:
    #mysql_connect($host, $user, $pass) or die(mysql_error());
    #mysql_select_db($dbname) or die(mysql_error());

    $backupFile "web_db_backup_" date("Y-m-d-H-i-s") . '.gz';
    $command "mysqldump --opt -h $host -u $user -p$pass $dbname | gzip > $backupFile";
    #chmod($backupFile,0755);
    if(system($command)){
        echo 
    'Backup successful';
    }else{
        echo 
    'error could not backup';

    but still same thing.. error could not backup

    Any ideas what else might be causing this?

    Thanks.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  12. #12
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not seeing the definition of your database variables; you are defining them right?
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  13. #13
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, perhaps try this:

    PHP Code:
    system($command$retval);
    if (!
    $retval) {
    echo 
    "Success";
    } else {
    echo 
    "Failure";

    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  14. #14
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot devbanna, that saved the file.....and yes lol I was defining my db_vars i just didnt want to display their values to everyone lol.

    when I extract the .gz file tho It contains another .gz file (the same exact thing) then if I extract that one its empty???

    very strange.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •