1 |
#include "jsoc_main.h" |
2 |
#include "drms.h" |
3 |
#include "drms_names.h" |
4 |
#include <unistd.h> |
5 |
#include <strings.h> |
6 |
|
7 |
#define NOTSPECIFIED "***NOTSPECIFIED***" |
8 |
/* List of default parameter values. */ |
9 |
ModuleArgs_t module_args[] = { |
10 |
{ARG_STRING, "ds", NOTSPECIFIED, "", ""}, /* Series to store into */ |
11 |
{ARG_STRING, "in", NOTSPECIFIED, "", ""}, /* File to store from */ |
12 |
{ARG_STRING, "file_name", NOTSPECIFIED, "", ""}, /* Title of file being backed up */ |
13 |
{ARG_STRING, "src_dir", NOTSPECIFIED, "", ""}, /* Title of file being backed up */ |
14 |
{ARG_STRING, "backup_type", NOTSPECIFIED, "", ""}, /* Type of backup */ |
15 |
{ARG_INT, "backup_id", "0", "", ""}, /* backup_id, if appending */ |
16 |
{ARG_INT, "file_id", "0", "", ""}, /* file_id, index starts at 1 */ |
17 |
{ARG_INT, "chunk_id", "0", "", ""}, /* chunk_id, index starts at 1 */ |
18 |
{ARG_INT, "chunk_max", "0", "", ""}, /* chunk_id, index starts at 1 */ |
19 |
{ARG_INT, "chunk_size", "0", "", ""}, /* file size in bytes*/ |
20 |
{ARG_TIME, "backup_date", NOTSPECIFIED, "date backup was taken"}, |
21 |
{ARG_TIME, "date", NOTSPECIFIED, "date backup was put into SUMS"}, |
22 |
{ARG_FLAG, "s", "0", "", ""}, /* start a new backup session (new backup_id) */ |
23 |
{ARG_FLAG, "h", "0", "", ""}, /* print usage message and quit */ |
24 |
{ARG_FLAG, "v", "0", "", ""}, /* verbose flag, normally do not use */ |
25 |
{ARG_END} |
26 |
}; |
27 |
// {ARG_DOUBLE, "backup_date", "0", ""}, /* backup date */ |
28 |
// {ARG_DOUBLE, "date", "0", ""}, /* archive date */ |
29 |
/* Module name presented to DRMS. */ |
30 |
char *module_name = "store_lmbackup_v2"; |
31 |
|
32 |
/* Some global variables for this module. */ |
33 |
int verbose = 0; |
34 |
|
35 |
/* Check DRMS session status and calling arguments and set verbose variable */ |
36 |
int nice_intro () { |
37 |
int usage = cmdparams_get_int (&cmdparams, "h", NULL); |
38 |
verbose = cmdparams_get_int (&cmdparams, "v", NULL); |
39 |
if (usage) { |
40 |
printf ("store_lmbackup ds=<series> in=<file> backup_title=<title> file_name=<name> backup_type=<type> {-h} {-v}\n" |
41 |
" -s: STart new backup\n" |
42 |
" -h: print this message and exit\n" |
43 |
" -v: verbose\n" |
44 |
"ds= - data series to store file into\n" |
45 |
"in= - file to store\n" |
46 |
"sel= - optional description for retrieval key\n"); |
47 |
return (1); |
48 |
} |
49 |
if (verbose) cmdparams_printall (&cmdparams); |
50 |
return (0); |
51 |
} |
52 |
|
53 |
/* Module main function. */ |
54 |
int DoIt (void) { |
55 |
int status = 0; |
56 |
|
57 |
const char *in, *filename; |
58 |
char *series, *note, *sel, *rsp; |
59 |
DRMS_Record_t *rec, *template; |
60 |
int yes_create = cmdparams_get_int (&cmdparams, "c", NULL); |
61 |
|
62 |
|
63 |
int backup_id = 0; |
64 |
int file_id = |
65 |
cmdparams_get_int(&cmdparams, "file_id", NULL); |
66 |
int chunk_id = |
67 |
cmdparams_get_int(&cmdparams, "chunk_id", NULL); |
68 |
int chunk_max = |
69 |
cmdparams_get_int(&cmdparams, "chunk_max", NULL); |
70 |
int chunk_size = |
71 |
cmdparams_get_int(&cmdparams, "chunk_size", NULL); |
72 |
const char* file_name = |
73 |
cmdparams_get_str(&cmdparams, "file_name", NULL); |
74 |
const char* src_dir = |
75 |
cmdparams_get_str(&cmdparams, "src_dir", NULL); |
76 |
const char* backup_type = |
77 |
cmdparams_get_str(&cmdparams, "backup_type", NULL); |
78 |
const char* backup_date = |
79 |
cmdparams_get_str(&cmdparams, "backup_date", &status); |
80 |
const char* date= |
81 |
cmdparams_get_str(&cmdparams, "date", &status); |
82 |
|
83 |
|
84 |
if (nice_intro()) |
85 |
return (0); |
86 |
|
87 |
series = strdup(cmdparams_get_str(&cmdparams, "ds", NULL)); |
88 |
in = cmdparams_get_str(&cmdparams, "in", NULL); |
89 |
if (strcmp(series, NOTSPECIFIED) == 0) |
90 |
{ |
91 |
fprintf(stderr, "'ds' series must be specified. Abort\n"); |
92 |
return(1); |
93 |
} |
94 |
if (strcmp(in, NOTSPECIFIED) == 0) |
95 |
{ |
96 |
fprintf(stderr, "'in' file must be specified. Abort\n"); |
97 |
return(1); |
98 |
} |
99 |
|
100 |
/* First, check to see that the file exists */ |
101 |
if (access(in, R_OK) != 0) |
102 |
{ |
103 |
printf("The requested file can not be accessed. %s\n", in); |
104 |
return(1); |
105 |
} |
106 |
|
107 |
/* Now, extract the filename from the path */ |
108 |
filename = rindex(in, '/'); |
109 |
if (filename) |
110 |
filename++; |
111 |
else |
112 |
filename = in; |
113 |
|
114 |
/* Check to see if series exists */ |
115 |
template = drms_template_record(drms_env, series, &status); |
116 |
if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES) |
117 |
{ |
118 |
printf("Series '%s' does not exist. Give up.\n", series); |
119 |
return(1); |
120 |
} |
121 |
else |
122 |
if(status) |
123 |
{ |
124 |
fprintf(stderr, "DRMS problem looking up series.\n"); |
125 |
return(status); |
126 |
} |
127 |
|
128 |
/* |
129 |
* If new backup session, automatically increment the backup_id |
130 |
* by first querying for it |
131 |
*/ |
132 |
if (cmdparams_get_int(&cmdparams, "s", NULL)) |
133 |
{ |
134 |
DRMS_RecordSet_t *recordset = |
135 |
drms_open_records(drms_env, series, &status); |
136 |
if(!recordset) |
137 |
{ |
138 |
printf("Series '%s' does not exist.\n", series); |
139 |
return(1); |
140 |
} |
141 |
if(recordset->n == 0) |
142 |
{ |
143 |
// printf("Default backup_id to 1\n"); |
144 |
backup_id = 1; |
145 |
} |
146 |
else |
147 |
{ |
148 |
int rec_n, cur_bkid, max_bkid = 0; |
149 |
for(rec_n = 0; rec_n < recordset->n; rec_n++) |
150 |
{ |
151 |
rec = recordset->records[(recordset->n)-1]; |
152 |
cur_bkid = drms_getkey_int(rec, "backup_id", &status); |
153 |
if(status) |
154 |
{ |
155 |
fprintf(stderr, "DRMS Problem getting last backup_id"); |
156 |
return(1); |
157 |
} |
158 |
if(cur_bkid > max_bkid) |
159 |
{ |
160 |
max_bkid = cur_bkid; |
161 |
} |
162 |
} |
163 |
backup_id = max_bkid+1; |
164 |
// printf("Max bkid = %i\n",max_bkid); |
165 |
/* |
166 |
* printf("Reading from record #%i\n", (recordset->n)-1); |
167 |
* rec = recordset->records[(recordset->n)-1]; |
168 |
*/ |
169 |
/* |
170 |
DRMS_Segment_t *seg = drms_segment_lookupnum(rec, 0); |
171 |
char path[DRMS_MAXPATHLEN]; |
172 |
drms_record_directory(rec,path,1); |
173 |
drms_segment_filename(seg, path); |
174 |
printf("Next available backup_id is %i, last seg in %s\n",backup_id, path); |
175 |
*/ |
176 |
} |
177 |
} |
178 |
else |
179 |
{ |
180 |
backup_id = cmdparams_get_int(&cmdparams, "backup_id", NULL); |
181 |
if (backup_id <= 0) |
182 |
{ |
183 |
fprintf(stderr, "Need to specify backup_id when continuing a backup session\n"); |
184 |
return(1); |
185 |
} |
186 |
} |
187 |
printf("%i\n",backup_id); |
188 |
|
189 |
if (file_id == 0) |
190 |
{ |
191 |
fprintf(stderr,"Need to specify file_id\n"); |
192 |
return(1); |
193 |
} |
194 |
if (chunk_id == 0) |
195 |
{ |
196 |
fprintf(stderr,"Need to specify chunk_id\n"); |
197 |
return(1); |
198 |
} |
199 |
if (chunk_max == 0) |
200 |
{ |
201 |
fprintf(stderr,"Need to specify chunk_max\n"); |
202 |
return(1); |
203 |
} |
204 |
if (chunk_size == 0) |
205 |
{ |
206 |
fprintf(stderr,"Need to specify chunk_size\n"); |
207 |
return(1); |
208 |
} |
209 |
if (strcmp(file_name, NOTSPECIFIED) == 0) |
210 |
{ |
211 |
fprintf(stderr,"Need to specify file_name\n"); |
212 |
return(1); |
213 |
} |
214 |
if (strcmp(backup_type, NOTSPECIFIED) == 0) |
215 |
{ |
216 |
fprintf(stderr,"Need to specify backup_type\n"); |
217 |
return(1); |
218 |
} |
219 |
if (strcmp(backup_date, NOTSPECIFIED) == 0) |
220 |
{ |
221 |
fprintf(stderr,"Need to specify valid backup_date\n"); |
222 |
return(1); |
223 |
} |
224 |
if (strcmp(date, NOTSPECIFIED) == 0) |
225 |
{ |
226 |
fprintf(stderr,"Need to specify valid date\n"); |
227 |
return(1); |
228 |
} |
229 |
|
230 |
/* Now ready to make a new record and set keywords */ |
231 |
rec = drms_create_record(drms_env, series, DRMS_PERMANENT, &status); |
232 |
if (!rec || status) |
233 |
{ |
234 |
printf("drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status); |
235 |
return(status); |
236 |
} |
237 |
if ((status = drms_setkey_int(rec, "backup_id", backup_id))) |
238 |
{ |
239 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
240 |
printf("ERROR: series %s does not have a keyword named 'backup_id'\n", series); |
241 |
else |
242 |
printf("ERROR: drms_setkey_int failed for 'backup_id'\n"); |
243 |
return(1); |
244 |
} |
245 |
if ((status = drms_setkey_int(rec, "file_id", file_id))) |
246 |
{ |
247 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
248 |
printf("ERROR: series %s does not have a keyword named 'file_id'\n", series); |
249 |
else |
250 |
printf("ERROR: drms_setkey_int failed for 'file_id'\n"); |
251 |
return(1); |
252 |
} |
253 |
if ((status = drms_setkey_int(rec, "chunk_id", chunk_id))) |
254 |
{ |
255 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
256 |
printf("ERROR: series %s does not have a keyword named 'chunk_id'\n", series); |
257 |
else |
258 |
printf("ERROR: drms_setkey_int failed for 'chunk_id'\n"); |
259 |
return(1); |
260 |
} |
261 |
if ((status = drms_setkey_int(rec, "chunk_max", chunk_max))) |
262 |
{ |
263 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
264 |
printf("ERROR: series %s does not have a keyword named 'chunk_max'\n", series); |
265 |
else |
266 |
printf("ERROR: drms_setkey_int failed for 'chunk_max'\n"); |
267 |
return(1); |
268 |
} |
269 |
if ((status = drms_setkey_int(rec, "chunk_size", chunk_size))) |
270 |
{ |
271 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
272 |
printf("ERROR: series %s does not have a keyword named 'chunk_size'\n", series); |
273 |
else |
274 |
printf("ERROR: drms_setkey_int failed for 'chunk_size'\n"); |
275 |
return(1); |
276 |
} |
277 |
if (strcmp(src_dir, NOTSPECIFIED) == 1) |
278 |
{ |
279 |
if ((status = drms_setkey_string(rec, "src_dir", src_dir))) |
280 |
{ |
281 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
282 |
printf("ERROR: series %s does not have a keyword named 'src_dir'\n", series); |
283 |
else |
284 |
printf("ERROR: drms_setkey_string failed for 'src_dir'\n"); |
285 |
return(1); |
286 |
} |
287 |
} |
288 |
else |
289 |
{ |
290 |
if ((status = drms_setkey_string(rec, "src_dir", " "))) |
291 |
{ |
292 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
293 |
printf("ERROR: series %s does not have a keyword named 'src_dir'\n", series); |
294 |
else |
295 |
printf("ERROR: drms_setkey_string failed for 'src_dir'\n"); |
296 |
return(1); |
297 |
} |
298 |
} |
299 |
if ((status = drms_setkey_string(rec, "file_name", file_name))) |
300 |
{ |
301 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
302 |
printf("ERROR: series %s does not have a keyword named 'file_name'\n", series); |
303 |
else |
304 |
printf("ERROR: drms_setkey_string failed for 'file_name'\n"); |
305 |
return(1); |
306 |
} |
307 |
if ((status = drms_setkey_string(rec, "backup_type", backup_type))) |
308 |
{ |
309 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
310 |
printf("ERROR: series %s does not have a keyword named 'backup_type'\n", series); |
311 |
else |
312 |
printf("ERROR: drms_setkey_string failed for 'backup_type'\n"); |
313 |
return(1); |
314 |
} |
315 |
if ((status = drms_setkey_string(rec, "backup_date", backup_date))) |
316 |
{ |
317 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
318 |
printf("ERROR: series %s does not have a keyword named 'backup_date'\n", series); |
319 |
else |
320 |
printf("ERROR: drms_setkey_double failed for 'backup_date'\n"); |
321 |
return(1); |
322 |
} |
323 |
if ((status = drms_setkey_string(rec, "date", date))) |
324 |
{ |
325 |
if (status == DRMS_ERROR_UNKNOWNKEYWORD) |
326 |
printf("ERROR: series %s does not have a keyword named 'date'\n", series); |
327 |
else |
328 |
printf("ERROR: drms_setkey_double failed for 'date'\n"); |
329 |
return(1); |
330 |
} |
331 |
if ((status = drms_segment_write_from_file(drms_segment_lookup(rec, "file_seg"), (char*)in))) |
332 |
{ |
333 |
printf("drms_segment_write_file failed, status=%d\n",status); |
334 |
return(status); |
335 |
} |
336 |
|
337 |
if ((status = drms_close_record(rec, DRMS_INSERT_RECORD))) |
338 |
printf("drms_close_record failed!\n"); |
339 |
|
340 |
return(status); |
341 |
} |