Skip to content

Commit

Permalink
Added backup module
Browse files Browse the repository at this point in the history
  • Loading branch information
cbell504 committed Jun 16, 2024
1 parent 9abb69e commit 297519c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
1 change: 1 addition & 0 deletions backedup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3

from .constants import Constants
from .backup import Backup
54 changes: 19 additions & 35 deletions backedup/__main__.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,43 @@
#!/usr/bin/env python3

from backedup import Constants
from backedup import Backup

import datetime
import json
import logging
import os
import shutil


def main():
logging.basicConfig(filename='backedup.log', level=logging.INFO)
create_backedup_folder()
if not did_backup_today():
json_configs = get_config_json_file()
backup = Backup(json_configs)
backup.create_backedup_folder()
if not backup.did_backup_today():
backup_directory = create_backup_directory()
create_backup(backup_directory)
backup.start_backup(backup_directory)
print(Constants.DONE)

def get_config_json_file(self):
"""
get_config_json_file
def create_backedup_folder():
if os.path.isdir(Constants.DEST_FOLDER):
print(Constants.FOLDER_EXIST)
return True
else:
os.mkdir(Constants.DEST_FOLDER)


def did_backup_today():
dest_folder_list = os.listdir(Constants.DEST_FOLDER)
for folder in dest_folder_list:
if str(datetime.date.today()) in folder:
print(Constants.OUTPUT_FOLDER_EXIST)
return True
else:
print(Constants.STARTING_UPDATE)
return False


def create_backup(backup_directory):
shutil.copytree(src=Constants.SRC_FOLDER,
dst=backup_directory,
ignore=_logpath)

- Gets the config JSON file and returns the output
"""
logging.info("Current directory: " + os.getcwd())
logging.info("Loading JSON config file.")
with open(Constants.CONFIG_LOCATION, 'r') as file:
return json.load(file)

def create_backup_directory():
"""
create_backup_directory
"""
backup_directory = Constants.DEST_FOLDER + \
Constants.NEW_UPDATE_FOLDER + str(datetime.date.today())
if os.path.isdir(backup_directory):
print(Constants.OUTPUT_FOLDER_EXIST)
return backup_directory


def _logpath(path, names):
logging.info('Copying %s' % path)
return []


if __name__ == "__main__":
main()
58 changes: 58 additions & 0 deletions backedup/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

from backedup import Constants

import datetime
import logging
import os
import shutil

class Backup:

def __init__(self, configs):
self.config = configs
self.destination_folder = self.config["destFolder"]
self.source_folder = self.config["srcFolder"]

def create_backedup_folder(self):
"""
create_backedup_folder
"""
if os.path.isdir(self.destination_folder):
logging.info("Destionation folder does exist. Continuing forward with backup.")
return True
else:
logging.info("Destionation folder does not exist.")
logging.info("Creating destionation folder for backup.")
os.mkdir(self.destination_folder)

def create_source_folder(self):
pass

def did_backup_today(self):
"""
did_backup_today
"""
dest_folder_list = os.listdir(self.destination_folder)
for folder in dest_folder_list:
if str(datetime.date.today()) in folder:
print(Constants.OUTPUT_FOLDER_EXIST)
return True
else:
print(Constants.STARTING_UPDATE)
return False

def start_backup(self, backup_directory):
"""
start_backup
"""
shutil.copytree(src=Constants.SRC_FOLDER,
dst=backup_directory,
ignore=self._logpath)

def _logpath(self, path, names):
logging.info('Copying %s' % path)
return []


8 changes: 4 additions & 4 deletions backedup/constants.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3


class Constants:
DONE = "DONE!"
FOLDER_EXIST = "The Backedup folder exist!"
DEST_FOLDER = "/media/passport/backedup"
NEW_UPDATE_FOLDER = "/backedup_"
DEST_FOLDER = "./dest"
NEW_UPDATE_FOLDER = "./new_folder"
OUTPUT_FOLDER_EXIST = "An update has already occurred today!"
SRC_FOLDER = "/home/chris"
SRC_FOLDER = "./src_folder"
STARTING_UPDATE = "Starting Backedup..."
CONFIG_LOCATION = "./backedup.json"

0 comments on commit 297519c

Please sign in to comment.