2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2012 Andrew Hutchings (andrew@linuxjedi.co.uk)
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are
11
* * Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* * Redistributions in binary form must reproduce the above
15
* copyright notice, this list of conditions and the following disclaimer
16
* in the documentation and/or other materials provided with the
19
* * The names of its contributors may not be used to endorse or
20
* promote products derived from this software without specific prior
23
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
#include <libdrizzle-5.0/libdrizzle.h>
41
#include <glib/gstdio.h>
45
#define STR_HELPER(x) #x
46
#define STR(x) STR_HELPER(x)
49
gint port= DRIZZLE_DEFAULT_TCP_PORT;
51
gboolean user_alloced= FALSE;
54
gchar *start_file= NULL;
56
gboolean continuous= FALSE;
58
static GOptionEntry main_options[]= {
59
{ "host", 0, 0, G_OPTION_ARG_STRING, &host, "Hostname of server, default "DRIZZLE_DEFAULT_TCP_HOST, NULL },
60
{ "port", 0, 0, G_OPTION_ARG_INT, &port, "Port number of server, default "STR(DRIZZLE_DEFAULT_TCP_PORT), NULL },
61
{ "user", 0, 0, G_OPTION_ARG_STRING, &user, "Username for the server, default is current system user", NULL },
62
{ "pass", 0, 0, G_OPTION_ARG_STRING, &pass, "Password for the server", NULL },
63
{ "outdir", 0, 0, G_OPTION_ARG_FILENAME, &outdir, "Output directory", NULL },
64
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
67
static GOptionEntry binlog_options[]= {
68
{ "start-file", 0, 0, G_OPTION_ARG_FILENAME, &start_file, "Binlog file to start with", NULL },
69
{ "start-pos", 0, 0, G_OPTION_ARG_INT, &start_pos, "Position to start with", NULL },
70
{ "continuous", 0, 0, G_OPTION_ARG_NONE, &continuous, "Continous download mode", NULL }
73
gboolean get_system_user(char *dest, uint8_t len);
74
drizzle_con_st *_connect(void);
75
FILE *create_binlog_file(char *binlog_file);
76
void get_binlogs(drizzle_con_st *con);
77
void write_binlog(FILE* file, const uint8_t* data, uint32_t len);
79
gboolean get_system_user(char *dest, uint8_t len)
81
long pw_len= sysconf(_SC_GETPW_R_SIZE_MAX);
82
struct passwd pw_struct;
83
struct passwd *pw_tmp_struct;
88
char *pw_buffer= g_malloc(pw_len);
90
if (getpwuid_r(geteuid(), &pw_struct, pw_buffer, pw_len, &pw_tmp_struct) == 0)
92
g_strlcpy(dest, pw_struct.pw_name, len);
100
drizzle_con_st *_connect(void)
104
drizzle_return_t ret;
106
drizzle= drizzle_create();
109
g_print("Drizzle object creation error\n");
112
con= drizzle_con_add_tcp(drizzle, host, port, user, pass, "", 0);
115
g_print("Drizzle connection object creation error\n");
118
ret= drizzle_con_connect(con);
119
if (ret != DRIZZLE_RETURN_OK)
121
g_print("Error connecting to server: %s\n", drizzle_con_error(con));
127
FILE *create_binlog_file(char *binlog_file)
133
filename= g_strdup_printf("%s/%s", outdir, binlog_file);
137
filename= binlog_file;
139
outfile= g_fopen(filename, "w");
147
void get_binlogs(drizzle_con_st *con)
149
drizzle_result_st *result;
150
drizzle_return_t ret;
155
gboolean read_end= FALSE;
159
server_id= g_random_int_range(32768, 65535);
166
result= drizzle_start_binlog(con, server_id, start_file, start_pos, &ret);
167
if (ret != DRIZZLE_RETURN_OK)
169
g_print("Drizzle binlog start failure: %s\n", drizzle_con_error(con));
173
binlog_file= g_strdup(start_file);
174
outfile= create_binlog_file(binlog_file);
177
g_print("Could not create binlog file '%s', errno %d\n", binlog_file, errno);
183
write_binlog(outfile, (uint8_t *)DRIZZLE_BINLOG_MAGIC, 4);
186
ret= drizzle_binlog_get_next_event(result);
187
event_len= drizzle_binlog_event_raw_length(result);
188
if (ret != DRIZZLE_RETURN_OK)
191
if (ret != DRIZZLE_RETURN_EOF)
193
g_print("Read error: %d - %s\n", ret, drizzle_con_error(con));
198
if (drizzle_binlog_event_type(result) == DRIZZLE_EVENT_TYPE_ROTATE)
202
binlog_file= g_strndup((const gchar *)drizzle_binlog_event_data(result), drizzle_binlog_event_length(result));
203
outfile= create_binlog_file(binlog_file);
206
g_print("Could not create binlog file '%s', errno %d\n", binlog_file, errno);
211
write_binlog(outfile, drizzle_binlog_event_raw_data(result), event_len);
219
drizzle_result_free(result);
222
void write_binlog(FILE* file, const uint8_t* data, uint32_t len)
226
if (write(fileno(file), data, len) <= 0)
228
g_print("Error: binlog: Error writing binary log: %s", strerror(errno));
235
int main(int argc, char *argv[])
238
GOptionContext *context;
241
char sysuser[DRIZZLE_MAX_USER_SIZE];
243
context= g_option_context_new(" - Drizzle binlog retriever");
244
GOptionGroup *main_group= g_option_group_new("main", "Main Options", "Main Options", NULL, NULL);
245
GOptionGroup *binlog_group= g_option_group_new("binlog", "Binlog Options", "Binlog Options", NULL, NULL);
246
g_option_group_add_entries(main_group, main_options);
247
g_option_group_add_entries(binlog_group, binlog_options);
248
g_option_context_set_main_group(context, main_group);
249
g_option_context_add_group(context, binlog_group);
250
if (!g_option_context_parse(context, &argc, &argv, &error))
252
g_print("Error parsing options: %s, try --help\n", error->message);
258
if (!get_system_user(user, DRIZZLE_MAX_USER_SIZE))
260
g_print("No user specified and could not determine current user\n");
266
host= g_strdup(DRIZZLE_DEFAULT_TCP_HOST);
270
port= DRIZZLE_DEFAULT_TCP_PORT;
274
g_print("Binlog start file required\n");
283
drizzle= drizzle_con_drizzle(con);
284
drizzle_con_quit(con);
285
drizzle_free(drizzle);
286
g_option_context_free(context);