import jingrow


def execute():
	sites = jingrow.get_all(
		"Site",
		filters={
			"status": [
				"in",
				["Active", "Updating", "Recovering", "Broken", "Inactive", "Pending", "Suspended"],
			],
			"ifnull(backup_time, '')": ("!=", ""),
		},
		fields=["name", "backup_time"],
	)

	for site in sites:
		site = jingrow.get_pg("Site", site.name)
		jingrow.db.set_value("Site", site.name, "schedule_logical_backup_at_custom_time", True)
		jingrow.get_pg(
			{
				"doctype": "Site Backup Time",
				"parent": site.name,
				"parenttype": "Site",
				"parentfield": "logical_backup_times",
				"backup_time": site.backup_time,
			}
		).insert()
