def backup_db_file(db_file, log=None):
    """Backup a database file if it exists"""
    timestamp = datetime.now().strftime('.%Y-%m-%d-%H%M%S')
    backup_db_file = db_file + timestamp
    for i in range(1, 10):
        if not os.path.exists(backup_db_file):
            break
        backup_db_file = f'{db_file}.{timestamp}.{i}'
    #
    if os.path.exists(backup_db_file):
        raise OSError(f"backup db file already exists: {backup_db_file}")
    if log:
        log.info("Backing up %s => %s", db_file, backup_db_file)
    shutil.copy(db_file, backup_db_file)
Backup a database file if it exists
jupyterhub/dbutil.py