I have 4 separate drives, all un-RAIDed. Each drive has almost the same root directories, but different contents in each folder.
For example:
/mnt/sda/TV Shows/Lost/
/mnt/sdb/TV Shows/Your_Favorite_Show/
The goal is to have a single folder that has symlinks to all the files in each of the drives. Pretty much a poor man's JBOD.
Previously, I had problems with conditions like 2 drives having the same sub folder contents, but I ended up solving that with the current script I'm using now.
What I'm looking for now is speed. I'm very new to Perl and the script takes about 12 minutes to complete with the current drives.
Basically, the script makes a list of all directories and files in each drive. First, it makes the directories. I didn't use any validation because if a directory already exists, it simply won't make one. However, with the files, I used a hash to only keep the unique files. Then I use the key/value pairs with ln to create every link to the files only, not directories.
Here's my very simple code to get that done. Any ideas on how to speed it up or perhaps any insight to a different approach?
Code:
#!/usr/bin/perl
use warnings;
my @drives_to_sync = qw ( /mnt/sda/ /mnt/sdb/ /mnt/sdc/ /mnt/sdd/);
my @folders_to_sync;
my %create_folders;
my %file_links;
foreach (@drives_to_sync) {
my @folder_to_create = `find $_ -type d`;
my @files_to_create = `find $_ -type f`;
foreach (@folder_to_create) {
chomp ($_);
my $dest = $_;
$create_folders{ $_ } = &link_location($dest);
mkdir $_;
}
foreach (@files_to_create) {
chomp ($_);
my $dest = $_;
$file_links{ $_ } = &link_location($dest);
}
}
sub link_location {
s/\/mnt\/sd./\/mnt\/test/;
$_[0];
}
foreach $key (keys %file_links) {
$value = $file_links{$key};
#print "$key => $value\n";
`ln -s "$value" "$key"`;
}