1 /***********************************************************************************************************************************
2 Backup Info Handler
3 ***********************************************************************************************************************************/
4 #ifndef INFO_INFOBACKUP_H
5 #define INFO_INFOBACKUP_H
6 
7 #include "common/type/stringId.h"
8 
9 /***********************************************************************************************************************************
10 Object type
11 ***********************************************************************************************************************************/
12 typedef struct InfoBackup InfoBackup;
13 
14 /***********************************************************************************************************************************
15 Backup type enum
16 ***********************************************************************************************************************************/
17 typedef enum
18 {
19     backupTypeFull = STRID5("full", 0x632a60),
20     backupTypeDiff = STRID5("diff", 0x319240),
21     backupTypeIncr = STRID5("incr", 0x90dc90),
22 } BackupType;
23 
24 #include "common/type/object.h"
25 #include "common/type/string.h"
26 #include "common/type/stringList.h"
27 #include "info/infoPg.h"
28 #include "info/manifest.h"
29 #include "storage/storage.h"
30 
31 /***********************************************************************************************************************************
32 Constants
33 ***********************************************************************************************************************************/
34 #define INFO_BACKUP_FILE                                            "backup.info"
35 
36 #define INFO_BACKUP_PATH_FILE                                       STORAGE_REPO_BACKUP "/" INFO_BACKUP_FILE
37     STRING_DECLARE(INFO_BACKUP_PATH_FILE_STR);
38 #define INFO_BACKUP_PATH_FILE_COPY                                  INFO_BACKUP_PATH_FILE INFO_COPY_EXT
39     STRING_DECLARE(INFO_BACKUP_PATH_FILE_COPY_STR);
40 
41 /***********************************************************************************************************************************
42 Information about an existing backup
43 ***********************************************************************************************************************************/
44 typedef struct InfoBackupData
45 {
46     const String *backupLabel;                                      // backupLabel must be first to allow for built-in list sorting
47     unsigned int backrestFormat;
48     const String *backrestVersion;
49     const String *backupArchiveStart;
50     const String *backupArchiveStop;
51     uint64_t backupInfoRepoSize;
52     uint64_t backupInfoRepoSizeDelta;
53     uint64_t backupInfoSize;
54     uint64_t backupInfoSizeDelta;
55     unsigned int backupPgId;
56     const String *backupPrior;
57     StringList *backupReference;
58     time_t backupTimestampStart;
59     time_t backupTimestampStop;
60     BackupType backupType;
61     bool optionArchiveCheck;
62     bool optionArchiveCopy;
63     bool optionBackupStandby;
64     bool optionChecksumPage;
65     bool optionCompress;
66     bool optionHardlink;
67     bool optionOnline;
68 } InfoBackupData;
69 
70 /***********************************************************************************************************************************
71 Constructors
72 ***********************************************************************************************************************************/
73 InfoBackup *infoBackupNew(unsigned int pgVersion, uint64_t pgSystemId, unsigned int pgCatalogVersion, const String *cipherPassSub);
74 
75 // Create new object and load contents from IoRead
76 InfoBackup *infoBackupNewLoad(IoRead *read);
77 
78 /***********************************************************************************************************************************
79 Getters/Setters
80 ***********************************************************************************************************************************/
81 typedef struct InfoBackupPub
82 {
83     MemContext *memContext;                                         // Mem context
84     InfoPg *infoPg;                                                 // Contents of the DB data
85     List *backup;                                                   // List of current backups and their associated data
86 } InfoBackupPub;
87 
88 // PostgreSQL info
89 __attribute__((always_inline)) static inline InfoPg *
infoBackupPg(const InfoBackup * const this)90 infoBackupPg(const InfoBackup *const this)
91 {
92     return THIS_PUB(InfoBackup)->infoPg;
93 }
94 
95 InfoBackup *infoBackupPgSet(InfoBackup *this, unsigned int pgVersion, uint64_t pgSystemId, unsigned int pgCatalogVersion);
96 
97 // Cipher passphrase
98 __attribute__((always_inline)) static inline const String *
infoBackupCipherPass(const InfoBackup * const this)99 infoBackupCipherPass(const InfoBackup *const this)
100 {
101     return infoPgCipherPass(infoBackupPg(this));
102 }
103 
104 // Return a structure of the backup data from a specific index
105 InfoBackupData infoBackupData(const InfoBackup *this, unsigned int backupDataIdx);
106 
107 // Return a pointer to a structure from the current backup data given a label, else NULL
108 __attribute__((always_inline)) static inline InfoBackupData *
infoBackupDataByLabel(const InfoBackup * const this,const String * const backupLabel)109 infoBackupDataByLabel(const InfoBackup *const this, const String *const backupLabel)
110 {
111     ASSERT_INLINE(backupLabel != NULL);
112     return lstFind(THIS_PUB(InfoBackup)->backup, &backupLabel);
113 }
114 
115 // Get total current backups
116 __attribute__((always_inline)) static inline unsigned int
infoBackupDataTotal(const InfoBackup * const this)117 infoBackupDataTotal(const InfoBackup *const this)
118 {
119     return lstSize(THIS_PUB(InfoBackup)->backup);
120 }
121 
122 /***********************************************************************************************************************************
123 Functions
124 ***********************************************************************************************************************************/
125 // Add backup to the current list
126 void infoBackupDataAdd(const InfoBackup *this, const Manifest *manifest);
127 
128 // Delete backup from the current backup list
129 void infoBackupDataDelete(const InfoBackup *this, const String *backupDeleteLabel);
130 
131 // Given a backup label, get the dependency list
132 StringList *infoBackupDataDependentList(const InfoBackup *this, const String *backupLabel);
133 
134 // Return a list of current backup labels, applying a regex expression if provided
135 StringList *infoBackupDataLabelList(const InfoBackup *this, const String *expression);
136 
137 // Move to a new parent mem context
138 __attribute__((always_inline)) static inline InfoBackup *
infoBackupMove(InfoBackup * const this,MemContext * const parentNew)139 infoBackupMove(InfoBackup *const this, MemContext *const parentNew)
140 {
141     return objMove(this, parentNew);
142 }
143 
144 /***********************************************************************************************************************************
145 Destructor
146 ***********************************************************************************************************************************/
147 __attribute__((always_inline)) static inline void
infoBackupFree(InfoBackup * const this)148 infoBackupFree(InfoBackup *const this)
149 {
150     objFree(this);
151 }
152 
153 /***********************************************************************************************************************************
154 Helper functions
155 ***********************************************************************************************************************************/
156 // Load backup info
157 InfoBackup *infoBackupLoadFile(
158     const Storage *storage, const String *fileName, CipherType cipherType, const String *cipherPass);
159 
160 // Load backup info and update it by adding valid backups from the repo or removing backups no longer in the repo
161 InfoBackup *infoBackupLoadFileReconstruct(
162     const Storage *storage, const String *fileName, CipherType cipherType, const String *cipherPass);
163 
164 // Save backup info
165 void infoBackupSaveFile(
166     InfoBackup *infoBackup, const Storage *storage, const String *fileName, CipherType cipherType, const String *cipherPass);
167 
168 /***********************************************************************************************************************************
169 Macros for function logging
170 ***********************************************************************************************************************************/
171 String *infoBackupDataToLog(const InfoBackupData *this);
172 
173 #define FUNCTION_LOG_INFO_BACKUP_TYPE                                                                                              \
174     InfoBackup *
175 #define FUNCTION_LOG_INFO_BACKUP_FORMAT(value, buffer, bufferSize)                                                                 \
176     objToLog(value, "InfoBackup", buffer, bufferSize)
177 #define FUNCTION_LOG_INFO_BACKUP_DATA_TYPE                                                                                         \
178     InfoBackupData
179 #define FUNCTION_LOG_INFO_BACKUP_DATA_FORMAT(value, buffer, bufferSize)                                                            \
180     FUNCTION_LOG_STRING_OBJECT_FORMAT(&value, infoBackupDataToLog, buffer, bufferSize)
181 
182 #endif
183