Ryflex
Ryflex

Reputation: 5779

Backing up/copying an entire folder tree in batch or python?

I'm trying to copy an entire directory from one locations to another via python every 7 days to essentially make a backup...

The backup folder/tree folder may or may not exist so it needs to create the folder if it doesn't exist, that's why I assumed distutils is better suited over shutil

Note Is it better for me to use batch or some other language for the said job?

The following code:

import distutils
distutils.dir_util.copy_tree("C:\Users\A\Desktop\Test", "C:\Users\A\Desktop\test_new", preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=1, verbose=0, dry_run=0)

Returns:

Traceback (most recent call last):
  File "C:\Users\A\Desktop\test.py", line 2, in <module>
    distutils.dir_util.copy_tree("C:\Users\A\Desktop\test", "C:\Users\A\Desktop\test2", preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=1, verbose=0, dry_run=0)
AttributeError: 'module' object has no attribute 'dir_util'

What am I doing wrong?

Thanks in advance - Hyflex

Upvotes: 1

Views: 1390

Answers (3)

sbrosnihan
sbrosnihan

Reputation: 91

You need to import dir_util specifically to access it's functions:

from distutils import dir_util

If there are other modules in that package that you need, add them to the line, separated by commas. Only import the modules you need.

Upvotes: 8

MtWoRw
MtWoRw

Reputation: 196

I've been attempting essentially the same thing to back up what I write on a plethora of virtual machines.

I ran into the same problem you did with distutils. From what I can tell, the Python community is using the distutils module to start standardizing how new modules interface with Python. I think they're still in the thick of it though as everything I've seen relating to it seems more complicated, not less complicated. Hopefully, I'm just seeing all the crazy that happens in the middle of a big change.

But I did figure out how to get it working. To use distutil.dir_util.copytree(),

>>> from distutils import dir_util
>>> dir_util.copy_tree("/home/user/backing_up/temp", "/home/user/backing_up/other")
['/home/user/backing_up/other/stuff.txt']     # Return value indicating success

If you feel like it's worthwhile, you can import distutils.core and make the longer call to distutils.dir_util.copy_tree().

>>> import distutils.core
>>> distutils.dir_util.copy_tree("/home/user/backing_up/temp", "/home/user/backing_up/other")
['/home/user/backing_up/other/stuff.txt']     # Return value indicating success

(I know, I know, there are subtle differences between "import module.submodule" and "from module import submodule" but that's not the intent of the question and so long as you're importing the correct stuff and calling the functions appropriately, it doesn't make a difference.)

Like you, I also explicitly stated that I wanted the default for preserve_mode and preserve_times, but I didn't touch the other variables. Everything worked as expected once I imported and called the function the way it wanted me to.

Now that my back up script works, I realize I should have written it in Bash since I plan on having it run whenever the machine goes to a specific runlevel. I'm using a wrapper instead now, even if I should just re-write it.

Upvotes: 0

Elad Tabak
Elad Tabak

Reputation: 2417

For Unix/Linux, I suggest 'rsync'. For windows: xcopy

Upvotes: 3

Related Questions