code: def backup_db(callback, bool_func, output_fname, *args, **kwargs):
      from datetime import datetime
      try:
          verbose = kwargs['verbose']
      except KeyError:
          verbose = False
      try:
          retries = kwargs['retries']
      except KeyError:
          retries = -1
      try:
          force_download_latest = bool(kwargs['force_download_latest'])
      except KeyError:
          force_download_latest = False
      if verbose:
          log = open('/tmp/log_mambu_backup','a')
          log.write(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + " - Mambu DB Backup\n")
          log.flush()
      user = kwargs.pop('user', apiuser)
      pwd = kwargs.pop('pwd', apipwd)
      data = {'callback' : callback}
      try:
          posturl = iriToUri(getmambuurl(*args, **kwargs) + "database/backup")
          if verbose:
              log.write("open url: "+posturl+"\n")
              log.flush()
          resp = requests.post(posturl, data=data, headers={'content-type': 'application/json'}, auth=(apiuser, apipwd))
      except Exception as ex:
          mess = "Error requesting backup: %s" % repr(ex)
          if verbose:
              log.write(mess + "\n")
              log.close()
          raise MambuError(mess)
      if resp.status_code != 200:
          mess = "Error posting request for backup: %s" % resp.content
          if verbose:
              log.write(mess + "\n")
              log.close()
          raise MambuCommError(mess)
      data['latest'] = True
      while retries and not bool_func():
          if verbose:
              log.write("waiting...\n")
              log.flush()
          sleep(10)
          retries -= 1
          if retries < 0: retries = -1
      if not retries:
          mess = "Tired of waiting, giving up..."
          if verbose:
              log.write(mess + "\n")
              log.flush()
          if not force_download_latest:
              if verbose:
                  log.close()
              raise MambuError(mess)
          else:
              data['latest'] = False
      sleep(30)
      geturl = iriToUri(getmambuurl(*args, **kwargs) + "database/backup/LATEST")
      if verbose:
          log.write("open url: "+geturl+"\n")
          log.flush()
      resp = requests.get(geturl, auth=(apiuser, apipwd))
      if resp.status_code != 200:
          mess = "Error getting database backup: %s" % resp.content
          if verbose:
              log.write(mess + "\n")
              log.close()
          raise MambuCommError(mess)
      if verbose:
          log.write("saving...\n")
          log.flush()
      with open(output_fname, "w") as fw:
          fw.write(resp.content)
      if verbose:
          log.write("DONE!\n")
          log.close()
      return data