Managing backup policies in Managed Service for PostgreSQL
Backup policies give you flexible control over how cluster backups are created and stored. You can configure a schedule for creating full backups and set their retention period.
Creating a backup policy
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
See the description of the CLI command for creating a policy:
yc managed-postgresql backup-retention-policy create --help -
Create a policy:
yc managed-postgresql backup-retention-policy create \ --cluster-id <cluster_ID> \ --policy-name <policy_name> \ --day-of-month <day_of_month> \ --month <month> \ --day-of-week <day_of_week> \ --retain-for-days <backup_retention_period_in_days> \ --description <policy_description>Where:
-
day-of-month: Day of month. Allowed values:1–31. -
month: Month. Allowed values:1–12orJAN–DEC. -
day-of-week: Day of week. Allowed values:0–6orSUN–SAT. -
retain-for-days: Backup retention period in days. The minimum value is7.The
day-of-month,month, andday-of-weeksettings define the schedule for creating backups. The schedule is defined as a cron expression without hours and minutes.Cron expressions also support the following special characters:
*: Selecting all possible values. A backup will be created on each scheduled interval (day or month) when possible, provided it does not conflict with other settings. This option is used by default if the setting is not specified.,: Listing multiple values.–: Specifying a range of values./: Specifying a step value. For example,*/3for theday-of-monthsetting means a backup will be created every three days.
If no schedule settings are specified, backups are created using the cron expression
* * *, meaning they run daily.Examples of cron expressions:
1 */6 *: Backup is created on the first day of the month, every six months.31 jan SUN: Backup is created on Sundays in January, and also on January 31.* * WED: Backup is created every Wednesday.
You can test your cron expression in this editor
.
You can get the cluster ID from the cluster list.
Result:
policy_id: mdbt553glp51******** cluster_id: c9q5le6h1a4k******** policy_name: test-policy created_at: "2025-03-25T15:55:50.393000450Z" cron: day_of_month: "25" month: mar day_of_week: TUE retain_for_days: "300" -
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the BackupRetentionPolicy.Create method, for instance, via the following cURL
request:curl \ --request POST \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/retention_policies' \ --data '{ "cron": { "dayOfMonth": "<day_of_month>", "month": "<month>", "dayOfWeek": "<day_of_week>" }, "retainForDays": "<retention_period_in_days>", "description": "<policy_description>", "policyName": "<policy_name>" }'Where:
-
dayOfMonth: Day of the month. Allowed values:1-31. -
month: Month. Allowed values:1-12orJAN-DEC. -
dayOfWeek: Day of the week. Allowed values:0-6orSUN-SAT. -
retainForDays: Backup retention period in days. The minimum value is7.The
dayOfMonth,month, anddayOfWeekdefine the schedule for creating backups. The schedule is defined as a cron expression without hours and minutes.Cron expressions also support the following special characters:
*: Selecting all possible values. A backup will be created on each scheduled interval (day or month) when possible, provided it does not conflict with other settings. This option is used by default if the setting is not specified.,: Listing multiple values.–: Specifying a range of values./: Specifying a step value. For example,*/3for thedayOfMonthsetting means a backup will be created every three days.
If no schedule settings are specified, backups are created using the cron expression
* * *, meaning they run daily.Examples of cron expressions:
1 */6 *: Backup is created on the first day of the month, every six months.31 jan SUN: Backup is created on Sundays in January, and also on January 31.* * WED: Backup is created every Wednesday.
You can test your cron expression in this editor
.
You can get the cluster ID from the list of clusters.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume that the repository contents reside in the
~/cloudapi/directory. -
To create a policy:
-
Call the BackupRetentionPolicyService.Create method, for instance, via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/backup_retention_policy_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "cron": { "day_of_month": "<day_of_month>", "month": "<month>", "day_of_week": "<day_of_week>" }, "retain_for_days": "<backup_retention_period_in_days>", "description": "<policy_description>", "policy_name": "<policy_name>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.BackupRetentionPolicyService.CreateWhere:
-
day_of_month: Day of the month. Allowed values:1–31. -
month: Month. Allowed values:1–12orJAN–DEC. -
day_of_week: Day of the week. Allowed values:0–6orSUN–SAT. -
retain_for_days: Backup retention period in days. The minimum value is7.The
day_of_month,month, andday_of_weekdefine the schedule for creating backups. The schedule is defined as a cron expression without hours and minutes.Cron expressions also support the following special characters:
*: Selecting all possible values. A backup will be created on each scheduled interval (day or month) when possible, provided it does not conflict with other settings. This option is used by default if the setting is not specified.,: Listing multiple values.–: Specifying a range of values./: Specifying a step value. For example,*/3for theday_of_monthsetting means a backup will be created every three days.
If no schedule settings are specified, backups are created using the cron expression
* * *, meaning they run daily.Examples of cron expressions:
1 */6 *: Backup is created on the first day of the month, every six months.31 jan SUN: Backup is created on Sundays in January, and also on January 31.* * WED: Backup is created every Wednesday.
You can test your cron expression in this editor
.
You can get the cluster ID from the cluster list.
-
-
Check the server response to make sure your request was successful.
-
Getting a list of backup policies
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
See the description of the CLI command for getting a list of policies:
yc managed-postgresql backup-retention-policy list --help -
Get a list of policies for the Managed Service for PostgreSQL cluster:
yc managed-postgresql backup-retention-policy list \ --cluster-id <cluster_ID>You can get the cluster ID from the cluster list.
Result:
+----------------------+-------------+--------------------+------------+-------------------+ | ID | NAME | CLUSTER ID | CRONTAB | RETENTION PERIOD | +----------------------+-------------+--------------------+------------+-------------------+ | mdbt553glp51******** | test-policy | c9q5le6h1a4******* | 31 JAN SUN | 50 | +----------------------+-------------+--------------------+------------+-------------------+Order of parameters in the
CRONTABcolumn:day_of_month,month,day_of_week.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the BackupRetentionPolicy.List method, for instance, via the following cURL
request:curl \ --request GET \ --header "Authorization: Bearer $IAM_TOKEN" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/retention_policies?pageSize=<number_of_results>'Where
page_sizeis the number of query results per page. Specify a number greater than zero.You can get the cluster ID from the list of clusters.
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume that the repository contents reside in the
~/cloudapi/directory. -
To get a list of policies for a cluster:
-
Call the BackupRetentionPolicyService.List method, for instance, via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/backup_retention_policy_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "page_size": "<number_of_results>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.BackupRetentionPolicyService.ListWhere
page_sizeis the number of query results per page. Specify a number greater than zero.You can get the cluster ID from the list of clusters.
-
Check the server response to make sure your request was successful.
-
Deleting a backup policy
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
-
See the description of the CLI command for deleting a policy:
yc managed-postgresql backup-retention-policy delete --help -
Delete a policy:
yc managed-postgresql backup-retention-policy delete <policy_ID> \ --cluster-id <cluster_ID>You can get the cluster ID from the list of clusters, and the policy ID from the list of policies.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the BackupRetentionPolicy.Delete method, for instance, via the following cURL
request:curl \ --request DELETE \ --header "Authorization: Bearer $IAM_TOKEN" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/retention_policies/<policy_ID>'You can get the cluster ID from the list of clusters, and the policy ID from the list of policies.
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume that the repository contents reside in the
~/cloudapi/directory. -
To delete a policy:
-
Call the BackupRetentionPolicyService.Delete method, for instance, via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/backup_retention_policy_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "policy_id": "<policy_ID>", "cluster_id": "<cluster_ID>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.BackupRetentionPolicyService.DeleteYou can get the cluster ID from the list of clusters, and the policy ID from the list of policies.
-
Check the server response to make sure your request was successful.
-