开发者

How to make tar backup using python

I have directory /home/user1 , user2 . I want to loop through all usernames home 开发者_StackOverflowdir and then make the tar.gz file and then store it in /backups directory.

I am new to python so confused how to start


This should work:

import os
import tarfile

home = '/home/'
backup_dir = '/backup/'

home_dirs = [ name for name in os.listdir(home) if os.path.isdir(os.path.join(home, name)) ]

for directory in home_dirs:
    full_dir = os.path.join(home, directory)
    tar = tarfile.open(os.path.join(backup_dir, directory+'.tar.gz'), 'w:gz')
    tar.add(full_dir)
    tar.close()


python write string directly to tarfile and http://docs.python.org/library/tarfile.html#tar-examples

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜