2

I try to execute exp file:

#!/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

I execute if with -f flag, and I get an error: mysqldump: Couldn't find table: "|"

if I try this approach

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\" "

I get an output

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
Andrey Yasinishyn
  • 1,851
  • 2
  • 23
  • 36

3 Answers3

3

When Johannes is saying use a shell, he means:

  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\""
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

mysqldump commands takes parameter as you given and after database name it expect table name to be there

so it is assuming | is a table name

mysqldump [options] db_name [tbl_name ...]

updated answer

in order to dump a set of one or more tables,

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

a set of one or more complete databases

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

or an entire MySQL server—as shown here:

shell> mysqldump [options] --all-databases
developerCK
  • 4,418
  • 3
  • 16
  • 35
  • I need to dump entire database, not table! And it isn't expecting there to be a table name if you run it in bash. This is an issue only fro exp. `mysqldump -h [server] -u [user] -p[password] db1 | mysql -h [server] -u [user] -p[password] db2` is the standard solution for db cloning! – Andrey Yasinishyn Sep 21 '13 at 16:50
  • 1
    use --database before dbname. for more check http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html – developerCK Sep 21 '13 at 16:55
  • Ok.. But this isn't an issue.. Try `mysqldump -h [server] -u [user] -p[password] db1 | mysql -h [server] -u [user] -p[password] db2` yourself. I use it for many years, and I know that this command works. – Andrey Yasinishyn Sep 21 '13 at 16:57
  • yes it works, if my answer does not solver your problem then we both lookup for a solution. just try and let me know – developerCK Sep 21 '13 at 16:59
0

On my Mac I used this command:

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

It was as simple as including a chevron (>) to push out the file where I wanted it.

The screen went silent as it went to work.

sbgib
  • 5,580
  • 3
  • 19
  • 26