Recursively Backup Directories

Discussion in 'Python' started by misceverything@gmail.com, Apr 6, 2008.

  1. Guest

    I am writing a script that will backup specified folders from one hard
    drive to another (for example, backup source "C:\DATA", destination "D:
    \Backup"), and was thinking of using shutil. What I would like to do
    is recursively backup the specified directories (which copytree will
    do), but be able to specify exclusion directories (which copytree does
    not appear to allow you to do). My initial thoughts were I'll
    probably have to use os.path.walk for the backup source directory, but
    I'm not sure how to go from there. Thanks in advance.
    , Apr 6, 2008
    #1
    1. Advertising

  2. En Sat, 05 Apr 2008 20:56:31 -0300, <> escribió:

    > I am writing a script that will backup specified folders from one hard
    > drive to another (for example, backup source "C:\DATA", destination "D:
    > \Backup"), and was thinking of using shutil. What I would like to do
    > is recursively backup the specified directories (which copytree will
    > do), but be able to specify exclusion directories (which copytree does
    > not appear to allow you to do). My initial thoughts were I'll
    > probably have to use os.path.walk for the backup source directory, but
    > I'm not sure how to go from there. Thanks in advance.


    I'd use os.walk (not os.path.walk) and shutil.copy2; use os.makedirs to
    create the target directory (only when it doesn't already exist).
    If you remove directories from the dirnames list, they're not recursed
    into.

    --
    Gabriel Genellina
    Gabriel Genellina, Apr 6, 2008
    #2
    1. Advertising

  3. Rick Dooling Guest

    On Apr 5, 6:56 pm, wrote:
    > What I would like to do
    > is recursively backup the specified directories . . .
    > but be able to specify exclusion directories (which copytree does
    > not appear to allow you to do). My initial thoughts were I'll
    > probably have to use os.path.walk for the backup source directory, but
    > I'm not sure how to go from there. Thanks in advance.


    There's a nice Python Cookbook recipe.

    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/191017

    I think the one in the book is newer and better

    http://tinyurl.com/5vr4n6

    And the Cookbook is my favorite way to learn Python.

    rd
    Rick Dooling, Apr 6, 2008
    #3
  4. Jorgen Grahn Guest

    On Sat, 5 Apr 2008 16:56:31 -0700 (PDT), <> wrote:
    > I am writing a script that will backup specified folders from one hard
    > drive to another (for example, backup source "C:\DATA", destination "D:
    > \Backup"), and was thinking of using shutil.


    I'd avoid doing that (writing a backup script, that is).

    It may be fine if your goal is using it only under MS-DOS, and not
    distribute it. But making it robust (vital to a backup utility!) and
    portable is tricky. You have to handle things like
    - symbolic links (when to follow, when not to)
    - hard links
    - copying metadata of various kinds (timestamps, permissions,
    ACLs, file system-specific metadata)
    - non-obvious error handling (like copying the file 'foo', but
    the target exists as a directory and must be rmdir()ed first)
    - ...

    I believe it is better to write a script which drives a widely known
    and well-tested copying utility. On Unix these include tar, cpio and
    rsync -- don't know which ones are common under DOS (xcopy?)

    I guess I'm saying that I do not trust module shutil. I see now that
    it documents how it treats some of the things above, but ...

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
    Jorgen Grahn, Apr 6, 2008
    #4
  5. Aahz Guest

    In article <>,
    Jorgen Grahn <> wrote:
    >
    >I believe it is better to write a script which drives a widely known
    >and well-tested copying utility. On Unix these include tar, cpio and
    >rsync -- don't know which ones are common under DOS (xcopy?)


    Just use pax (I haven't bothered learning it because I haven't used
    Windows in years, but it's the only cross-platform archive/copy tool
    available).
    --
    Aahz () <*> http://www.pythoncraft.com/

    "It is easier to optimize correct code than to correct optimized code."
    --Bill Harlan
    Aahz, Apr 8, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Bresie
    Replies:
    1
    Views:
    1,722
    Ryan Stewart
    Feb 13, 2004
  2. Ken
    Replies:
    1
    Views:
    3,722
    Roedy Green
    Dec 2, 2005
  3. Trinity
    Replies:
    1
    Views:
    501
    Peter van Merkerk
    Nov 7, 2003
  4. gavino
    Replies:
    13
    Views:
    1,030
  5. Replies:
    0
    Views:
    569
Loading...

Share This Page