I have written some small (?) script to update a local copy of the
Database of the cddb clone
FreeDB.
For hints on usage see sourcecode and comments in source.
#!/usr/bin/perl -w
use Net::FTP;
#
# Script to update freedb cd-database automatically.
#
# Finds actual date of local db by searching for database-source-files
+ in
# database directory, so if you deleted them, you'll have to create a
+file
# (may be empty) with the filename of the last added db-file, like
# freedb-complete-20021023.tar.bz2 or freedb-update-20020917-20021023.
+tar.bz2
# I didn't find any other possibility to find out the version of the l
+ocally
# installed db. If you have got any idea, please tell me.
#
# You should change the ftp-server settings to a local mirror of yours
+ and
# check the settings for your database and the external programs.
#
# Version 0.70 by Spida (on www.perlmonks.org)
#
# ToDo:
# - Error handling: server unreachable, wrong user/pass, no write acce
+ss,
# hd full, ...
# - Stability in case of killed script. Add ./.freedb_update_status fi
+le
# - resume downloads
#
my $freedb_ftp_mirror = "ftp.freedb.org";
my $freedb_ftp_user = "anonymous";
my $freedb_ftp_password = '-anonymous@';
my $remote_dir = "/ftp.freedb.org/pub/freedb/";
my $database_dir = '/data/raid/cddb';
my $database_user = 'cddb';
my $database_group = 'root';
my $command_database_stop = "/etc/init.d/cddbd stop";
my $command_database_start = "/etc/init.d/cddbd start";
my $command_database_reindex = "/usr/local/bin/cddbd/cddbd -fdv";
my $command_tar = "/bin/tar";
my $command_chown = "/bin/chown";
my $command_chmod = "/bin/chmod";
#connect to ftp
print "Connecting to $freedb_ftp_mirror\n";
my $ftp=Net::FTP->new($freedb_ftp_mirror);
print "Logging in\n";
$ftp->login($freedb_ftp_user, $freedb_ftp_password);
$ftp->cwd($remote_dir);
#get remote filellist
@ftpfilelist = $ftp->ls();
@ftpfilelist = sort(@ftpfilelist);
@ftpcompletefilelist = grep /freedb-complete-\d+.tar.bz2/, @ftpfil
+elist;
@ftpupdatefilelist = grep /freedb-update-\d+-\d+.tar.bz2/, @ftpfil
+elist;
if (@ftpcompletefilelist) {
print "\n";
print "Server has complete files to use:\n";
foreach $file(@ftpcompletefilelist) {
print "$file\n";
}
@ftpcompletefilelist = reverse(@ftpcompletefilelist);
$lastftpcompletefile = $ftpcompletefilelist[0];
}
$laststartversionftpupdatefile=0;
$lastendversionftpupdatefile=0;
if (@ftpupdatefilelist) {
print "\n";
print "Server has update files to use:\n";
foreach $file(@ftpupdatefilelist) {
print "$file\n";
}
@ftpupdatefilelist = reverse(@ftpupdatefilelist);
}
#get local filelist
opendir(DIR, $database_dir);
my @localfilelist = readdir(DIR);
closedir(DIR);
@localfilelist = sort(@localfilelist);
@localcompletefilelist = grep /freedb-complete-\d+.tar.bz2/, @loca
+lfilelist;
@localupdatefilelist = grep /freedb-update-\d+-\d+.tar.bz2/, @loca
+lfilelist;
$lastversionlocalcompletefile=0;
if (@localcompletefilelist) {
print "\n";
print "Found local files which are supposed to be in database:
+\n";
foreach $file(@localcompletefilelist) {
print "$file\n";
}
@localcompletefilelist = reverse(@localcompletefilelist);
$lastlocalcompletefile = $localcompletefilelist[0];
($lastversionlocalcompletefile) = $lastlocalcompletefile =~ /f
+reedb-complete-(\d+).
tar.bz2/;
}
$laststartversionlocalupdatefile=0;
$lastendversionlocalupdatefile=0;
if (@localupdatefilelist) {
print "\n";
print "Found local files which are supposed to be included in
+database:\n";
foreach $file(@localupdatefilelist) {
print "$file\n";
}
@localupdatefilelist = reverse(@localupdatefilelist);
$lastlocalupdatefile = $localupdatefilelist[0];
($laststartversionlocalupdatefile, $lastendversionlocalupdatef
+ile) = $lastlocalupda
tefile =~ /freedb-update-(\d+)-(\d+).tar.bz2/;
}
#find out which file to use
print "\n";
if ($lastversionlocalcompletefile > $lastendversionlocalupdatefile
+) {
print "Last local version seems to be a complete db from $last
+versionlocalcompletef
ile.\n";
$lastversionlocalfile=$lastversionlocalcompletefile;
} else {
if ($lastendversionlocalupdatefile != 0) {
print "Last local version seems to be an update db from $l
+astendversionlocalupd
atefile.\n";
$lastversionlocalfile=$lastendversionlocalupdatefile;
} else {
print "No local db found.\n";
$lastversionlocalfile=0;
}
}
print "\n";
if ($lastversionlocalfile==0) {
print "Starting fresh\n";
push(@dbfiles, $lastftpcompletefile);
} else {
print "Will get following files:\n";
foreach $file(@ftpupdatefilelist) {
($laststartversionftpupdatefile, $lastendversionftpupdatef
+ile) = $file =~ /free
db-update-(\d+)-(\d+).tar.bz2/;
if ($lastversionlocalfile <= $laststartversionftpupdatefil
+e) {
push(@dbfiles, $file);
print "$file\n";
}
}
}
#download updates
print "\n";
if (@dbfiles) {
foreach $dbfile(@dbfiles) {
print "downloading $dbfile\n";
$ftp->get("$dbfile", "$database_dir/$dbfile");
}
} else {
print "No need to update found.\n";
}
$ftp->quit;
#extract updates
if (@dbfiles) {
print "\n";
foreach $dbfile(@dbfiles) {
print "extracting $dbfile\n";
system(`$command_tar xvjf $dbfile -C $database_dir`)
}
#regenerate index
system(`$command_chown -R $database_user:$database_group $data
+base_dir/*`);
system(`$command_chmod -R 644 $database_dir/*`);
system(`$command_chmod 755 * $database_dir/*`);
#regenerate index
system(`$command_database_stop`);
system(`$command_database_reindex`);
system(`$command_database_start`);
}
print "\n";
print "completed\n";