shell - Proper escaping of quotes and parens when using expect, ssh, ant and mysqldump -
we have working ant script use mysqldump dump database on remote server file on remote server. use ant's exec task run expect script ssh remote machine , run mysqldump command. i'm trying update command restrict dump include data less 3 months old. i'm running annoying escaping issues quotes , parens.
here command line i'm trying working:
expect -f ssh-pass.exp <server_passwd> ssh <user>@<ip_address> \"mysqldump -h localhost -u user --password=passwd staging --where=\"createtimefi > now() - interval 3 month\" --ignore-table=staging.jms_messages --ignore-table=staging.jms_roles --ignore-table=staging.jms_subscriptions --ignore-table=staging.jms_transactions --ignore-table=staging.jms_users --ignore-table=staging.timers > dump.sql\"
here expect script:
set password [lrange $argv 0 0] set command [lrange $argv 1 1] set arg1 [lrange $argv 2 2] set arg2 [lrange $argv 3 end] puts "command: $command" puts "arg1: $arg1" puts "arg2: $arg2" set timeout -1 spawn $command $arg1 "$arg2" match_max 100000 expect "*?assword:*" puts "\rsending password..." send -- "$password\r" puts "\r**************** running remote command, wait... ******************" expect eof
i've tried various combinations of escaping quotes around ssh command , argument mysqldump no success. expect complain "interval" unknown command. on local machine command create files called "now" or "dump.sql" seem indicate local shell eating quotes , doing redirects shouldn't or complain invalid tokens (i.e. parens).
this not expect issue. it's related shell quoting.
you need pass long mysqldump command , arguments single "word", can't escape outer quotes. , can use single quotes outer quotes don't have escape inner double quotes.
expect -f ssh-pass.exp <server_passwd> ssh <user>@<ip_address> 'mysqldump -h localhost -u user --password=passwd staging --where="createtimefi > now() - interval 3 month" --ignore-table=staging.jms_messages --ignore-table=staging.jms_roles --ignore-table=staging.jms_subscriptions --ignore-table=staging.jms_transactions --ignore-table=staging.jms_users --ignore-table=staging.timers > dump.sql'
====
update: notes tcl (expect)
- use
[lindex $argv 0]
instead of[lrange $argv 0 0]
- the mysqlcommand sent braces because create list (
[lrange $argv 3 end]
) should single string. you're using proper shell quoting,[lindex $argv 3]
suffice , solve issue too. if don't pass expect 1 argument,set remote_command [join [lrange $argv 3 end]]
,spawn $command $arg1 $arg2
Comments
Post a Comment