-
Notifications
You must be signed in to change notification settings - Fork 0
/
recm_restoremeta.h
293 lines (267 loc) · 12.7 KB
/
recm_restoremeta.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**********************************************************************************/
/* RESTORE META command */
/* Usage: */
/* restore meta */
/* Available Option(s): */
/* /overwrite Overwrite existing file */
/* /verbose */
/* Available Qualifier(s): */
/* /cid=<STRING> use a backup from a different cluster */
/* /uid=<STRING> use a specified backup */
/* /file=<FILE> Select file to restore */
/* /directory=<PATH> Destination folder */
/* /tag=<STRING> Search for a backup by it's TAG */
/**********************************************************************************/
/*
@command restore meta
@definition
Restore META file(s) backuped by 'backup meta' command.
The '/directory' allow you to restore files onto a different folder.
(See {&backup meta&} command for more details)
The restore perform a restoration of flat files. The restored files are never applied onto the current connected cluster.
@option "/overwrite" "Overwrite existing file"
@option "/verbose" "Display more details"
@option "/cid=STRING" "use a backup from a different cluster"
@option "/uid=STRING" "use a specified backup"
@option "/file=FILE" "Select file to restore"
@option "/directory=PATH" "Destination folder"
@option "/before=DATE" "Find a backup with a given date before"
@option "/after=DATE" "Find a backup with a given date after "
@option "/tag=STRING" "Search for a backup by it's TAG"
@example
@inrecm {&list backup&}/meta
@out List backups of cluster 'CLU12' (CID=1)
@out UID Date Type Status TL Size Tag
@out ---------------------- -------------------- ------ ------------ ---- ---------- -------------------------
@out 0001634eecec1f4449a8 2022-10-18 20:14:05 META AVAILABLE 36 48 KB 20221018_CLU12_META
@out
@inrecm {&show backup&} /uid=0001634eecec1f4449a8 /files /piece=1
@out Backup 'META' UID:'0001634eecec1f4449a8' [AVAILABLE] Cluster:'CLU12' (CID=1)
@out Backup Pieces
@out 1) 0001634eecec1f4449a8_1_META.recm 6 files [49843 byte(s)]
@out Piece '/Volumes/pg_backups//0001634eecec1f4449a8_1_META.recm' Content:
@out mdate Size File name
@out -------------------- ---------- ----------------------------------------
@out 1] 2022-10-18 20:14:04 6489 ./recm_meta/metadata_CLU12_demo.sql
@out 2] 2022-10-18 20:14:04 8600 ./recm_meta/metadata_CLU12_depo_prod.sql
@out 3] 2022-10-18 20:14:04 12983 ./recm_meta/metadata_CLU12_limkydb.sql
@out 4] 2022-10-18 20:14:04 7448 ./recm_meta/metadata_CLU12_recm_db2.sql
@out 5] 2022-10-18 20:14:04 8348 ./recm_meta/metadata_CLU12_recmdb.sql
@out 6] 2022-10-18 20:14:04 5975 ./recm_meta/metadata_CLU12_recmrepo.sql
@out
@inrecm {&restore meta&} /dir="/tmp" /file="metadata_CLU12_recmdb.sql" /verbose
@out recm-inf: Found backup UID '0001634eecec1f4449a8', piece '/Volumes/pg_backups//0001634eecec1f4449a8_1_META.recm' of date '2022-10-18 20:14:05'
@out recm-inf: Restoring file '/tmp/metadata_CLU12_recmdb.sql'.
@out
@inrecm
@end
*/
void COMMAND_RESTOREMETA(int idcmd,char *command_line)
{
long cluster_id=0;
// zip_t *zipHandle;
if (DEPOisConnected(true) == false) return;
memBeginModule();
char *SRCDIR=memAlloc(1024);
if (isConnectedAndRegistered() == false) return;
if (varExist(GVAR_BACKUPDIR) == false)
{
ERROR(ERR_NOBACKUPDIR,"Backup directory not set.(See show cluster).\n");
memEndModule();
return;
}
if (directory_exist(varGet(GVAR_BACKUPDIR)) == false)
{
mkdirs(varGet(GVAR_BACKUPDIR),maskTranslate(varGet(GVAR_CLUMASK_BACKUP)));
}
if (directory_exist(varGet(GVAR_BACKUPDIR)) == false)
{
ERROR(ERR_INVDIR,"Invalid Backup directory '%s'\n",varGet(GVAR_BACKUPDIR));
memEndModule();
return;
};
char *query=memAlloc(1024);
char *cluster_name=memAlloc(128);
if (varGet(GVAR_CLUCID) != NULL && strcmp(varGet(GVAR_CLUCID),VAR_UNSET_VALUE) != 0) cluster_id=varGetLong(GVAR_CLUCID);
if (qualifierIsUNSET("qal_cid") == false)
{
sprintf(query,"select count(*) from %s.clusters where cid=%s",
varGet(GVAR_DEPUSER),
varGet("qal_cid"));
int rows=DEPOquery(query,0);
if (DEPOgetLong(0,0) == 0)
{
ERROR(ERR_BADCLUNAME,"Unknown cluster ID %s\n",varGet("qal_cid"));
DEPOqueryEnd();
memEndModule();
return;
}
DEPOqueryEnd();
sprintf(query,"select clu_nam from %s.clusters where cid=%s",
varGet(GVAR_DEPUSER),
varGet("qal_cid"));
rows=DEPOquery(query,0);
strcpy(cluster_name,DEPOgetString(0,0));
cluster_id=varGetInt("qal_cid");
DEPOqueryEnd();
}
else
{
strcpy(cluster_name,varGet(GVAR_CLUNAME));
}
char *destination_folder=memAlloc(1024);
if (qualifierIsUNSET("qal_directory") == false)
{
strcpy(destination_folder,varGet("qal_directory"));
}
else
{
/* CHANGE : 0xA0004 Check if RECM_META_DIR exist */
char *RECM_META_DIR=getenv(ENVIRONMENT_RECM_META_DIR); // Check if environment variable RECM_META_DIR exist
if (RECM_META_DIR != NULL) { strcpy(destination_folder,RECM_META_DIR); }
else { sprintf(destination_folder,"%s/recm_meta",varGet(GVAR_CLUPGDATA)); } // By default, METADATA are generated in subdirectory 'recm_meta'
};
if (directory_exist(destination_folder) == false)
{
mkdirs(destination_folder,maskTranslate(varGet(GVAR_CLUMASK_BACKUP) )); // Create directory if it does not exist
};
if (directory_exist(destination_folder) == false) // Abort process if could not create diectory
{
ERROR(ERR_INVMETADIR,"Invalid META data directory '%s'\n",destination_folder);
memEndModule();
return;
};
TRACE("destination_folder=%s\n",destination_folder);
long datablock_size=getHumanSize(GVAR_CLUREADBLOCKSIZE); // Allocate a buffer. Size is coming from variable 'GVAR_CLUREADBLOCKSIZE'
if (datablock_size < 1024) datablock_size=163840;
char *datablock=memAlloc(datablock_size);
if (optionIsSET("opt_verbose") == true) globalArgs.verbosity=true; // Set Verbosity
char *qry_id=memAlloc(128); qry_id[0]=0x00;
char *qry_file=memAlloc(128); qry_file[0]=0x00;
char *qry_tag=memAlloc(128); qry_tag[0]=0x00;
char *outfile=memAlloc(1024); outfile[0]=0x00;
if (qualifierIsUNSET("qal_uid") == false)
{ sprintf(qry_id," and b.bck_id='%s'",varGet("qal_uid")); };
if (qualifierIsUNSET("qal_tag") == false)
{ sprintf(qry_tag," and b.bcktag like '%%%s%%'",varGet("qal_tag")); };
sprintf(query,"select bp.bck_id,bp.pname,coalesce(to_char(b.bdate,'%s'),'0000-00-00 00:00:00'),b.bckdir"
" from %s.backup_pieces bp,%s.backups b"
" where bp.bck_id=b.bck_id and bp.cid=b.cid and b.cid=%ld and b.bcksts=0 and b.bcktyp='META'",
varGet(GVAR_CLUDATE_FORMAT),
varGet(GVAR_DEPUSER),
varGet(GVAR_DEPUSER),
cluster_id);
strcat(query,qry_id);
strcat(query,qry_tag);
strcat(query," order by b.edate desc limit 1");
TRACE("query=%s\n",query);
char *bck_id=memAlloc(128); bck_id[0]=0x00;
char *pname=memAlloc(1024); pname[0]=0x00;
char *bck_dat=memAlloc(128); bck_dat[0]=0x00;
int wal_rows=DEPOquery(query,1);
if (wal_rows == 0)
{
INFO("No META backup found.\n");
memEndModule();
return;
}
for(int i=0;i < wal_rows;i++)
{
strcpy(bck_id,DEPOgetString(i,0)); // UID
strcpy(bck_dat,DEPOgetString(i,2)); // date
if (varExist(SOURCE_DIRECTORY) == true) { strcpy(SRCDIR,varGet(SOURCE_DIRECTORY)); } // different SOURCE
else { strcpy(SRCDIR,DEPOgetString(i,3));}; // Original Path
sprintf(pname,"%s/%s",SRCDIR,DEPOgetString(i,1));
VERBOSE("Found backup UID '%s', piece '%s' of date '%s'\n",bck_id,pname,bck_dat);
RECMDataFile *hDF=RECMopenRead(pname);
if (hDF == NULL)
{
ERROR(ERR_INVRECMFIL,"Corrupted or invalid RECM file '%s'\n",pname);
memEndModule();
return;
}
else
{
struct zip_stat sb;
struct zip_file *zf;
int file_index=0;
while( file_index < zip_get_num_entries(hDF->zipHandle, 0))
{
if (zip_stat_index(hDF->zipHandle, file_index, 0, &sb) == 0)
{
char *filnam=extractFileName(sb.name);
sprintf(outfile,"%s/%s",destination_folder,filnam);
TRACE("filnam='%s' (%s)\n",filnam,sb.name);
int accept=false;
if (qualifierIsUNSET("qal_file") == false)
{
char *qal_file=extractFileName(varGet("qal_file"));
TRACE("compare '%s' == '%s'\n",filnam,qal_file);
if (strcmp(filnam,qal_file) == 0) {accept++; };
}
else { accept=true; };
int flg_overwrite=0;
if (accept ==true && file_exists(outfile) == true)
{
if (optionIsSET("opt_overwrite") == true)
{
unlink(outfile);
flg_overwrite++;
}
else
{
WARN(WRN_FILEPRESENT,"File '%s' already present on disk.\n",outfile);
accept=false;
};
};
if (accept == true)
{
RecmFileItemProperties *fileprop=RECMGetFileProperties(hDF,file_index);
if (flg_overwrite == 0) { VERBOSE("Restoring file '%s'.\n",outfile); }
else { VERBOSE("Restore file '%s' (overwrite by request).\n",outfile); };
zf = zip_fopen_index(hDF->zipHandle, file_index, 0);
if (!zf)
{
ERROR(ERR_BADPIECE, "Cannot process piece '%s': %s\n",pname,zip_strerror(hDF->zipHandle));
memEndModule();
return;
}
int fd = open(outfile, O_RDWR | O_TRUNC | O_CREAT, fileprop->mode);
if (fd < 0)
{
ERROR(ERR_CREATEFILE, "Cannot create file '%s': %s\n",outfile,strerror(errno));
memEndModule();
return;
}
long long sum = 0;
while (sum != sb.size)
{
int len = zip_fread(zf, datablock, datablock_size);
if (len < 0)
{
ERROR(ERR_BADPIECE, "Cannot process piece '%s'\n",pname);
memEndModule();
return;
}
write(fd, datablock, len);
sum += len;
}
close(fd);
// Set file attributes like it was before to added into ZIP file
struct utimbuf ut;
ut.actime=sb.mtime;
ut.modtime=sb.mtime;
utime(outfile, &ut);
//chmod(outfile,fileprop->mode);
chown(outfile,fileprop->uid,fileprop->gid);
zip_fclose(zf);
}
}
file_index++;
}
}
RECMcloseRead(hDF);
}
DEPOqueryEnd();
memEndModule();
}