Back up mysql db from shell

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • apostolosl@gmail.com

    Back up mysql db from shell

    Hi there.
    I need to update a remote database from a local one. I use mysqldump in
    my php page in order to create a file for each table, like this:

    $command="mysql dump table_products > table_products. sql";
    system($command );

    This works perfect and it gives me a file with a drop-table,
    create-table and all the insert values.

    Then I need to connect to the remote server and run the
    table_products. sql file.

    The way I do that is by calling the following in my php page:

    $command="mysql --host=$host --user=$user --password=$pass
    --database=$db;";
    system($command );

    , where $host, $user etc are being set at the top of the page. This
    works fine and I get connected to the remote database.

    Now I need to run the following mysql command: source stb_products.sq l

    When I try this sequence from a command line (windows dos prompt), it
    works perfect. When I
    call "mysql --host=$host --user=$user --password=$pass --database=$db;"
    in command line I get the "mysql>" prompt, and the run the "source
    table_products. sql" and the remote database gets updated.

    When I do this through PHP the last command doesn't work.

    I guess I am not calling 'source' from within a "mysql" prompt?

    How should I do it?

    Any other ideas for achieving the same result? I need to do everything
    from a single PHP page that runs on a local machine.

    The full code is the following :


    //DUMP DATA IN FILE
    $command="mysql dump backcatalogue table_products > table_products. sql";
    system($command );

    //CONNECT TO REMOTE DATABASE
    $command="mysql --host=$ohost --user=$ouser --password=$opass
    --database=$odb;" ;
    system($command );

    //UPDATE REMOTE DATABASE FROM FILE
    $command ="mysql source table_products. sql;";
    system("command ") ;



    Thanks in advance,
    Asaq


    END PHP CODE -->

  • Andy Jeffries

    #2
    Re: Back up mysql db from shell

    On Tue, 09 May 2006 00:35:55 -0700, apostolosl wrote:[color=blue]
    > $command="mysql --host=$host --user=$user --password=$pass
    > --database=$db;";
    > system($command );
    >
    > , where $host, $user etc are being set at the top of the page. This works
    > fine and I get connected to the remote database.
    >
    > Now I need to run the following mysql command: source stb_products.sq l[/color]

    That's where you're going wrong. What you want to do is:

    $command="mysql --host=$host --user=$user --password=$pass
    --database=$db < stb_products.sq l";
    system($command );

    Cheers,


    Andy


    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • tihu

      #3
      Re: Back up mysql db from shell


      apostolosl@gmai l.com wrote:[color=blue]
      > Hi there.
      > I need to update a remote database from a local one. I use mysqldump in
      > my php page in order to create a file for each table, like this:
      >
      > $command="mysql dump table_products > table_products. sql";
      > system($command );
      >
      > This works perfect and it gives me a file with a drop-table,
      > create-table and all the insert values.
      >
      > Then I need to connect to the remote server and run the
      > table_products. sql file.
      >
      > The way I do that is by calling the following in my php page:
      >
      > $command="mysql --host=$host --user=$user --password=$pass
      > --database=$db;";
      > system($command );
      >[/color]

      Adding "< stb_products.sq l" to the end of the command line should work.


      Tim

      Comment

      • apostolosl@gmail.com

        #4
        Re: Back up mysql db from shell

        Thanks guys, I thought I tried this... Probably I was adding a
        semicolon at the end of the command...
        It works like a charm now!!!
        Thanks again and take care...
        Apostolos

        Comment

        Working...