How to backup tar using python

I have a directory / home / user 1, user2. I want to iterate over all the usernames home names, and then make a tar.gz file and then save it in the / backups directory.

I'm new to python, so confused how to get started

+4
source share
2 answers

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() 
+10
source

All Articles