     
     // MARK: - Helper methods
     
+    public func backupIfNeeded() {
+        let dbBackupPath = dbPath + "1"
+        serialQueue.async {
+            let fileManager = FileManager.default
+            let dbTempPath = dbBackupPath + ".temp"
+            do {
+                //logger.log("dbPath: \(dbPath)")
+                try fileManager.copyItem(atPath: self.dbPath, toPath: dbTempPath)
+            }
+            catch {
+            }
+            do {
+                try fileManager.removeItem(atPath: dbBackupPath)
+            }
+            catch {
+            }
+            do {
+                try fileManager.moveItem(atPath: dbTempPath, toPath: dbBackupPath)
+            }
+            catch {
+            }
+        }
+    }
+    
     open func deleteDatabaseFromDisk() {
         self.close()
         serialQueue.smartSync {
             do {
                 let fileManager = FileManager.default
-                try fileManager.removeItem(atPath: path)
+                try fileManager.removeItem(atPath: dbPath)
             } catch {
-                NSLog("error deleting database at path \(path), \(error)")
+                NSLog("error deleting database at dbPath \(dbPath), \(error)")
             }
         }
     }
     
     public func open() {
         serialQueue.smartSync {
-            self.db = levelDBOpen(path.cString)
+            self.db = levelDBOpen(dbPath.cString)
         }
     }
     

    
    deinit {
        self.close()
    }
    
    // MARK: - Class methods

open class LevelDB {
    public let serialQueue = DispatchQueue(label: "org.amr.leveldb")
    
    public var parentPath = ""
    public var name = "Database"
	public var dictionaryEncoder: (String, [String : Any]) -> Data?
    public var dictionaryDecoder: (String, Data) -> [String : Any]?
    public var encoder: (String, Data) -> Data?
    public var decoder: (String, Data) -> Data?
    public var db: UnsafeMutableRawPointer?
    
    var dbPath: String {
        return parentPath + "/" + name
    }
    
    // MARK: - Life cycle
    
    
    required public init(parentPath: String, name: String) {
        //NSLog("LevelDB init")
        self.parentPath = parentPath
        self.name = name
        //NSLog("LevelDB self.name: \(name)")
        //self.path = parentPath + "/" + name
        //NSLog("LevelDB path: \(path)")
        self.dictionaryEncoder = { key, value in
            #if DEBUG
