Skip to content

Commit

Permalink
Now creates a backup when migrating LMDB databases
Browse files Browse the repository at this point in the history
Ticket: None
Changelog: Title
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Oct 23, 2024
1 parent 0293ff3 commit bae0737
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion libpromises/dbm_migration.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <lastseen.h>
#include <string_lib.h>
#include <files_copy.h>

extern const DBMigrationFunction dbm_migration_plan_lastseen[];

Expand Down Expand Up @@ -64,10 +65,46 @@ bool DBMigrate(DBHandle *db, dbid id)
{
if (step_version == DBVersion(db))
{
if (!(*step)(db))
/* Create a backup */
char *db_path = DBIdToPath(id);
char backup[strlen(db_path) + strlen(".backup") + 1];
NDEBUG_UNUSED int ret = snprintf(backup, sizeof(backup),
"%s.backup", db_path);
assert(ret > 0 && (size_t) ret < sizeof(backup));

Log(LOG_LEVEL_INFO,
"Backing up '%s' to '%s' before migration.", db_path,
backup);

if (!CopyRegularFileDisk(db_path, backup))
{
Log(LOG_LEVEL_WARNING,
"Failed to backup '%s' before migration", db_path);
}

/* Preform migration */
if ((*step)(db))
{
/* Migration was successful! We can safely remove the
* backup */
Log(LOG_LEVEL_INFO,
"Deleting backup of '%s' after successful migration.",
backup);
unlink(backup);
}
else
{
/* Migration failed! Error is already logged. Delete DB so
* that we can start fresh. It's not ideal, but it's the
* best we can do. */
Log(LOG_LEVEL_INFO,
"Deleting '%s' due to failed migration. "
"A backup is available in '%s'.", db_path, backup);
unlink(db_path);
free(db_path);
return false;
}
free(db_path);
}
}
}
Expand Down

0 comments on commit bae0737

Please sign in to comment.