1

I am trying to make a shell script that creates a mysql dump and then puts it on another computer. I have already set up keyless ssh and sftp. They script below creates the mysql dump file on the local computer when it is run and doesn't throw any errors, however the file "dbdump.db" is never put on the remote computer. If I execute the sftp connection and put command by hand then it works.

contents of mysql_backup.sh

mysqldump --all-databases --master-data > dbdump.db
sftp -b /home/tim tim@100.10.10.1 <<EOF
put dbdump.db
exit
EOF
jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160

3 Answers3

3

Try to use scp that should be easier in your case.

scp dbdump.db tim@100.10.10.1:/home/tim/dbdump.db

Both sftp and scp are using SSH.

rekire
  • 47,260
  • 30
  • 167
  • 264
1

Please write mput/put command into one file (file_contains_put_command) and try below command.

sftp2 -B file_contains_put_command /home/tim tim@100.10.10.1 >> log_file

Example:

echo binary > sample_file
echo mput dbdump.db >> sample_file
echo quit >> sample_file
sftp2 -B sample_file /home/tim tim@100.10.10.1 >> log_file
rekire
  • 47,260
  • 30
  • 167
  • 264
Prakash Mani
  • 115
  • 1
  • 2
  • 13
0

Your initial approach is a few characters off working though. You're telling sftp to read it's batch-commands from /home/tim -b /home/tim. So, if you change this to -b -, it should read it's batch commands from stdin.

Something along these lines, if -b /home/tim were intended to i.e. change directory remotely, you can add cd /home/tim to your here document.

mysqldump --all-databases --master-data > dbdump.db
sftp -b - tim@100.10.10.1 <<EOF
put dbdump.db
exit
EOF
Kjetil Joergensen
  • 1,595
  • 11
  • 10