create backup with python 3 one archive
import os
import datetime
import subprocess
import shutil
import errno
from pathlib import Path
home = str(Path.home())
now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
date_string = "{}".format(now)
dest = ''
'''
dest var used to move created archives to another location
example : dest = '/media/disk/'
'''
#dest = '{}/backup'.format(home)
'''
folders is a list of folder paths to backup
for example
folders = [
'/var/www/',
'.ssh'
]
'''
folders = [
]
try:
if dest is not None:
os.makedirs(dest)
except OSError as e:
if e.errno == errno.EEXIST:
print('Directory not created.')
else:
raise
archived_name = "backup_{}.tar.gz".format(date_string)
print('create archive for folder :', archived_name)
out = subprocess.Popen(['tar','--use-compress-program=pigz','-cvf', archived_name ] + folders,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
archive_exists = os.path.exists("{}/{}".format(home, archived_name))
if archive_exists:
print('archive {} created '.format(archived_name))
if dest:
if archived_name:
filename = "{}/{}".format(home, archived_name)
shutil.move(os.path.join(
home, archived_name), os.path.join(dest, archived_name))