Thursday, January 27, 2011

How do I run SSH to my web server using PHP?

I am backing up my database using mysqldump as I read here: http://www.tizag.com/mysqlTutorial/mysqlbackup.php

It gives me some sort of command(Linux I think):

mysqldump --tab=/path/to/some/dir --opt db_name

But it doesn't explain what I do with that command or how I send it to the database so it can backup. It says I need to run SSH to your web server. What does this mean and how can I do that?

EDIT: *The main reason I am doing this is so I can have the DB automatically backup every week using a cronjob. Using PhpmyADmin or any tool that I need to access manually to backup the DB is not a solution I am looking for.*

  • It is assuming your mysql server is some flavor of Unix (Linux, BSD, etc). You need to install the ssh server, then you can access the system from putty or any ssh client. If it is running ubuntu, you have to install openssh-server. Usually like so:

    sudo apt-get install openssh-server

    If you can't get it going easily, I suggest getting someone with some system administration skill to set it up for you.

    From Nathan
  • You need SSH access to your server. It's not really related to PHP, it's a separate service running on the server.

    If you are on shared hosting, it's likely that you don't have SSH access. Depending on who maintains your servers, you will either need to ask them for access or find another backup option.

  • [EDIT] Upon OP clarification, here's what you can do to directly input the command in the cron job:

     /usr/local/mysql/bin/mysqldump -uUSER -pPASSWORD --opt DatabaseName > /path/to/directory/filename.sql 
    

    Note, there is no space between the -p and PASSWORD or -u and USER - replace USER and PASSWORD with a correct database username & password. Also, change the name of DatabaseName to your database name.

    Old Answer:
    If you are not comfortable with SSH, I would advise you to NOT use the shell commands because a little mistake can create big problems. Instead use normal PHP based MySQL administrating utilities like:

    1) PHPMyAdmin
    2) Adminer

    Also, SSH access is not given usually on Shared Hosting servers.

  • How is your database hosted exactly, are you running it on a machine you have access to or do you pay/use a free service?

    From tipu
  • You run the mysqldump command in an SSH session. It's a bit like the old days of DOS you might say in that you type in a series of commands to get work done. It's got nothing to do with PHP or MySQL.

    Where is your website hosted? Do they provide SSH access?

    If they do and you're developing under windows you can use the program named Putty to "run SSH to your web server". That is, if SSH access to your web server is available.

    If not, maybe they provide an alternative way to get a backup? like through phpmyadmin?

    From Matt
  • You could run commands like that with the PHP function passthru():

    <?php
    passthru("mysqldump --tab=/path/to/some/dir --opt db_name");
    ?>
    

    Functions exec and system would work too.

    http://php.net/manual/en/function.passthru.php

    http://www.php.net/manual/en/function.exec.php

    http://www.php.net/manual/en/function.system.php

  • you can connect to your server using ssh via phpseclib:

    http://phpseclib.sourceforge.net/

    From

0 comments:

Post a Comment