We help IT Professionals succeed at work.

Perl Script Copy to FTP

AndriesKeun asked
Last Modified: 2013-11-05
Hi there

I have a linux web server running a MySQL database and I want to set up a CRON job to copy the database to my FTP account on a daily basis. I need a PERL script to do the following:

1) Connect to FTP account (hostname, username and password)
2) Copy database using the following command: mysqlhotcopy db_name /path/to/some/dir (run from linux shell)

Sorry, I have little experience in Perl, therefore 500 points goes to a working script.
Watch Question

Top Expert 2009

use Net::FTP;
my $dbname = "Your_DB_Name_here";
my $destdir = "/your/dest/dir/here";
my $hostname = "your.host.name.here";
my $user = "username";
my $pass = "pass";

system("mysqlhotcopy $dbname $destdir");

my @files=<*>;
my $ftp = Net::FTP->new($hostname, Debug => 0) or die "Cannot connect to $hostname: $@";
$ftp->login($user,$pass) or die "Cannot login ", $ftp->message;
foreach (@files) {
    $ftp->get($_) or warn "put failed ", $ftp->message;


"$ftp->get($_) or warn "put failed ", $ftp->message;"

should probably be:

$ftp->put($_) or warn "put failed ", $ftp->message;


Sorry it took so long to respond, been so busy and totally forgot about this.

The script is not working properly. What it is doing is it is copying the DB files to the same directory as the DB, and not to the FTP. No files are copied to the FTP, and no error message come up.

I extra thing to add, how would I create a directory with todays date, i.e. create a directory with the name of '14-09-2007' or similar.

Many Thanks

Top Expert 2009
This one is on us!
(Get your first solution completely free - no credit card required)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.