Right now to recover from a backup with an encrypted HD wallet the user must do something like unlock the wallet with a really long time out and manually trigger a rescan,  otherwise they risk missing some keys when auto-topup cannot run.
return nRet;
}
void CWalletMarkDirty()
{
    {
        LOCK(cs_);
        for (stdpair<const uint256, CWalletTx>& item  mapWallet)
            item.second.MarkDirty();
    }
}
bool CWalletMarkReplaced(const uint256& originalHash, const uint256& newHash)
{
    LOCK(cs_);
    auto mi = mapWallet.find(originalHash);
    // T
    assert(mi != mapWallet.end());
    CWalletTx& wtx = (*mi).second;
    // E
    assert(wtx.mapValue.count("replaced_by_txid") == 0);
    wtx.mapValue["replaced_by_txid"] = newHash.ToString();
    // R
    RefreshMempoolStatus(wtx, chain());
    WalletBatch batch(GetDatabase());
    bool success = true;
    if (!batch.WriteTx(wtx)) {
        WalletLogPrintf("%s Updating batch tx %s failed\n", __func__, wtx.GetHash().ToString());
        success = false;
    }
    NotifyTransactionChanged(originalHash, CT_UPDATED);
    return success;