Magnolia25 has asked for the wisdom of the Perl Monks concerning the following question:

I need to copy the directory and subdirectories to some other location. FROM Path 1: /tmp/home/status/ TO Path2: /home/status/

if user creates any directory tree under Path 1, then my script should check the directory and subdirectories + files created under that and copy the directory structure from Path 1: /tmp/home/status/ to Path2: /home/status/

Now when I run the script again :

1.This time then my script should check the directory and subdirectories + files created under that and copy the directory structure from Path 1: /tmp/home/status/ to Path2: /home/status/ OR 2. ALSO It should see the latest modified directory and subdirectories + files created under that and copy only the latest changes. it would compare two directories (including subdirectories) together and do: - file comparison ... is in directory_1 at source path but not in target path directory_1 - file ... FileX is in both directories, but is modified after previous copy.

Any suggestions how to proceed on this.Thanks

  • Comment on Copy directory structure and only latest changes in them when re-run

Replies are listed 'Best First'.
Re: Copy directory structure and only latest changes in them when re-run
by Athanasius (Archbishop) on Nov 20, 2016 at 05:45 UTC

    Hello Magnolia25,

    If you want to implement this yourself, you should look at the make_path function in the core module File::Path, and consider testing files either with one of the file test functions (such as -A for access time) or (better) with a module such as Digest::MD5 to determine which files need updating. (If you use MD5 you will need to store the known file hashes. A lightweight database such as DBD::SQLite will be useful here.)

    But why reinvent the wheel when a module such as Dackup is available on ? As a test, I created a text file in a suitable directory structure and then ran the following script (adapted directly from the documentation):

    #! perl use strict; use warnings; use Dackup; my $source = Dackup::Target::Filesystem->new ( prefix => '.\tmp\home', ); my $destination = Dackup::Target::Filesystem->new ( prefix => '.\home', ); my $dackup = Dackup->new ( source => $source, destination => $destination, delete => 0, dry_run => 0, verbose => 1, throttle => '1Mbps', ); $dackup->backup;

    I then edited the text file and re-ran the script to verify that it copied the changed file to the destination (backup) location. You may need to experiment a bit to determine whether it meets your backup criteria.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Copy directory structure and only latest changes in them when re-run
by BrowserUk (Patriarch) on Nov 20, 2016 at 05:51 UTC

    Depending on your platform look at:


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Copy directory structure and only latest changes in them when re-run
by hippo (Archbishop) on Nov 20, 2016 at 12:00 UTC
      This sounds like a job for File::Rsync to me.

      ... or just bare rsync.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)