Issues (23)

backuppc_clone/helper/BackupDelete.py (1 issue)

1
import os
2
3
from backuppc_clone.Config import Config
4
from backuppc_clone.DataLayer import DataLayer
5
from backuppc_clone.style.BackupPcCloneStyle import BackupPcCloneStyle
6
7
8 View Code Duplication
class BackupDelete:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
9
    """
10
    Deletes a backup of a host
11
    """
12
13
    # ------------------------------------------------------------------------------------------------------------------
14
    def __init__(self, io: BackupPcCloneStyle):
15
        """
16
        Object constructor.
17
18
        @param BackupPcCloneStyle io: The output style.
19
        """
20
21
        self.__io: BackupPcCloneStyle = io
22
        """
23
        The output style.
24
        """
25
26
        self.__host: str = ''
27
        """
28
        The host of the backup.
29
        """
30
31
        self.__backup_no: int = 0
32
        """
33
        The number of the backup.
34
        """
35
36
    # ------------------------------------------------------------------------------------------------------------------
37
    def __delete_files(self) -> None:
38
        """
39
        Removes the backup from the cone file system.
40
        """
41
        self.__io.writeln(' Removing files')
42
43
        backup_dir_clone = Config.instance.backup_dir_clone(self.__host, self.__backup_no)
44
        if os.path.isdir(backup_dir_clone):
45
            os.system('rm -fr "%s"' % backup_dir_clone)
46
47
    # ------------------------------------------------------------------------------------------------------------------
48
    def __delete_metadata(self) -> None:
49
        """
50
        Removes the metadata from the database.
51
        """
52
        self.__io.writeln(' Removing metadata')
53
54
        hst_id = DataLayer.instance.get_host_id(self.__host)
55
        bck_id = DataLayer.instance.get_bck_id(hst_id, int(self.__backup_no))
56
57
        DataLayer.instance.backup_delete(bck_id)
58
        DataLayer.instance.commit()
59
60
    # ------------------------------------------------------------------------------------------------------------------
61
    def delete_backup(self, host: str, backup_no: int) -> None:
62
        """
63
        Deletes a backup of a host.
64
65
        @param str host: The host of the backup.
66
        @param int backup_no: The number of the backup.
67
        """
68
        self.__host = host
69
        self.__backup_no = backup_no
70
71
        self.__delete_metadata()
72
        self.__delete_files()
73
74
# ----------------------------------------------------------------------------------------------------------------------
75