Skip to content

Instantly share code, notes, and snippets.

@bkbilly
Created January 7, 2019 20:05
Show Gist options
  • Save bkbilly/d7ab169f0ba6db1b71d423f3994470af to your computer and use it in GitHub Desktop.
Save bkbilly/d7ab169f0ba6db1b71d423f3994470af to your computer and use it in GitHub Desktop.
Backup files to a directory and then encrypts it
#!/usr/bin/env python3
import subprocess
import sys
import logging
from shutil import copytree, ignore_patterns, rmtree
import os
from subprocess import call
logging.basicConfig(
filename='/var/log/backup.log',
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logging.info("Started Script")
username = str(subprocess.check_output("whoami").strip().decode('ascii'))
if username != "root":
logging.critical("You have to be root to run this app, current user is: %s" % (username))
sys.exit()
if len(sys.argv) > 1:
password = sys.argv[1]
else:
print('Please provide password')
sys.exit()
tmp_backup_folder = "/home/user/tmp_backup"
tmp_out_targz = "/home/user/tmp_backup.tar.gz"
out_gpg = "/home/user/Dropbox/backup.tar.gz.gpg"
systems = {
"samba": {
"input": "/etc/samba/",
"ignore": [
]
},
"homeassitant": {
"input": "/home/user/.homeassistant/",
"ignore": [
"deps",
"home-assistant_v2.db",
"__pycache__",
"*.pyc",
]
},
}
def get_size(path):
size = sum([sum(map(lambda fname: os.path.getsize(os.path.join(directory, fname)), files)) for directory, folders, files in os.walk(path)])
#2**10 = 1024
power = 2**10
n = 0
Dic_powerN = {0 : '', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
while size > power:
size /= power
n += 1
size = round(size, 2)
return size, Dic_powerN[n]+'B'
if os.path.isdir(tmp_backup_folder):
rmtree(tmp_backup_folder)
if os.path.exists(tmp_out_targz):
os.remove(tmp_out_targz)
if os.path.exists(out_gpg):
os.remove(out_gpg)
os.makedirs(tmp_backup_folder)
for system, options in systems.items():
backup_location = os.path.join(tmp_backup_folder, system)
copytree(options['input'], backup_location, ignore=ignore_patterns(*options['ignore']))
size, unit = get_size(backup_location)
logging.info('%s: %s %s' % (system, size, unit))
print(' %s: %s %s' % (system, size, unit))
size, unit = get_size(tmp_backup_folder)
print('ALL: %s %s' % (size, unit))
a = call("/bin/tar czf %s -C %s ." % (tmp_out_targz, tmp_backup_folder), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
a = call("/usr/bin/gpg --pinentry-mode loopback --passphrase='%s' -o %s -c %s" % (password, out_gpg, tmp_out_targz), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print('Done with backup')
print('To extract the files do the following:')
print(' gpg --output %s %s' % (tmp_out_targz, out_gpg))
print(' mkdir %s' % (tmp_backup_folder))
print(' tar xfo %s -C %s' % (tmp_out_targz, tmp_backup_folder))
print(' rm -R %s' % (tmp_out_targz))
if os.path.isdir(tmp_backup_folder):
rmtree(tmp_backup_folder)
if os.path.exists(tmp_out_targz):
os.remove(tmp_out_targz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment