如何使用python进行tar备份 [英] How to make tar backup using python
本文介绍了如何使用python进行tar备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有目录/home/user1和user2. 我想遍历所有用户名的主目录,然后制作tar.gz文件,然后将其存储在/backups目录中.
I have directory /home/user1 , user2 . I want to loop through all usernames home dir and then make the tar.gz file and then store it in /backups directory.
我是python的新手,所以很困惑如何开始
I am new to python so confused how to start
推荐答案
这应该有效:
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进行tar备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文