/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following functions.
- process_arguments
- mark_pkey_position
- export_archive
- get_begin_time
- main
1 /***************************************
2 $Revision: 1.1 $
3
4 Backup. backup.c - whois archive DB backup.
5
6 Status: NOT REVIEWED, NOT TESTED, NOT COMPLETE
7
8 Implementation by: Tiago Antao
9
10 ******************/ /******************
11 Copyright (c) 2002 RIPE NCC
12
13 All Rights Reserved
14
15 Permission to use, copy, modify, and distribute this software and its
16 documentation for any purpose and without fee is hereby granted,
17 provided that the above copyright notice appear in all copies and that
18 both that copyright notice and this permission notice appear in
19 supporting documentation, and that the name of the author not be
20 used in advertising or publicity pertaining to distribution of the
21 software without specific, written prior permission.
22
23 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
25 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
26 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 ***************************************/
30
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <rip.h>
36 #include "miniconf.h"
37 #include "dbsupport.h"
38 #include "aconf.h"
39
40 long begin_time = 0; //begin_time is really begin_time-1
41
42 /*
43 process_arguments: processes command-line arguments.
44 */
45 void process_arguments(int argv, char** argc) {
/* [<][>][^][v][top][bottom][index][help] */
46 if (argv != 1) {
47 printf("Usage: %s \n", argc[0]);
48 exit(1);
49 }
50
51 }
52
53 /*
54 mark_pkey_position: updates the table of pkey positions
55
56 The mark is the end_time variable.
57 */
58 void mark_pkey_position() {
/* [<][>][^][v][top][bottom][index][help] */
59 SQ_result_set_t* rs;
60 SQ_row_t* row;
61 char pkey[100];
62 char ins_upd[200];
63 char sel[200];
64
65 //clumsy design courtesy of mysql bugs - should have a distinct
66 //and no, I cannot use nostore
67 sprintf(sel,
68 "SELECT pkey FROM archive WHERE timestamp>%d",
69 begin_time);
70 SQ_execute_query(archive_conn, sel, &rs);
71 //printf("%s\n", sel);
72
73 while ((row = SQ_row_next(rs)) != NULL) {
74 prepare_string_attribute(SQ_get_column_string_nocopy(rs, row, 0), pkey);
75
76 sprintf(ins_upd,
77 "INSERT INTO pkey_position(pkey,position) VALUES ('%s', %ld)",
78 pkey, begin_time + 1);
79 //printf("%s\n", ins_upd);
80 if (SQ_execute_query(archive_conn, ins_upd, NULL) != 0) {
81 sprintf(ins_upd,
82 "UPDATE pkey_position SET position=%ld WHERE pkey='%s'",
83 begin_time, pkey);
84 SQ_execute_query(archive_conn, ins_upd, NULL);
85 }
86 }
87 SQ_free_result(rs);
88 }
89
90 /*
91 export_archive: exports current archiving to dump
92
93 Exports current archive and table with previous references.
94
95 The name of the archive is determined from the timestamps from the
96 current archiving operation.
97
98 Also sets timestamp on backup table.
99
100 mysqldump is used and has to be on PATH!
101 */
102 void export_archive() {
/* [<][>][^][v][top][bottom][index][help] */
103 SQ_result_set_t* rs;
104 SQ_row_t* row;
105 char* cmax;
106 char dump_command[2000];
107 char ts_upd[100];
108
109 SQ_execute_query(archive_conn,
110 "SELECT max(timestamp) from archive", &rs);
111 row = SQ_row_next(rs);
112
113 if (row == NULL) {
114 printf("Problems accessing the archive2 table");
115 exit(1);
116 }
117 cmax = SQ_get_column_string_nocopy(rs, row, 0);
118 if (cmax == NULL) {
119 //printf("Its empty\n");
120 SQ_free_result(rs);
121 exit(0);
122 }
123 sprintf(dump_command,
124 "mysqldump -q -t -h%s -u%s \"-w timestamp>%ld\" -p%s %s archive > %ld-%s.dump",
125 host2, user2, begin_time, pass2, db2, begin_time, cmax);
126 if (system(dump_command) != 0) {
127 printf("mysqldump failed on archive\n");
128 exit(1);
129 }
130 sprintf(dump_command,
131 "mysqldump -t -h%s -u%s -p%s %s pkey_position > %ld-%s+pos.dump",
132 host2, user2, pass2, db2, begin_time, cmax);
133 if (system(dump_command) != 0) {
134 printf("mysqldump failed on pkey_position\n");
135 exit(1);
136 }
137
138 sprintf(ts_upd, "INSERT INTO dump VALUES (%s)", cmax);
139 if (SQ_execute_query(archive_conn, ts_upd, NULL) != 0) {
140 printf("Problems inserting new dump timestamp\n");
141 exit(1);
142 }
143 sprintf(ts_upd, "DELETE FROM dump WHERE timestamp <%s", cmax);
144 if (SQ_execute_query(archive_conn, ts_upd, NULL) != 0) {
145 printf("Problems deleting old dump timestamp\n");
146 exit(1);
147 }
148
149 SQ_free_result(rs);
150 }
151
152 void get_begin_time() {
/* [<][>][^][v][top][bottom][index][help] */
153 SQ_result_set_t* rs;
154 SQ_row_t* row;
155 char select_query[100];
156
157 sprintf(select_query, "SELECT max(timestamp) FROM dump");
158
159 //printf("%s\n", select_query);
160 SQ_execute_query(archive_conn, select_query, &rs);
161
162 row = SQ_row_next(rs);
163 if (row == NULL) {
164 printf("Could not get timestamp\n");
165 exit(1);
166 }
167
168 SQ_get_column_int(rs, row, 0, &begin_time);
169 if (begin_time <0) {
170 begin_time=0;
171 }
172 }
173
174
175 /*
176 main: Dump entry point
177
178 This function is reasonably self-commenting.
179 Just note that pk_position functions generate a table of
180 previous references of pkeys.
181 */
182 int main (int argv, char** argc) {
/* [<][>][^][v][top][bottom][index][help] */
183 process_arguments(argv, argc);
184 read_configuration();
185 get_db_connections();
186
187 get_begin_time();
188 mark_pkey_position();
189 export_archive();
190 return 0;
191 }