Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined MYDUMP
- #endinput
- #endif
- #define MYDUMP
- #if defined MAX_TABLES
- #if MAX_TABLES > 300
- #error MAX_TABLES can't be bigger than 300
- #endif
- #else
- #define MAX_TABLES 100
- #endif
- #define INVALID_TIMER 0xFFFF
- new MyDump_Table[MAX_TABLES][32];
- new MyDump_Temp[4000];
- new SMyDump_Temp[1000];
- new WMyDump_Temp[1000];
- new AMyDump_Temp[5000];
- new FMyDump_Temp[1000];
- new MyDump_DB[MyDump:4][50];
- new AutoExportTimer[MyDump:4] = INVALID_TIMER;
- new AutoExportTime[MyDump:4] = 43200;
- new AutoExportIfNotExists[MyDump:4] = 1;
- new MyDump_find1;
- new MyDump_find2;
- stock MyDump:MyDumpExportInit(host[], user[], pw[], db[])
- {
- new MySQL:id;
- new MyDump:MyID;
- id = mysql_init();
- mysql_connect(host, user, pw, db, id);
- MyID = MyDump:id;
- format(MyDump_DB[MyID], 50, "%s", db);
- return MyDump:id;
- }
- stock MyDumpExportExit(MyDump:handle)
- {
- mysql_close(MySQL:handle);
- return 1;
- }
- stock SaveTables(MyDump:handle)
- {
- format(MyDump_Temp, sizeof(MyDump_Temp), "SHOW TABLES FROM `%s`;", MyDump_DB[handle]);
- mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
- mysql_store_result(MySQL:handle);
- new data_table[32];
- new i;
- for(; i<MAX_TABLES; i++) MyDump_Table[i] = "\0";
- new bool:con;
- i = 0;
- while(mysql_fetch_row(data_table))
- {
- if(con) continue;
- format(MyDump_Table[i], 32, "%s", data_table);
- i++;
- printf(">> %s", data_table);
- if(i == MAX_TABLES) con = true;
- }
- mysql_free_result(MySQL:handle);
- return 1;
- }
- stock ExportTablesToFile(file[], MyDump:handle, IfNotExists = 1)
- {
- SaveTables(handle);
- for(new i; i<MAX_TABLES; i++) {
- if(strlen(MyDump_Table[i]) == 0) continue;
- new File:fhandle;
- format(MyDump_Temp, sizeof(MyDump_Temp), "%s", ExportTable(MyDump_Table[i], handle));
- strdel(MyDump_Temp, 42+strlen(MyDump_Table[i]), strlen(MyDump_Table[i])+1+42+strlen(MyDump_Table[i]));
- format(MyDump_Temp, sizeof(MyDump_Temp), "%s\n", MyDump_Temp);
- if(IfNotExists) strins(MyDump_Temp, " IF NOT EXISTS", 12+42+strlen(MyDump_Table[i]), 20);
- if(!fexist(file)) fhandle = fopen(file, io_write);
- else fhandle = fopen(file, io_append);
- fwrite(fhandle, MyDump_Temp);
- fclose(fhandle);
- fhandle = fopen(file, io_append);
- MyDump_find1 = strfind(MyDump_Temp, "(", true);
- MyDump_find2 = strfind(MyDump_Temp, ") ENGINE=", true);
- strdel(MyDump_Temp, 0, MyDump_find1+1);
- strdel(MyDump_Temp, MyDump_find2-MyDump_find1+1, strlen(MyDump_Temp));
- MyDump_find1 = strfind(MyDump_Temp, "PRIMARY", true);
- MyDump_find2 = strfind(MyDump_Temp, ")", true, MyDump_find1);
- strdel(MyDump_Temp, MyDump_find1, MyDump_find2+3);
- MyDump_find1 = strfind(MyDump_Temp, "`", true);
- strdel(MyDump_Temp, 0, MyDump_find1+1);
- format(SMyDump_Temp, sizeof(SMyDump_Temp), "\n--\n-- Daten für Tabelle `%s`\n--\n\nINSERT INTO `%s` (", MyDump_Table[i], MyDump_Table[i]);
- while(MyDump_find2 != -1) {
- MyDump_find1 = strfind(MyDump_Temp, "`", true);
- format(WMyDump_Temp, sizeof(WMyDump_Temp), "%s", MyDump_Temp);
- strdel(WMyDump_Temp, MyDump_find1, strlen(WMyDump_Temp));
- MyDump_find2 = strfind(MyDump_Temp, "`", true, MyDump_find1+1);
- strdel(MyDump_Temp, 0, MyDump_find2+1);
- printf("> %s", WMyDump_Temp);
- format(SMyDump_Temp, sizeof(SMyDump_Temp), "%s,`%s`", SMyDump_Temp, WMyDump_Temp);
- }
- strdel(SMyDump_Temp, strlen(MyDump_Table[i])+strlen(MyDump_Table[i])+16+32, strlen(MyDump_Table[i])+strlen(MyDump_Table[i])+17+32);
- fwrite(fhandle, SMyDump_Temp);
- ExportContents(MyDump_Table[i], fhandle, handle);
- fclose(fhandle);
- }
- return 1;
- }
- stock ExportContents(table[], File:fhandle, MyDump:handle)
- {
- format(MyDump_Temp, sizeof(MyDump_Temp), "SELECT * FROM `%s`;", table);
- mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
- mysql_store_result(MySQL:handle);
- fwrite(fhandle, ") VALUES");
- new rows = mysql_num_rows(MySQL:handle);
- new row;
- while(mysql_fetch_row(MyDump_Temp, "|", MySQL:handle))
- {
- row++;
- new num = mysql_num_fields(MySQL:handle);
- fwrite(fhandle, "\n(");
- AMyDump_Temp = "\0";
- for(new o; o<num; o++)
- {
- mysql_fetch_field_num(o, FMyDump_Temp, MySQL:handle);
- format(AMyDump_Temp, sizeof(AMyDump_Temp), "%s'%s',", AMyDump_Temp, FMyDump_Temp);
- }
- strdel(AMyDump_Temp, strlen(AMyDump_Temp)-1, strlen(AMyDump_Temp));
- fwrite(fhandle, AMyDump_Temp);
- if(rows == row) fwrite(fhandle, ");\n\n");
- else fwrite(fhandle, "),");
- }
- mysql_free_result(MySQL:handle);
- return 1;
- }
- stock ExportTable(data_table[], MyDump:handle)
- {
- format(MyDump_Temp, sizeof(MyDump_Temp), "SHOW CREATE TABLE `%s`;", data_table);
- mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
- mysql_store_result(MySQL:handle);
- mysql_fetch_row(MyDump_Temp, "|", MySQL:handle);
- mysql_free_result(MySQL:handle);
- format(MyDump_Temp, sizeof(MyDump_Temp), "--\n-- Tabellenstruktur für Tabelle `%s`\n--\n\n%s", data_table, MyDump_Temp);
- return MyDump_Temp;
- }
- stock SetAutoExportTime(sec, MyDump:handle)
- {
- AutoExportTime[handle] = sec;
- return 1;
- }
- stock SetAutoExportIfNotExists(num, MyDump:handle)
- {
- AutoExportIfNotExists[handle] = num;
- return 1;
- }
- stock AutoExportTimerStart(MyDump:handle)
- {
- if(AutoExportTimer[handle] != INVALID_TIMER) return 0;
- AutoExportTimer[handle] = SetTimerEx("AutoExport", AutoExportTimer[handle]*1000, 1, "dd", int:handle, int:AutoExportIfNotExists[handle]);
- return 1;
- }
- stock AutoExportTimerStop(MyDump:handle)
- {
- KillTimer(AutoExportTimer[handle]);
- AutoExportTimer[handle] = INVALID_TIMER;
- return 1;
- }
- forward AutoExport(MyDump:handle, ifnotexists);
- public AutoExport(MyDump:handle, ifnotexists)
- {
- new stamp, h, m, s;
- stamp = getdate(h, m, s);
- new str[50];
- format(str, sizeof(str), "%d.sql", stamp);
- ExportTablesToFile(str, handle, ifnotexists);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement