diff options
-rw-r--r-- | include/modules.h | 7 | ||||
-rw-r--r-- | include/services.h | 1 | ||||
-rw-r--r-- | src/core/db_plain.cpp | 47 | ||||
-rw-r--r-- | src/log.c | 4 |
4 files changed, 49 insertions, 10 deletions
diff --git a/include/modules.h b/include/modules.h index a99d69900..aad816d39 100644 --- a/include/modules.h +++ b/include/modules.h @@ -560,11 +560,6 @@ class CoreExport Module */ virtual EventReturn OnLoadDatabase() { return EVENT_CONTINUE; } - /** Called when anope backs up databases. - * NOTE: This event is deprecated pending new database handling. - */ - virtual void OnBackupDatabase() MARK_DEPRECATED { } - /** Called when anope needs to check passwords against encryption * see src/encrypt.c for detailed informations */ @@ -1183,7 +1178,7 @@ enum Implementation I_OnAddSXLine, I_OnDelSXLine, /* Database */ - I_OnPostLoadDatabases, I_OnSaveDatabase, I_OnLoadDatabase, I_OnBackupDatabase, + I_OnPostLoadDatabases, I_OnSaveDatabase, I_OnLoadDatabase, I_OnDatabaseExpire, I_OnDatabaseWrite, I_OnDatabaseRead, I_OnDatabaseReadMetadata, I_OnDatabaseWriteMetadata, diff --git a/include/services.h b/include/services.h index ea6b89364..4acd9928c 100644 --- a/include/services.h +++ b/include/services.h @@ -68,6 +68,7 @@ # define DllExport # define CoreExport # define MARK_DEPRECATED __attribute((deprecated)) +# define DeleteFile unlink #else # include <winsock2.h> # include <ws2tcpip.h> diff --git a/src/core/db_plain.cpp b/src/core/db_plain.cpp index ca10a3a03..d461fd282 100644 --- a/src/core/db_plain.cpp +++ b/src/core/db_plain.cpp @@ -514,6 +514,10 @@ void WriteMetadata(const std::string &key, const std::string &data) class DBPlain : public Module { + /* Day the last backup was on */ + int LastDay; + /* Backup file names */ + std::list<std::string> Backups; public: DBPlain(const std::string &modname, const std::string &creator) : Module(modname, creator) { @@ -525,6 +529,47 @@ class DBPlain : public Module ModuleManager::Attach(i, this, 6); OnReload(true); + + LastDay = 0; + } + + ~DBPlain() + { + } + + void BackupDatabase() + { + time_t now = time(NULL); + tm *tm = localtime(&now); + + if (tm->tm_mday != LastDay) + { + LastDay = tm->tm_mday; + char *newname = new char[DatabaseFile.length() + 30]; + snprintf(newname, DatabaseFile.length() + 30, "backups/%s.%d%d%d", DatabaseFile.c_str(), tm->tm_year, tm->tm_mon, tm->tm_mday); + + if (rename(DatabaseFile.c_str(), newname) != 0) + { + ircdproto->SendGlobops(findbot(Config.s_OperServ), "Unable to backup database!"); + Alog() << "Unable to back up database!"; + + if (!Config.NoBackupOkay) + quitting = 1; + + return; + } + + Backups.push_back(newname); + + delete [] newname; + + unsigned KeepBackups = Config.KeepBackups; + if (KeepBackups && Backups.size() > KeepBackups) + { + DeleteFile(Backups.front().c_str()); + Backups.pop_front(); + } + } } void OnReload(bool) @@ -842,6 +887,8 @@ class DBPlain : public Module EventReturn OnSaveDatabase() { + BackupDatabase(); + db.open(DatabaseFile.c_str(), std::ios_base::out | std::ios_base::trunc); if (!db.is_open()) @@ -58,11 +58,7 @@ static void remove_log() /* removed if from here cause get_logchan is always 1 */ get_logname(name, sizeof(name), &tm); -#ifndef _WIN32 - unlink(name); -#else DeleteFile(name); -#endif } /*************************************************************************/ |