Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to create a sync.sh script that coordinates with our less-than-ideal version control for our website. This script will migrate a copy of the live database to our local development environment and a couple other things. I'm having trouble with the mysql migration portion.

This script is run on a development machine. remote is the live host.

# --------------
# database

# ssh tunnel
ssh -L 3307:remote:3306 user@remote

# mysql dump
mysqldump -u someuser -h remote -P 3307 -p"p4ssw0rd" db > localfile.sql

# somehow close ssh tunnel ???

# populate local db with sql dump file
mysql -ulocal db < localfile.sql

# -----------------
# other sync stuff
# ...

When I just run the mysql portion of the script, I'm getting this output:

Pseudo-terminal will not be allocated because stdin is not a terminal.
Enter password: 

We have ssh keys setup and I can ssh to the remote properly. However, when run in the script context, things are going wrong. I think I'm just a total bash noob. I'm guessing that if I simply change the structure of my approach, I should have no problem. I'm just not sure how to do that...

share|improve this question
1  
Note: using the same data in development environment is not the best idea especially when there are some sensitive data in your database like users passwords or users private data. There was few examples when that kind of data leaked from development servers as there was no security at all at devs machines... –  pbm Mar 14 '12 at 18:07

1 Answer 1

up vote 3 down vote accepted

You could do it in one line without the tunnel using something like:

$ ssh user@remote "mysqldump -u user --password=password database_name" | 
  mysql -u local database_name
share|improve this answer
    
Holy toledo! this is awesome, thank you :) –  naomik Mar 14 '12 at 18:00

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.