2

我尝试执行 exp 文件:

#!/usr/bin/expect

# mysql credentials and connection data
current_db_name='webui_dev'
new_db_name='db_2013'
db_host='localhost'
db_user='root'
db_pass=''


# using a here-document to pass commands to expect. 
# (the commands could be stored in a file as well)
expect <<EOF
  log_user 0
  spawn mysqldump -h "$db_host" -u "$db_user" -p "$current_db_name" | mysql -h "$db_host" -u "$db_user" -p "$new_db_name"
  expect "password:"
  send "$db_pass\r"
  log_user 1
  expect eof
EOF

我用标志执行 if -f,我得到一个错误:mysqldump: Couldn't find table: "|"

如果我尝试这种方法

exp_internal 1
  spawn sh -c  "mysqldump -h \"$db_host\" -u \"$db_user\" -p \"$current_db_name\" | mysql -h \"$db_host\" -u \"$db_user\" -p \"$new_db_name\" "

我得到一个输出

expect: option requires an argument -- f
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {81914}

expect: does "" (spawn_id exp7) match glob pattern "password:"? no

expect: does "Enter password: " (spawn_id exp7) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "Enter password:"
send: sending "\r" to { exp7 }

Enter password: expect: timed out
4

3 回答 3

3

当 Johannes 说使用 shell 时,他的意思是:

  spawn sh -c "mysqldump -h \"$db_host\" -u \"$db_user\" -p \"$current_db_name\" | mysql -h \"$db_host\" -u \"$db_user\" -p \"$new_db_name\""
于 2013-09-21T16:37:51.473 回答
1

mysqldump 命令采用您给定的参数,并且在数据库名称之后它期望表名存在

所以它假设| 是一个表名

mysqldump [options] db_name [tbl_name ...]

更新的答案

为了转储一组一个或多个表,

shell> mysqldump [options] db_name [tbl_name ...]

一组一个或多个完整的数据库

shell> mysqldump [options] --databases db_name ...

或整个 MySQL 服务器——如下所示:

shell> mysqldump [options] --all-databases
于 2013-09-21T16:45:16.193 回答
0

在我的 Mac 上,我使用了这个命令:

mysqldump -u root -p [database_name] > /Users/rgavila/Documents/_WSCenter/Backup-Jul-5-2021.sql

就像包含一个 V 形 (>) 来将文件推出我想要的位置一样简单。

屏幕开始工作时安静了下来。

于 2021-07-06T03:28:28.663 回答