File size: 2,604 Bytes
83c8d2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Backup_Functionality.py
# Functionality for exporting items as markdown files
#
# Imports:
import os
import shutil
import gradio as gr
#
# Local Imports:
from App_Function_Libraries.DB.DB_Manager import create_automated_backup, db_path, backup_dir
#
# End of Imports
#######################################################################################################################
#
# Functions:

def create_backup():
    backup_file = create_automated_backup(db_path, backup_dir)
    return f"Backup created: {backup_file}"


def list_backups():
    backups = [f for f in os.listdir(backup_dir) if f.endswith('.db')]
    return "\n".join(backups)


def restore_backup(backup_name: str) -> str:
    backup_path_location: str = os.path.join(str(backup_dir), backup_name)
    if os.path.exists(backup_path_location):
        shutil.copy2(str(backup_path_location), str(db_path))
        return f"Database restored from {backup_name}"
    else:
        return "Backup file not found"


def create_backup_tab():
    with gr.Tab("Create Backup"):
        gr.Markdown("# Create a backup of the database")
        gr.Markdown("This will create a backup of the database in the backup directory(the default backup directory is `/tldw_DB_Backups/')")
        with gr.Row():
            with gr.Column():
                create_button = gr.Button("Create Backup")
                create_output = gr.Textbox(label="Result")
            with gr.Column():
                create_button.click(create_backup, inputs=[], outputs=create_output)


def create_view_backups_tab():
    with gr.TabItem("View Backups"):
        gr.Markdown("# Browse available backups")
        with gr.Row():
            with gr.Column():
                view_button = gr.Button("View Backups")
            with gr.Column():
                backup_list = gr.Textbox(label="Available Backups")
                view_button.click(list_backups, inputs=[], outputs=backup_list)


def create_restore_backup_tab():
    with gr.TabItem("Restore Backup"):
        gr.Markdown("# Restore a backup of the database")
        with gr.Column():
            backup_input = gr.Textbox(label="Backup Filename")
            restore_button = gr.Button("Restore")
        with gr.Column():
            restore_output = gr.Textbox(label="Result")
            restore_button.click(restore_backup, inputs=[backup_input], outputs=restore_output)

#
# End of Functions
#######################################################################################################################