Nathaniel Meyer Nathaniel Meyer - 15 days ago 6
Bash Question

FTP Database Dump

I have a backup server that I use to FTP to my live site from a shell file. Currently, it can FTP in and send a zip folder to the backup server. What I also would like it to do if get a dump of the database.

I have used the following command on the live site Cron Jobs to create the file. I would like to avoid this and have it all done from that shell script from the FTP

mysqldump -u User DB_name -p PASS > DB.sql


This command works if I run it on the live site from the Cron Jobs. What do I need to do differently in the FTP?

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD

mysqldump -u User DB_name -p PASS > DB.sql

END_SCRIPT
exit 0


The error i get is

?Invalid command.

Answer

This part goes at the top because it's a script command, not an FTP command:

mysqldump -u User DB_name -p PASS > DB.sql

You got the error message because you put that shell command inside the ftp HEREDOC.

Then you do the first part of your ftp connection HEREDOC (not sure what 'quote' was supposed to do):

ftp -n $HOST <<END_SCRIPT
USER $USER
PASS $PASSWD

Also depending on your ftp client flavor, you may just try USER $USER $PASSWD if the stuff above didn't seem to work. Now put in your ftp commands:

cd theDirectoryWhereIWantToDropTheFile
put DB.sql
quit

Lastly, the end of the HEREDOC:

END_SCRIPT

So all together now:

#!/bin/sh
#This is my script to make a sql dump file and send it via FTP to $HOST
HOST=theFTPServerName
USER=theUserName
PASSWD=thePassword
mysqldump -u User DB_name -p PASS > DB.sql
ftp -n $HOST <<END_SCRIPT
USER $USER
PASS $PASSWD
cd theDirectoryWhereIWantToDropTheFile
put DB.sql
quit
END_SCRIPT

Replace the bogus values for HOST, USER, and PASSWORD and you've got yourself a script that can insecurely send your data with no guarantee that it will arrive intact (thanks FTP!).

Comments