开发者

Shell Script SSH behaviours

I started to code shell scripting for about 2 days

currently I have the following code

echo "Permission to Access to Remote Machine"
ssh $USERNAME@$HOSTNAME  "pg_dump -f /dbexport.sql -t tb1 -t tb2 -t tb3 dbname"
echo "Permission to Access to Remote Machine to transfer data file"
scp $USERNAME@$HOSTNAME:/dbexport.sql /export/bin/
echo "Permission to delete temporary file on remote machine"
ssh $USERNAME@$HOSTNAME  "rm /dbexport.sql"
echo "Creating Database..."

Now I have 2 questions

1- is there a good way to make this operation in a once? I have to do it 3 times currently... and I don't want to use expect neither other 3rd party script neither I want to use RSA keys to ssh authentication

2- is there a 开发者_JAVA百科way to know if something has failed on that commands?

Thanks a Lot


To remotely dump the database to a local file I'd dump it to stdout and redirect it locally, e.g.

ssh $USERNAME@$HOSTNAME "pg_dump -f /dev/stdout -t tb1 -t tb2 -t tb3 dbname" > /export/bin/dbexport.sql

The return code of the command line executed on the server is returned to you as the return code of the ssh call. If ssh fails (e.g. connection issue) you get 255. If all is okay you get 0.

Bash provides you the return code of the last executed command in the shell variable $?. You can use it the following way:

ssh $USERNAME@$HOSTNAME "pg_dump -f /dev/stdout -t tb1 -t tb2 -t tb3 dbname" > /export/bin/dbexport.sql
err=$?
if [ $err -ne 0 ]; then
  echo "Dump failed with error code ${err}!"
fi

Or if you are not interested in the exact value:

if ! ssh $USERNAME@$HOSTNAME "pg_dump -f /dev/stdout -t tb1 -t tb2 -t tb3 dbname" > /export/bin/dbexport.sql; then
  echo "Dump failed!"
fi

The pg_dump man page will probably give you the error codes for it. From the ssh man page:

EXIT STATUS
     ssh exits with the exit status of the remote command or with 255 if an
     error occurred.


For your first question, I can't think of a good way to combine the three commands into one If they were all executing commands on the remote machine, you could do something like

ssh user@remote sh -c 'dothis ; dothat'

but you've got an scp in the middle. I'd use an RSA or DSA key, but you said you don't want to do that (why?).

For the second question, the ssh and scp commands both return a status indicating whether they succeeded. For example, this:

ssh user@remote /bin/false ; echo $?

should print 1, indicating failure. (Change $? to $status if you're using csh or tcsh.) Read the man pages (man ssh, man scp) and look for the "EXIT STATUS" section at the bottom.


If you absolutely don't want to work with keys (which I don't understand), there is a way to open a connection and subsequently use it.

You can work with the ssh options ControlMaster and ControlPath: use ControlMaster=yes to establish a connection first, asking for the password, and leaving a control socket on the place you told to do so with ControlPath. Subsequenty, tell your ssh commands to make use of this socket with ControlMaster=no.

echo "Creating control socket"
ssh $USERNAME@$HOSTNAME -o ControlMaster=yes,ControlPath=whereveryouwant -Nf
echo "Permission to Access to Remote Machine"
ssh $USERNAME@$HOSTNAME -o ControlMaster=no,ControlPath=whereveryouwant "pg_dump -f /dbexport.sql -t tb1 -t tb2 -t tb3 dbname"
echo "Permission to Access to Remote Machine to transfer data file"
scp -o ControlMaster=no,ControlPath=whereveryouwant $USERNAME@$HOSTNAME:/dbexport.sql /export/bin/
echo "Permission to delete temporary file on remote machine"
ssh $USERNAME@$HOSTNAME -o ControlMaster=no,ControlPath=whereveryouwant "rm /dbexport.sql"
echo "Creating Database..."

But here I am not quite sure how to get rid of this control connection, which might be a security risk, at the right time. Maybe you replace the -Nf with -f 'sleep 30m'`, depending on how long the complete process will last.

Another alternative could be, if there is no other output on remote stdout, to get the SQL dump directly via pipe, without using a temporary file.

ssh $USERNAME@$HOSTNAME "pg_dump -f - -t tb1 -t tb2 -t tb3 dbname"  > /export/bin/dbexport.sql

Short, tidy, with no potentially disturbing intermediate files. (But I am not sure about the -f -part - maybe there is another way to tell pg_dump to output its stuff to stdout.)


Copy from remote server to local machine with out creating file, use pipe

ssh -C {ssh.user}@{remote_host} \ 'PGPASSWORD={remote_dbpassword} pg_dump -U{remote_dbuser} {remote_dbname} | bzip2 -c' \ | bunzip2 -dc | PGPASSWORD={local_dbpassword} psql -U{local_dbuser} {local_dbname}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜