00001 #include "jsoc_main.h"
00002 #include "drms.h"
00003 #include "drms_names.h"
00004 #include <unistd.h>
00005 #include <strings.h>
00006
00007 #define NOTSPECIFIED "***NOTSPECIFIED***"
00008
00009 ModuleArgs_t module_args[] = {
00010 {ARG_STRING, "ds", NOTSPECIFIED, "", ""},
00011 {ARG_STRING, "in", NOTSPECIFIED, "", ""},
00012 {ARG_STRING, "file_name", NOTSPECIFIED, "", ""},
00013 {ARG_STRING, "src_dir", NOTSPECIFIED, "", ""},
00014 {ARG_STRING, "backup_type", NOTSPECIFIED, "", ""},
00015 {ARG_INT, "backup_id", "0", "", ""},
00016 {ARG_INT, "file_id", "0", "", ""},
00017 {ARG_INT, "chunk_id", "0", "", ""},
00018 {ARG_INT, "chunk_max", "0", "", ""},
00019 {ARG_INT, "chunk_size", "0", "", ""},
00020 {ARG_TIME, "backup_date", NOTSPECIFIED, "date backup was taken"},
00021 {ARG_TIME, "date", NOTSPECIFIED, "date backup was put into SUMS"},
00022 {ARG_FLAG, "s", "0", "", ""},
00023 {ARG_FLAG, "h", "0", "", ""},
00024 {ARG_FLAG, "v", "0", "", ""},
00025 {ARG_END}
00026 };
00027
00028
00029
00030 char *module_name = "store_lmbackup_v2";
00031
00032
00033 int verbose = 0;
00034
00035
00036 int nice_intro () {
00037 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00038 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00039 if (usage) {
00040 printf ("store_lmbackup ds=<series> in=<file> backup_title=<title> file_name=<name> backup_type=<type> {-h} {-v}\n"
00041 " -s: STart new backup\n"
00042 " -h: print this message and exit\n"
00043 " -v: verbose\n"
00044 "ds= - data series to store file into\n"
00045 "in= - file to store\n"
00046 "sel= - optional description for retrieval key\n");
00047 return (1);
00048 }
00049 if (verbose) cmdparams_printall (&cmdparams);
00050 return (0);
00051 }
00052
00053
00054 int DoIt (void) {
00055 int status = 0;
00056
00057 const char *in, *filename;
00058 char *series, *note, *sel, *rsp;
00059 DRMS_Record_t *rec, *template;
00060 int yes_create = cmdparams_get_int (&cmdparams, "c", NULL);
00061
00062
00063 int backup_id = 0;
00064 int file_id =
00065 cmdparams_get_int(&cmdparams, "file_id", NULL);
00066 int chunk_id =
00067 cmdparams_get_int(&cmdparams, "chunk_id", NULL);
00068 int chunk_max =
00069 cmdparams_get_int(&cmdparams, "chunk_max", NULL);
00070 int chunk_size =
00071 cmdparams_get_int(&cmdparams, "chunk_size", NULL);
00072 const char* file_name =
00073 cmdparams_get_str(&cmdparams, "file_name", NULL);
00074 const char* src_dir =
00075 cmdparams_get_str(&cmdparams, "src_dir", NULL);
00076 const char* backup_type =
00077 cmdparams_get_str(&cmdparams, "backup_type", NULL);
00078 const char* backup_date =
00079 cmdparams_get_str(&cmdparams, "backup_date", &status);
00080 const char* date=
00081 cmdparams_get_str(&cmdparams, "date", &status);
00082
00083
00084 if (nice_intro())
00085 return (0);
00086
00087 series = strdup(cmdparams_get_str(&cmdparams, "ds", NULL));
00088 in = cmdparams_get_str(&cmdparams, "in", NULL);
00089 if (strcmp(series, NOTSPECIFIED) == 0)
00090 {
00091 fprintf(stderr, "'ds' series must be specified. Abort\n");
00092 return(1);
00093 }
00094 if (strcmp(in, NOTSPECIFIED) == 0)
00095 {
00096 fprintf(stderr, "'in' file must be specified. Abort\n");
00097 return(1);
00098 }
00099
00100
00101 if (access(in, R_OK) != 0)
00102 {
00103 printf("The requested file can not be accessed. %s\n", in);
00104 return(1);
00105 }
00106
00107
00108 filename = rindex(in, '/');
00109 if (filename)
00110 filename++;
00111 else
00112 filename = in;
00113
00114
00115 template = drms_template_record(drms_env, series, &status);
00116 if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES)
00117 {
00118 printf("Series '%s' does not exist. Give up.\n", series);
00119 return(1);
00120 }
00121 else
00122 if(status)
00123 {
00124 fprintf(stderr, "DRMS problem looking up series.\n");
00125 return(status);
00126 }
00127
00128
00129
00130
00131
00132 if (cmdparams_get_int(&cmdparams, "s", NULL))
00133 {
00134 DRMS_RecordSet_t *recordset =
00135 drms_open_records(drms_env, series, &status);
00136 if(!recordset)
00137 {
00138 printf("Series '%s' does not exist.\n", series);
00139 return(1);
00140 }
00141 if(recordset->n == 0)
00142 {
00143
00144 backup_id = 1;
00145 }
00146 else
00147 {
00148 int rec_n, cur_bkid, max_bkid = 0;
00149 for(rec_n = 0; rec_n < recordset->n; rec_n++)
00150 {
00151 rec = recordset->records[(recordset->n)-1];
00152 cur_bkid = drms_getkey_int(rec, "backup_id", &status);
00153 if(status)
00154 {
00155 fprintf(stderr, "DRMS Problem getting last backup_id");
00156 return(1);
00157 }
00158 if(cur_bkid > max_bkid)
00159 {
00160 max_bkid = cur_bkid;
00161 }
00162 }
00163 backup_id = max_bkid+1;
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 }
00177 }
00178 else
00179 {
00180 backup_id = cmdparams_get_int(&cmdparams, "backup_id", NULL);
00181 if (backup_id <= 0)
00182 {
00183 fprintf(stderr, "Need to specify backup_id when continuing a backup session\n");
00184 return(1);
00185 }
00186 }
00187 printf("%i\n",backup_id);
00188
00189 if (file_id == 0)
00190 {
00191 fprintf(stderr,"Need to specify file_id\n");
00192 return(1);
00193 }
00194 if (chunk_id == 0)
00195 {
00196 fprintf(stderr,"Need to specify chunk_id\n");
00197 return(1);
00198 }
00199 if (chunk_max == 0)
00200 {
00201 fprintf(stderr,"Need to specify chunk_max\n");
00202 return(1);
00203 }
00204 if (chunk_size == 0)
00205 {
00206 fprintf(stderr,"Need to specify chunk_size\n");
00207 return(1);
00208 }
00209 if (strcmp(file_name, NOTSPECIFIED) == 0)
00210 {
00211 fprintf(stderr,"Need to specify file_name\n");
00212 return(1);
00213 }
00214 if (strcmp(backup_type, NOTSPECIFIED) == 0)
00215 {
00216 fprintf(stderr,"Need to specify backup_type\n");
00217 return(1);
00218 }
00219 if (strcmp(backup_date, NOTSPECIFIED) == 0)
00220 {
00221 fprintf(stderr,"Need to specify valid backup_date\n");
00222 return(1);
00223 }
00224 if (strcmp(date, NOTSPECIFIED) == 0)
00225 {
00226 fprintf(stderr,"Need to specify valid date\n");
00227 return(1);
00228 }
00229
00230
00231 rec = drms_create_record(drms_env, series, DRMS_PERMANENT, &status);
00232 if (!rec || status)
00233 {
00234 printf("drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status);
00235 return(status);
00236 }
00237 if ((status = drms_setkey_int(rec, "backup_id", backup_id)))
00238 {
00239 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00240 printf("ERROR: series %s does not have a keyword named 'backup_id'\n", series);
00241 else
00242 printf("ERROR: drms_setkey_int failed for 'backup_id'\n");
00243 return(1);
00244 }
00245 if ((status = drms_setkey_int(rec, "file_id", file_id)))
00246 {
00247 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00248 printf("ERROR: series %s does not have a keyword named 'file_id'\n", series);
00249 else
00250 printf("ERROR: drms_setkey_int failed for 'file_id'\n");
00251 return(1);
00252 }
00253 if ((status = drms_setkey_int(rec, "chunk_id", chunk_id)))
00254 {
00255 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00256 printf("ERROR: series %s does not have a keyword named 'chunk_id'\n", series);
00257 else
00258 printf("ERROR: drms_setkey_int failed for 'chunk_id'\n");
00259 return(1);
00260 }
00261 if ((status = drms_setkey_int(rec, "chunk_max", chunk_max)))
00262 {
00263 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00264 printf("ERROR: series %s does not have a keyword named 'chunk_max'\n", series);
00265 else
00266 printf("ERROR: drms_setkey_int failed for 'chunk_max'\n");
00267 return(1);
00268 }
00269 if ((status = drms_setkey_int(rec, "chunk_size", chunk_size)))
00270 {
00271 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00272 printf("ERROR: series %s does not have a keyword named 'chunk_size'\n", series);
00273 else
00274 printf("ERROR: drms_setkey_int failed for 'chunk_size'\n");
00275 return(1);
00276 }
00277 if (strcmp(src_dir, NOTSPECIFIED) == 1)
00278 {
00279 if ((status = drms_setkey_string(rec, "src_dir", src_dir)))
00280 {
00281 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00282 printf("ERROR: series %s does not have a keyword named 'src_dir'\n", series);
00283 else
00284 printf("ERROR: drms_setkey_string failed for 'src_dir'\n");
00285 return(1);
00286 }
00287 }
00288 else
00289 {
00290 if ((status = drms_setkey_string(rec, "src_dir", " ")))
00291 {
00292 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00293 printf("ERROR: series %s does not have a keyword named 'src_dir'\n", series);
00294 else
00295 printf("ERROR: drms_setkey_string failed for 'src_dir'\n");
00296 return(1);
00297 }
00298 }
00299 if ((status = drms_setkey_string(rec, "file_name", file_name)))
00300 {
00301 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00302 printf("ERROR: series %s does not have a keyword named 'file_name'\n", series);
00303 else
00304 printf("ERROR: drms_setkey_string failed for 'file_name'\n");
00305 return(1);
00306 }
00307 if ((status = drms_setkey_string(rec, "backup_type", backup_type)))
00308 {
00309 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00310 printf("ERROR: series %s does not have a keyword named 'backup_type'\n", series);
00311 else
00312 printf("ERROR: drms_setkey_string failed for 'backup_type'\n");
00313 return(1);
00314 }
00315 if ((status = drms_setkey_string(rec, "backup_date", backup_date)))
00316 {
00317 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00318 printf("ERROR: series %s does not have a keyword named 'backup_date'\n", series);
00319 else
00320 printf("ERROR: drms_setkey_double failed for 'backup_date'\n");
00321 return(1);
00322 }
00323 if ((status = drms_setkey_string(rec, "date", date)))
00324 {
00325 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00326 printf("ERROR: series %s does not have a keyword named 'date'\n", series);
00327 else
00328 printf("ERROR: drms_setkey_double failed for 'date'\n");
00329 return(1);
00330 }
00331 if ((status = drms_segment_write_from_file(drms_segment_lookup(rec, "file_seg"), (char*)in)))
00332 {
00333 printf("drms_segment_write_file failed, status=%d\n",status);
00334 return(status);
00335 }
00336
00337 if ((status = drms_close_record(rec, DRMS_INSERT_RECORD)))
00338 printf("drms_close_record failed!\n");
00339
00340 return(status);
00341 }