Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am backing up my database using mysqldump as I read here:

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

share|improve this question

migrated from Aug 23 '10 at 5:29

This question came from our site for professional and enthusiast programmers.

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

share|improve this answer
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 '10 at 4:28
@zeckdude, I have updated answer according to your needs. – shamittomar Aug 23 '10 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 '10 at 4:56
@zeckdude, Yes, just don't use PHP code ilke date('m-d-Y') in that. – shamittomar Aug 23 '10 at 5:18
Great! Why should I not use the date in that? – zeckdude Aug 23 '10 at 5:19

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

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

Functions exec and system would work too.

share|improve this answer

you can connect to your server using ssh via phpseclib:

share|improve this answer

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.

share|improve this answer
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 '10 at 4:35

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.

share|improve this answer
I do have SSH access to my server, I just don't know how to connect to it using PHP. – zeckdude Aug 23 '10 at 4:30

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?

share|improve this answer
I have access to it and I have SSH access. – zeckdude Aug 23 '10 at 4:34

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?

share|improve this answer
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 '10 at 4:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.