views:

16

answers:

2

I'm creating a snippet to be used in my Mac OS X terminal (bash) which will allow me to do the following in one step:

  1. Log in to my server via ssh
  2. Create a mysqldump backup of my Wordpress database
  3. Download the backup file to my local harddrive
  4. Replace my local Mamp Pro mysql database

The idea is to create a local version of my current online site to do development on. So far I have this:

ssh server 'mysqldump -u root -p'mypassword' --single-transaction wordpress_database > wordpress_database.sql' && scp [email protected]:~/wordpress_database.sql /Users/me/Downloads/wordpress_database.sql && /Applications/MAMP/Library/bin/mysql -u root -p'mylocalpassword' wordpress_database < /Users/me/Downloads/wordpress_database.sql

Obviously I'm a little new to this, and I think I've got a lot of unnecessary redundancy in there. However, it does work. Oh, and the ssh command ssh server is working because I've created an alias in a local .ssh file to do that bit.

Here's what I'd like help with:

  1. Can this be shortened? Made simpler?
  2. Am I doing this in a good way? Is there a better way?
  3. How could I add gzip compression to this?

I appreciate any guidance on this. Thank you.

A: 

That's the way I would do it too.

To answer your question #3:

Q: How could I add gzip compression to this?

A: You can run gzip wordpress_database.sql right after the mysqldump command and then scp the gzipped file instead (wordpress_database.sql.gz)

ryanprayogo
If I do that, then I guess I would need to un-gzip it before the local database overwrite?
leggo-my-eggo
that is correct. That can be done by `gunzip wordpress_database.sql.gz`
ryanprayogo
A: 

You can dump it out of your server and into your local database in one step (with a hint of gzip for compression):

ssh server "mysqldump -u root -p'mypassword' --single-transaction wordpress_database | gzip -c" | gunzip -c | /Applications/MAMP/Library/bin/mysql -u root -p'mylocalpassword' wordpress_database

The double-quotes are key here, since you want gzip to be executed on the server and gunzip to be executed locally.

I also store my mysql passwords in ~/.my.cnf (and chmod 600 that file) so that I don't have to supply them on the command line (where they would be visible to other users on the system):

[mysql]
password=whatever

[mysqldump]
password=whatever
Brian
Note that if you are using this approach, if your connection breaks (disconnected, etc) in the middle, your local DB will end up in an inconsistent state (only some of the tables created)
ryanprayogo
Oh, this is really excellent. Is the ~/.my.cnf file on the server? Or the local machine?
leggo-my-eggo
@ryanprayogo I'm fine with that, actually, as the local copy is only for development, and I'm the only one with access.
leggo-my-eggo
OK, so it seems as though I need two .my.cnf files, one on the local machine for the mysql command, and one on the server for the mysqldump command. Is this correct? Is the one on the server a security risk for any reason?
leggo-my-eggo
@leggo-my-eggo, yes you would need one .my.cnf on each machine. If there are other users on the server, I would consider the .my.cnf to be *less* of a security risk. If you change the permission on that file to 600 (owner read/write), then it can only be accessed by you and root. But if you enter the password as part of the command, then anybody else on the system can get the password through a process listing.
Brian
God I love this site. @Brian and @ryanprayogo, thank you both.
leggo-my-eggo