-2

I have a scenario where I need to copy production database to my dev database on daily basis. Both are different servers. What I have thought of writing a cronjob that will do the stuff. I have written a php script. I am connecting to remote production server via sshpass, taking its dump and then populating that dump.

exec("sshpass -p 'mypassword' ssh root@IP_ADDRESS:PORT");
exec("mysqldump -u root -p DB > production_dump.sql");
exec("mysql -u root -p test < production_dump.sql");

But at first line it throws error of stating

ssh: Could not resolve hostname IP_ADDRESS:PORT: Name or service not known

I have tried given solution on internet but non of them worked. Can any on please explain what I am doing wrong?

Marcin Orlowski
  • 67,279
  • 10
  • 112
  • 132
Awais Qarni
  • 14,876
  • 22
  • 72
  • 134
  • 1
    Backup/Restore ? – RiggsFolly Oct 22 '19 at 14:10
  • 2
    It looks like you wrote bash script wrapped in PHP? Why not make it pure bash? – Marcin Orlowski Oct 22 '19 at 14:10
  • @MarcinOrlowski because I am not very familiar with that and didn't know how can I achieve that in above scenario like running it as cron job – Awais Qarni Oct 22 '19 at 14:11
  • See https://stackoverflow.com/questions/1895185/how-to-ssh-from-within-a-bash-script and https://stackoverflow.com/questions/13928116/write-a-shell-script-to-ssh-to-a-remote-machine-and-execute-commands and https://stackoverflow.com/questions/26434604/bash-script-execute-commands-after-ssh – aynber Oct 22 '19 at 14:16

1 Answers1

1

Your command is failing because it's not formatted right. You need to use one of the following formats:

sshpass -p 'mypassword' ssh root@IP_ADDRESS PORT
sshpass -p 'mypassword' ssh root@IP_ADDRESS -p PORT
sshpass -p 'mypassword' ssh ssh://root@IP_ADDRESS:PORT

However, I'm not sure if the rest of the script will work, especially if it starts asking for a password. A bash script would be the way to go.

aynber
  • 17,808
  • 7
  • 43
  • 52