#!/usr/bin/perl use strict; use warnings; use Cwd; use File::Spec; use Getopt::Long; use Net::FTP; use DBI; use Digest::MD5::File qw(-nofatals file_md5_hex); use File::Basename; my $path = File::Spec->catdir(getcwd()); my $host, my $username, my $password; GetOptions ( "dir=s" => \$path, "host=s" => \$host, "user=s" => \$username, "pass=s" => \$password ) or die("Error in command line arguments\n"); $path = File::Spec->catdir(getcwd(),$path); # Connect my $ftp = Net::FTP->new($host,Passive => 0) or die("Can't connect to: $!"); $ftp->login($username,$password) or die("Couldn't authenticate ftp: $!"); #$ftp->pasv(); $ftp->binary(); my $dbh = DBI->connect("DBI:SQLite:dbname=moving.db", "", "", { RaiseError => 1 }) or die $DBI::errstr; $dbh->do(q{ CREATE TABLE IF NOT EXISTS "files"( hash TEXT, path TEXT, size INT )}); my @tree = ($path); loop: { $path = pop(@tree); opendir(DIR, $path) or die $!; unless ($ftp->cwd($path)) { $ftp->mkdir($path,1) or die "$! ", $ftp->message; $ftp->cwd($path) or die "$! ", $ftp->message; } while (my $file = readdir(DIR)) { next if $file =~ m/^\./ or ! $file; $file = File::Spec->catdir($path,$file); if (-d $file) { push @tree,$file; next; } $ftp->put($file) or die "$! ", $ftp->message; die(sprintf("$file didn't match size %s <> %s",$ftp->size($file),-s $file)) if $ftp->size($file) != -s $file; $dbh->do('INSERT INTO "files"(hash,path,size) VALUES (\''.file_md5_hex($file).'\',\''.$dbh->quote($path).'\',\''.$dbh->quote(-s $path).'\')') or die $DBI::errstr; print "Moved: $path\n"; } closedir(DIR); if (scalar @tree) { goto loop; } } $ftp->quit;