add zipping files on new workdir

This commit is contained in:
Jakobus Schürz 2020-02-13 10:12:53 +01:00
parent 2c250a8428
commit 5d61634255

View file

@ -12,6 +12,7 @@ import time
import fileinput import fileinput
import argparse import argparse
import zipfile
try: try:
from fuse import FUSE, FuseOSError, Operations from fuse import FUSE, FuseOSError, Operations
@ -27,6 +28,7 @@ class WorkdirFS(Operations):
def __init__(self, args): def __init__(self, args):
self.args = args self.args = args
self.today = datetime.now() - timedelta(hours=self.args.timeoffset) self.today = datetime.now() - timedelta(hours=self.args.timeoffset)
self.yesterday = datetime.now() - timedelta(hours=self.args.timeoffset)
# Helpers # Helpers
# ======= # =======
@ -44,7 +46,15 @@ class WorkdirFS(Operations):
if partial.startswith("/"): if partial.startswith("/"):
partial = partial[1:] partial = partial[1:]
path = os.path.join(check_dir(os.path.join(path, self.today.strftime("%Y-%m-%d"))), partial) path = os.path.join(check_dir(
os.path.join(path, self.today.strftime("%Y-%m-%d")),
os.path.join(path, self.yesterday.strftime("%Y-%m-%d"))
),
partial
)
if self.today > self.yesterday:
self.yesterday = self.today
return path return path
@ -180,16 +190,37 @@ def cleanup_dirs(root):
os.path.join(root, _dir)) os.path.join(root, _dir))
def check_dir(path): def check_dir(path, yesterpath=None):
checkdir = os.path.isdir(path) checkdir = os.path.isdir(path)
if not checkdir: if not checkdir:
try: try:
os.makedirs(path, exist_ok=True) os.makedirs(path, exist_ok=True)
print("Created directory {}".format(path), flush=True) print("Created directory {}".format(path), flush=True)
if yesterpath != None and path != yesterpath:
_zipfiles(yesterpath)
except: except:
print("[-] Makedir error") print("[-] Makedir error")
return path return path
def _zipfiles(path)
print("Zip files in yesterdays archivdir {}".format(path))
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.gz' not in file:
files.append(os.path.join(r, file))
for f in files:
print("file to zip: {}".format(f))
with Zipfile(f+'.gz', allowZip64=True, 'w') as zf:
zf.write(f)
def main(args): def main(args):
#FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True) #FUSE(WorkdirFS(root), mountpoint, nothreads=True, foreground=True)
check_dir(os.path.join(os.environ['HOME'], args.archive)) check_dir(os.path.join(os.environ['HOME'], args.archive))