updated directory name, changed configs and systemctl slightly, preparing to make it useable for backups
This commit is contained in:
Executable
+68
@@ -0,0 +1,68 @@
|
||||
import paramiko
|
||||
import sys
|
||||
import json
|
||||
import logging
|
||||
import unicodedata
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
def create_remote(config, settings):
|
||||
type_r = config["remote_type"]
|
||||
if type_r == "sftp":
|
||||
import remote_sftp
|
||||
remote = remote_sftp.sftp_remote(config, settings)
|
||||
elif type_r == "local_dir":
|
||||
import remote_local_dir
|
||||
remote = remote_local_dir.local_dir_remote(config)
|
||||
return remote
|
||||
|
||||
def distribute_files(path_list, dest):
|
||||
for pathfile in path_list:
|
||||
if not str(pathfile).endswith('.dem'):
|
||||
continue
|
||||
log_msg = "uploading demo: " + str(pathfile)
|
||||
logging.info(log_msg)
|
||||
if not dest.put(pathfile, dest.path):
|
||||
log_msg = ''.join(["failed putting file: ", str(pathfile)])
|
||||
logging.warning(log_msg)
|
||||
sys.exit(1)
|
||||
|
||||
def load_config(config_file):
|
||||
try:
|
||||
with open(config_file) as infile:
|
||||
try:
|
||||
json_dict = json.load(infile)
|
||||
#logging.info(json_dict)
|
||||
return json_dict["remotes"], json_dict["jobs"], json_dict["settings"]
|
||||
except json.JSONDecodeError as e:
|
||||
logging.warning('exception caught: ', exc_info = True)
|
||||
sys.exit(1)
|
||||
except FileNotFoundError:
|
||||
logging.warning('exception caught: ', exc_info = True)
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
#runs demos every 5 minutes but backups thrice a week with systemctl services/timers
|
||||
config_file = 'config_demos.json'
|
||||
if sys.argv[1:]:
|
||||
config_file = sys.argv[1]
|
||||
remotes, jobs, settings = load_config(config_file)
|
||||
for job in jobs:
|
||||
src = create_remote(remotes[job["src"]], settings)
|
||||
dest = create_remote(remotes[job["dest"]], settings)
|
||||
if 'demo_' in job["job_name"]:
|
||||
source_files = src.list_dir()
|
||||
distribute_files(source_files, dest)
|
||||
for pathfile in source_files:
|
||||
src.delete_local_demo(pathfile)
|
||||
dest.delete_remote(source_files)
|
||||
elif "backup_local" in job["job_name"]:
|
||||
zip_directory_cmd = f'zip -r {job["zipname"]}-{str(datetime.now()).split(" ")[0]}.zip {remotes[job["src"]]["path"]}'
|
||||
|
||||
logging.info(('finished job: ', job))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user