Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Recursively Backup Directories

Reply
Thread Tools

Recursively Backup Directories

 
 
misceverything@gmail.com
Guest
Posts: n/a
 
      04-05-2008
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.
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      04-06-2008
En Sat, 05 Apr 2008 20:56:31 -0300, <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Rick Dooling
Guest
Posts: n/a
 
      04-06-2008
On Apr 5, 6:56 pm, (E-Mail Removed) 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/Coo.../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
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-06-2008
On Sat, 5 Apr 2008 16:56:31 -0700 (PDT), http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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!
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      04-08-2008
In article <(E-Mail Removed)>,
Jorgen Grahn <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
recursively removing files and directories bahoo Python 2 04-09-2007 07:17 PM
recursively removing files and directories rbt Python 5 01-16-2006 03:19 PM
list files or directories recursively in high efficiency Ken Java 1 12-02-2005 06:49 PM
Copying Directories Recursively using Ant Eric Bresie Java 1 02-13-2004 12:39 AM
Help on Shell API to delete files/directories recursively Trinity C++ 1 11-07-2003 03:33 PM



Advertisments