2

I have created a bash script for the automation of my database restore. When I run the following commands, I get /my/sql/file/path.sql: No such file or directory.

ssh $USER@"$cloneMysqlHost" gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql 

I did an ls -lrot on the host I ssh to, just to make sure the file exists the permissions are correct, and they are.

Any ideas what I'm doing wrong?

Thanks in advance!

3
  • 1
    Your intent is unclear because of the && and <, which are probably not doing what you expect they are. Where exactly are the files in question (local and remote), and where is mysql supposed to be running?
    – jbafford
    Commented Jan 11, 2016 at 19:12
  • Have you tried putting "" around the commands to be executed on the remote host? Something like "gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql" so you get more than the gunzip executed there.
    – DDay
    Commented Jan 11, 2016 at 19:14
  • 2
    i doubt that there is a command named "MySQL". *nix systems are case sensitive so you should probably use mysql instead.
    – Marc Bredt
    Commented Jan 11, 2016 at 19:16

2 Answers 2

2

The && is causing the local shell to split the command and run the MySQL command locally.

The < redirection is also being done locally (and the cause of your error).

The gunzip is being performed on the remote host though.

You need to quote the entire argument to ssh if you want it all run on the remote system.

ssh "$USER@$cloneMysqlHost" 'gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql'
1
  • Thank you so much!! I wasn't aware that anything after the && would be done locally. This worked liked a charm!
    – Samantha
    Commented Jan 11, 2016 at 19:57
0

Are you providing the password properly? Also, not sure what's going on with the &&, should use a pipe there. MySql is probably not valid, use mysql. See here for more details.

ssh $USER@"$cloneMysqlHost" gunzip /path/file.sql.gz | mysql -u root -p [password] db_name
2
  • A pipe is incorrect here. gunzip is not sending data to standard output (though it could be changed to do that and avoid the "temporary" file). Commented Jan 11, 2016 at 19:20
  • @EtanReisner Ah, right you are. Your answer looks correct. Commented Jan 11, 2016 at 19:24

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