import json


def backup_luks2_token(module, device, token_id):
    """Backup LUKS2 token, as we may need to restore the metadata.
    Return: <backup> <error>"""

    args = ["cryptsetup", "token", "export", "--token-id", token_id, device]
    ret, token, err = module.run_command(args)
    if ret != 0:
        return None, {"msg": "Error during token backup: {0}".format(err)}

    try:
        token_json = json.loads(token)
    except ValueError as exc:
        return None, {"msg": str(exc)}
    return token_json, None