Recursively Backup Directories

M

misceverything

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.
 
G

Gabriel Genellina

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.
 
R

Rick Dooling

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
 
J

Jorgen Grahn

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
 
A

Aahz

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top