function _RestoreDBSaveState($PARAMS = array())
{
    global $CONFIG, $TABLES, $DBOBJ;
    reload_tables();
    $output = array();
    $TablesToRestore = array();
    if (strlen($PARAMS['table']) > 0) {
        $TablesToRestore[] = "~~" . $PARAMS['table'];
    } else {
        foreach ($TABLES as $table => $data) {
            if (substr($table, 0, 2) == "~~") {
                $TablesToRestore[] = $table;
            }
        }
    }
    $Status = $DBOBJ->GetStatus();
    // if ($Status === true){ $DBOBJ->Query("LOCK TABLES *"); }
    foreach ($TablesToRestore as $table) {
        $Status = $DBOBJ->GetStatus();
        if ($Status === true) {
            $output[] = "restoring `" . $table . "` to `" . substr($table, 2) . "`";
            $DBOBJ->Query("DROP TABLE IF EXISTS `" . substr($table, 2) . "`;");
            $DBOBJ->Query("RENAME TABLE `" . $table . "` TO `" . substr($table, 2) . "`;");
        } else {
            $output[] = "DB Error: " . $Status;
            break;
        }
    }
    $Status = $DBOBJ->GetStatus();
    if ($Status === true) {
        $DBOBJ->Query("UNLOCK TABLES;");
    }
    return $output;
}
function _RestoreDBSaveState($PARAMS = array())
{
    global $CONFIG, $TABLES, $DBOBJ;
    reload_tables();
    $MongoDB = $DBOBJ->GetMongoDB();
    $output = array();
    $TablesToRestore = array();
    if (strlen($PARAMS['table']) > 0) {
        $TablesToRestore[] = "~~" . $PARAMS['table'];
    } else {
        $list = $MongoDB->listCollections();
        $TableList = array();
        foreach ($list as $sub) {
            $name = $sub->getName();
            if ($name != "cache" && $name != "log" && substr($name, 0, 2) == "~~") {
                $TablesToRestore[] = $name;
            }
        }
    }
    foreach ($TablesToRestore as $table) {
        $Status = $DBOBJ->GetStatus();
        if ($Status === true) {
            $Connection = $DBOBJ->GetConnection();
            $adminMongoDB = $Connection->admin;
            //connect to admin DB to force authentication
            $output[] = "restoring `" . $table . "` to `" . substr($table, 2) . "`";
            $origName = substr($table, 2);
            $MongoDB->{$origName}->drop();
            $adminMongoDB->command(array("renameCollection" => $CONFIG["DB"] . "." . $table, "to" => $CONFIG["DB"] . "." . $origName));
            $MongoDB->{$table}->drop();
        } else {
            $output[] = "DB Error: " . $Status;
            break;
        }
    }
    return $output;
}