49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
import os
|
|
import shutil
|
|
from src import config
|
|
import datetime
|
|
|
|
class Backup:
|
|
def __init__(self):
|
|
self.source_path = config.database.path + "/" + config.database.name
|
|
self.backup_path = config.database.backupLocation + "/" + config.database.name
|
|
self.backup = False
|
|
if not os.path.exists(config.database.backupLocation):
|
|
os.makedirs(config.database.backupLocation)
|
|
backupPath = config.database.backupLocation
|
|
# create an archive path based on the backuppath, one level up with a new folder called archive
|
|
# example: backuppath = /home/user/backup
|
|
# result: archivepath = /home/user/archive
|
|
self.archivePath = os.path.join(os.path.dirname(backupPath), "archive")
|
|
# check if the archive path exists, if not create it
|
|
if not os.path.exists(self.archivePath):
|
|
os.makedirs(self.archivePath)
|
|
|
|
if config.database.do_backup is True:
|
|
self.checkpaths()
|
|
config.database.do_backup = self.backup
|
|
|
|
def checkpaths(self):
|
|
if os.path.exists(config.database.backupLocation):
|
|
self.backup = True
|
|
|
|
def createBackup(self):
|
|
if os.path.exists(self.archivePath) and os.path.exists(self.source_path):
|
|
# copy the active database to the archive path, add _[date]
|
|
day = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
|
# copy the active database to the archive path, add _[date]
|
|
shutil.copy(
|
|
self.source_path,
|
|
os.path.join(
|
|
self.archivePath, config.database.name + "_" + day + ".archive"
|
|
),
|
|
)
|
|
|
|
if self.backup:
|
|
if os.path.exists(self.source_path):
|
|
if os.path.exists(self.backup_path):
|
|
os.remove(self.backup_path)
|
|
shutil.copy(self.source_path, self.backup_path)
|
|
return True
|
|
return False
|