                 else
                     cb()
 
+        (cb) ->
+            winston.debug("rollback_last_save: testing that bup ls works, so head commit is valid")
+            bup
+                args    : ['ls']
+                bup_dir : info.bup_dir
+                cb      : (err, output) ->
+                    if not err # repo works fine
+                        cb()
+                    if err
+                        # revert to the previously specified commit, as in the log.
+                        fs.readFile "#{info.bup_dir}/logs/HEAD", (err, data) ->
+                            if err
+                                cb(err); return
+                            v = data.toString().split('\n')
+                            commit = v[v.length-1].split(' ')[0]
+                            fs.writeFile("#{info.bup_dir}/refs/heads/master", commit, cb)
+
+        (cb) ->
+            winston.debug("rollback_last_save: test again that bup ls works, so head commit is valid and rollback worked")
+            bup
+                args    : ['ls']
+                bup_dir : info.bup_dir
+                cb      : cb
     ], opts.cb)
 
 

# 30 minutes.
ensure_snapshot_queue_working = () ->
    if monitor_snapshot_queue_last_run?
        if misc.walltime() - monitor_snapshot_queue_last_run > 60*15
            winston.debug("ensure_snapshot_queue_working: BUG/ERROR ** monitor_snapshot_queue has not been called in over 15 minutes -- restarting, but you need to fix this. check logs!")
            winston.debug("ensure_snapshot_queue_working: connecting to database")
            connect_to_database (err) =>
                winston.debug("ensure_snapshot_queue_working: connect_to_databasegot back err=#{err}")

        args    : opts.args
        timeout : opts.timeout
        env     : {BUP_DIR : opts.bup_dir}
        err_on_exit : true
        cb      : opts.cb

##------------------------------------------
