Skip to content

Instantly share code, notes, and snippets.

@icedraco
Created November 27, 2014 11:56
Show Gist options
  • Select an option

  • Save icedraco/2a6f80c35f788a3098fb to your computer and use it in GitHub Desktop.

Select an option

Save icedraco/2a6f80c35f788a3098fb to your computer and use it in GitHub Desktop.
A small program I wrote to update IRC services databases to MySQL on demand from outside the network
/* Routines to handle `mysqldump' invocations.
*
* (C) 2009 QuickFox.org
*
*/
#include "services.h"
/*************************************************************************/
/**
* Perform MySQL database update.
* @param ac Number of Arguments
* @param av Array if Arguments
* @return void
*/
void do_mysqldump(int ac, char **av)
{
int usage = 0; /* Display command usage? (>0 also indicates error) */
int i;
i = 1;
while (i < ac) {
if (av[i][0] == '-') {
switch (av[i][1]) {
case 'h':
usage = -1;
break;
case 'd':
if (av[i][2]) {
services_dir = av[i] + 2;
} else {
if (i >= ac - 1) {
usage = 1;
break;
}
ac--;
memmove(av + i, av + i + 1, sizeof(char *) * ac - i);
services_dir = av[i];
}
default:
usage = 1;
break;
} /* switch */
ac--;
if (i < ac)
memmove(av + i, av + i + 1, sizeof(char *) * ac - i);
} else {
i++;
}
}
if (usage) {
fprintf(stderr, "\
\n\
Usage: mysqldump [-d data-dir]\n\
\n");
exit(usage > 0 ? 1 : 0);
}
if (chdir(services_dir) < 0) {
fprintf(stderr, "chdir(%s): %s\n", services_dir, strerror(errno));
ModuleRunTimeDirCleanUp();
exit(1);
}
if (!read_config(0)) {
ModuleRunTimeDirCleanUp();
exit(1);
}
/* Load all databases */
printf("Loading databases...\n");
load_ns_dbase();
load_cs_dbase();
load_bs_dbase();
load_exceptions();
load_hs_dbase();
load_news();
load_os_dbase();
/* Save databases to MySQL */
printf("Exporting databases to MySQL...\n");
db_mysql_init();
printf(" - BotServ data\n");
save_bs_rdb_dbase();
printf(" - ChanServ data\n");
save_cs_rdb_dbase();
printf(" - Exceptions\n");
save_rdb_exceptions();
printf(" - HostServ data\n");
save_hs_rdb_dbase();
printf(" - News\n");
save_rdb_news();
printf(" - NickServ data\n");
save_ns_rdb_dbase();
printf(" - OperServ data\n");
save_os_rdb_dbase();
printf("\nAll done!\n");
lang_init();
exit(0);
}
/*************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment