0

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.

7 Answers 7

1

[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.

5
  • Thanks for the suggestion, but I am looking to have a php script automatically backup the DB using a cronjob, so your solution wouldn't work for me.
    – zeckdude
    Aug 23, 2010 at 4:28
  • @zeckdude, I have updated answer according to your needs.
    – shamittomar
    Aug 23, 2010 at 4:38
  • Wow, is that the same as shell_exec("mysqldump --add-drop-table -h localhost -uUser -pPassword database_name > ".date('m-d-Y_', time())."file.sql"); except that I won't have to make the cronjob run the php script?
    – zeckdude
    Aug 23, 2010 at 4:56
  • @zeckdude, Yes, just don't use PHP code ilke date('m-d-Y') in that.
    – shamittomar
    Aug 23, 2010 at 5:18
  • Great! Why should I not use the date in that?
    – zeckdude
    Aug 23, 2010 at 5:19
1

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

1

you can connect to your server using ssh via phpseclib:

http://phpseclib.sourceforge.net/

0

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.

1
  • I am looking for a solution where my DB is automatically backed up every week running a php script using a cronjob. Putty is an application I will need to manually use, correct?
    – zeckdude
    Aug 23, 2010 at 4:35
0

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.

1
  • I do have SSH access to my server, I just don't know how to connect to it using PHP.
    – zeckdude
    Aug 23, 2010 at 4:30
0

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?

1
  • I have access to it and I have SSH access.
    – zeckdude
    Aug 23, 2010 at 4:34
0

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?

1
  • I am looking to backup my DB automatically. Using Putty or PhpMyAdmin will require me to manually backup the DB, when I need an automatic solution.
    – zeckdude
    Aug 23, 2010 at 4:33

You must log in to answer this question.