本文为您介绍MaxCompute的备份与恢复功能和操作命令,并提供参考示例。

概述

MaxCompute提供数据备份与恢复功能,系统会自动备份数据的历史版本(例如被删除或修改前的数据)并保留一定时间,您可以对保留周期内的数据进行快速恢复,避免因误操作丢失数据。备份与恢复
备份与恢复功能具备以下特点:
  • 默认开启,不需要手动开通

    该功能不依赖外部存储,系统默认为所有MaxCompute项目开放的数据保留周期为24小时,备份和存储免费。

  • 自动持续备份

    系统自动对发生变更的数据进行备份,多次变更时将备份多个数据版本,相比固定周期性的备份策略,可以有效避免因误操作丢失数据。

  • 恢复快速,操作简单

    MaxCompute具备先进的元数据和多数据版本管理能力,备份和恢复操作不占用额外的计算资源,您可以通过命令快速恢复不同规模的数据。

注意事项

使用备份恢复功能时,您需要注意如下事项:
  • 如果您通过MaxCompute客户端查看表的备份信息,建议您下载最新版本的MaxCompute客户端,历史版本的客户端可能无法查看到备份恢复参数及表的备份信息。
  • 对表执行一次修改操作就会生成一个备份版本。如果未对表执行修改操作,不会生成备份版本。
  • 当某个备份版本的保留天数超过项目设置的备份数据保留天数时,MaxCompute会自动删除备份版本。被删除的备份版本无法恢复,也无法查询到。
  • 如果表执行过Purge命令后,数据不可以恢复。

操作命令

备份与恢复功能涉及的操作命令如下表所示。
场景命令功能权限说明
设置备份数据保留周期setproject odps.timemachine.retention.days=days;设置备份数据的保留天数。在此期间,您可以将当前版本恢复至任意一个备份的数据版本。

days的取值范围为[0,30],默认值为1。0代表关闭备份功能。

调整备份周期后的生效策略为:
  • 延长备份周期:新的备份周期于当日开始生效。
  • 缩短备份周期:系统将自动删除超过保留周期的备份数据。
仅阿里云账号或项目管理员可以操作。
setproject;通过MaxCompute客户端控执行此命令打印项目级别的参数信息,MaxCompute客户端使用请参见使用客户端(odpscmd)连接。您可以查看odps.timemachine.retention.days参数的取值。例如odps.timemachine.retention.days=1,代表当前项目备份数据的保留周期为1天(24小时)。
查看备份数据show history for tables;查看当前项目内的表和处于备份状态的表信息,包括表名、表ID、创建时间和删除时间等,与show tables;命令不相同。您需要具备Project的List权限。

权限详情请参见MaxCompute权限

show history for table <table_name>;查看指定表的备份数据,获取保留周期内备份的各个数据版本信息。
  • 如果表存在,您需要具备Table的ShowHistory权限。
  • 如果表已删除,执行该命令会报错,您可以通过show history for tables;命令查找已经删除的表及备份的数据版本信息。

权限详情请参见MaxCompute权限

show history for table table_name ('id'='xxxx');查看已删除表的备份数据,获取保留周期内备份的各个数据版本信息。

您可以通过show history for tables;命令查找已经删除的表,获取表名和表ID信息。

您需要具备Project的List权限。

权限详情请参见MaxCompute权限

show history for table table_name partition_spec;查看指定分区的备份数据,获取保留周期内备份的各个数据版本信息。无。
show history for table table_name PARTITION('id'='xxxx');查看已删除分区的备份数据,获取保留周期内备份的各个数据版本信息。id可以通过show history for table <table_name>;命令返回结果中的ObjectId字段获取。
恢复数据restore table table_name ('id'='xxxxx');恢复已删除的表。

您可以通过show history for tables;命令查找已经删除的表,获取表名和表ID信息。

  • 执行表恢复操作时:
    • 如果表存在,您需要具备Table的Update权限。
    • 如果表不存在,您需要具备Project的CreateTable权限。

      权限详情请参见MaxCompute权限

    说明 不支持聚簇表(cluster table)。
  • 执行分区恢复操作时,只能指定1个LSN,即备份版本。

    当一次性恢复多个分区时,MaxCompute会将每个分区都恢复至指定LSN。如果某个分区不存在指定的LSN,MaxCompute会将该分区恢复至其第1个LSN。

    例如,pt1的LSN分别为100、102、104和106,pt2的LSN分别为101、103、104和105,执行命令如下。
    restore table table_name PARTITION(pt='1') PARTITION(pt='2') to LSN '102';
    执行结果为:pt1恢复至102版本,pt2恢复至101版本。
restore table table_name to LSN 'xxxx';恢复表至指定版本。

您可以通过show history for table <table_name>;命令获取表的版本信息。

restore table table_name to LSN 'xxxx' as new_table_name;恢复表至指定版本,并命名为新表或将数据更新到不同名的表中。
restore table table_name PARTITION('id'='xxxx')[PARTITION('id'='xxxx')];恢复已删除的指定分区,支持一次恢复多个分区。通常用于恢复因执行drop partition操作或被生命周期回收后,需要恢复的分区。
restore table table_name partition_spec1[partition_spec2 ]to LSN 'xxxx';恢复指定分区至指定版本,支持一次恢复多个分区。通常用于恢复因执行overwritemerge操作后,需要恢复的分区。
restore table table_name partition_spec1[partition_spec2 ]to LSN 'xxxx' as new_table_name;恢复指定分区至指定版本,并命名为新表。

查看备份数据示例

以test_restore项目为例,为您介绍如何查看表的备份数据。

  • 查看所有表的备份数据。

    执行show history for tables;命令,示例如下。

    odps@ test_restore>show history for tables;
    返回结果如下。
    Name                   Id                                  Type                IsPartitioned   CreateTime          DropTime
    partition_table_1      2815aee9cab74881975705789a01d7ae    MANAGED_TABLE       TRUE            2020-02-14 12:42:01
    test_restore_part_y    1d42e7d9e0044d389776ccbd3a8ad7ae    MANAGED_TABLE       TRUE            2020-01-27 16:01:50 2020-01-27 18:16:49
  • 查看指定表的备份数据。

    执行show history for table <table_name>;命令,示例如下。

    ## 创建表test_restore_x。
    odps@ test_restore>Create Table test_restore_x(a string);
    ## 更新表test_restore_x数据。
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_x values("0");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_x values("1");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_x values("2");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_x values("3");
    ## 查看表test_restore_x的备份数据。
    odps@ test_restore>show history for table test_restore_x;
    返回结果如下。
    ObjectType  ObjectId                            ObjectName         LSN               Time                Operation
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000001  2020-02-18 14:17:58 CREATE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000002  2020-02-18 14:22:26 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000003  2020-02-18 14:23:32 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000004  2020-02-18 14:24:37 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000005  2020-02-18 14:25:44 OVERWRITE
  • 查看已删除表的备份数据。

    执行show history for table table_name ('id'='xxxx');命令,示例如下。

    ## 删除表test_restore_x。
    odps@ test_restore>drop table test_restore_x;
    ## 确认删除表test_restore_x操作。
    Confirm to "drop table test_restore_x;" (yes/no)? yes
    ## 查看删除表test_restore_x的备份数据。
    odps@ test_restore>show history for table test_restore_x('id'='d6266b2c49b9418cb999dc65c10ad7ae');
    返回结果如下。
    ObjectType  ObjectId                            ObjectName         LSN                 Time                Operation
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000001    2020-02-18 14:17:58 CREATE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000002    2020-02-18 14:22:26 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000003    2020-02-18 14:23:32 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000004    2020-02-18 14:24:37 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000005    2020-02-18 14:25:44 OVERWRITE
    TABLE       d6266b2c49b9418cb999dc65c10ad7ae    test_restore_x     0000000000000006    2020-02-18 14:30:32 DROP
  • 查看分区表或分区的备份数据。

    执行show history for table table_name ('id'='xxxx');命令,查看分区表的备份数据。执行show history for table table_name partition_spec;show history for table table_name PARTITION('id'='xxxx');命令,查看分区的备份数据。

    查看分区表的备份数据,示例如下。
    ## 新建表test_restore_part_x。
    odps@ test_restore>Create Table test_restore_part_x(a string) PARTITIONED BY(ds string);
    ## 更新表test_restore_part_x。
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191201") values ("1");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191202") values ("2");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191203") values ("3");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191204") values ("4");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191205") values ("5");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191205") values ("6");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20200101") values ("20200101");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20200102") values ("20200102");
    ## 查看表test_restore_part_x的备份数据。
    odps@ test_restore>show history for table test_restore_part_x('id'='94d436523fe14ba39f33d2dee738c018');
    返回结果如下。
    ObjectType  ObjectId                            ObjectName             LSN                 Time                Operation
    TABLE       94d436523fe14ba39f33d2dee738c018    test_restore_part_x    0000000000000001    2020-02-18 17:29:35 CREATE
    PARTITION   f4614a34620346aaa729761f082aae74    ds=20191201            0000000000000002    2020-02-18 17:32:56 CREATE
    PARTITION   0698ed40169044c7bf66b14a3c3c2f35    ds=20191202            0000000000000003    2020-02-18 17:35:12 CREATE
    PARTITION   19f26f7b1976438c94f8f53cfb5c6912    ds=20191203            0000000000000004    2020-02-18 17:35:22 CREATE
    PARTITION   dc15ed7d5da441649a5f32c4929b2fb2    ds=20191204            0000000000000005    2020-02-18 17:35:57 CREATE
    PARTITION   e01128f1183b44369b06dae1e73a8134    ds=20191205            0000000000000006    2020-02-18 17:37:21 CREATE
    PARTITION   e01128f1183b44369b06dae1e73a8134    ds=20191205            0000000000000007    2020-02-18 17:37:48 OVERWRITE
    PARTITION   189727214c0d4e8e92b52814211dd086    ds=20200101            0000000000000008    2020-02-18 17:37:59 CREATE
    PARTITION   adbc79ade65d4b0dbea4a4dcbf0ce719    ds=20200102            0000000000000009    2020-02-18 17:38:09 CREATE
    查看分区的备份数据,示例如下。
    ## 查看表test_restore_part_x指定分区的备份数据。
    odps@ test_restore>show history for table test_restore_part_x('id'='94d436523fe14ba39f33d2dee738c018') partition(ds='20191201') partition(ds='20191202');
    返回结果如下。
    ObjectType  ObjectId                            ObjectName           LSN                 Time                Operation
    PARTITION   f4614a34620346aaa729761f082aae74    ds=20191201          0000000000000002    2020-02-18 17:32:56 CREATE
    PARTITION   0698ed40169044c7bf66b14a3c3c2f35    ds=20191202          0000000000000003    2020-02-18 17:35:12 CREATE

恢复非分区表示例

以test_restore项目为例,为您介绍如何恢复非分区表的数据。

  • 恢复已删除的表。

    执行restore table table_name ('id'='xxxxx');命令,示例如下。如果存在同名的表,您需要将同名的表重命名后才能执行恢复操作。

    ## 查询已删除表test_restore_x的备份数据。
    odps@ test_restore>show history for table test_restore_x('id'='d6266b2c49b9418cb999dc65c10ad7ae');
    ## 创建同名表test_restore_part_x。
    odps@ test_restore>Create Table test_restore_x(a string);
    ## 恢复表test_restore_part_x,但因为存在同名表会报错。
    odps@ test_restore>restore table test_restore_x('id'='d6266b2c49b9418cb999dc65c10ad7ae');
    ## 重命名存在的同名表test_restore_part_x。
    odps@ test_restore>alter table test_restore_x rename to test_restore_x_rename;
    ## 恢复已删除的表test_restore_part_x。
    odps@ test_restore>restore table test_restore_x('id'='d6266b2c49b9418cb999dc65c10ad7ae');
    返回结果显示OK
  • 恢复表至指定版本。

    执行restore table table_name to LSN 'xxxx';命令,示例如下。

    ## 恢复表test_restore_x至指定版本。
    odps@ test_restore>restore table test_restore_x to LSN '0000000000000004';
    ## 查询表test_restore_part_x数据。
    odps@ test_restore>select * from test_restore_x;
    返回结果如下。
    Summary:
    +---+
    | a |
    +---+
    | 2 |
    +---+
  • 恢复表至指定版本,并命名为新表或将数据更新到不同名的表中。

    执行restore table table_name to LSN 'xxxx' as new_table_name;命令,示例如下。

    包含以下三种场景:
    • 恢复表至指定版本,并命名为新表。
      ## 恢复已删除的表test_restore_x至指定版本,并命名为新表。
      odps@ test_restore>restore table test_restore_x to LSN '0000000000000005' as test_restore_x_v5;
      ## 查询表test_restore_x_v5数据。
      odps@ test_restore>select * from test_restore_x_v5;
      返回结果如下。
      Summary:
      +---+
      | a |
      +---+
      | 3 |
      +---+
    • 恢复表至指定版本,并将数据更新到已存在的不同名表中。
      ## 恢复表test_restore_x至指定版本,并更新到已存在的不同名表中。
      odps@ test_restore>restore table test_restore_x to LSN '0000000000000005' as test_restore_x_v5;
      ## 查询表test_restore_x_v5的备份数据。
      odps@ test_restore>show history for table test_restore_x_v5;
      返回结果如下。
      ObjectType  ObjectId                            ObjectName            LSN                 Time                Operation
      TABLE       2e17a61561e4456db8ab66d0dd41e1b9    test_restore_x_v5     0000000000000001    2020-02-18 16:18:41 CREATE
      TABLE       2e17a61561e4456db8ab66d0dd41e1b9    test_restore_x_v5     0000000000000003    2020-02-18 16:22:07 OVERWRITE
    • 恢复表至指定版本,并将数据更新到不同名且Schema不一致的表中。该操作执行失败,两个表的Schema必须要保持一致。示例如下。
      ## 创建一个Schema不一致的表。
      odps@ test_restore>Create Table test_restore_2cols(a string, b string);
      ## 恢复表test_restore_x数据至指定版本,并将数据更新到test_restore_2cols表中。
      odps@ test_restore>restore table test_restore_x to LSN '0000000000000005' as test_restore_2cols;
      返回结果如下。
      FAILED: Catalog Service Failed, ErrorCode: 105, Error Message: ODPS-0110061: Failed to run ddltask - Restore table failed because: field schema not same, [{"comment":"","id":"","name":"a","type":"string"}] vs [{"comment":"","id":"","name":"a","type":"string"},{"comment":"","id":"","name":"b","type":"string"}]

恢复分区表和分区示例

以test_restore项目为例,为您介绍如何恢复分区表或分区的数据。

  • 恢复分区表。

    执行restore table table_name ('id'='xxxxx');命令,示例如下。

    ## 新建表test_restore_part_x。
    odps@ test_restore>Create Table test_restore_part_x(a string) PARTITIONED BY(ds string);
    ## 更新表test_restore_part_x。
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191201") values ("1");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191202") values ("2");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191203") values ("3");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191204") values ("4");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191205") values ("5");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20191205") values ("6");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20200101") values ("20200101");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_x partition(ds="20200102") values ("20200102");
    ## 查看表test_restore_part_x的分区。
    odps@ test_restore>list partitions test_restore_part_x;
    ## 查看表test_restore_part_x的数据。
    odps@ test_restore>select * from test_restore_part_x;
    ## 删除表test_restore_part_x。
    odps@ test_restore>drop table test_restore_part_x;
    ## 确认删除表test_restore_part_x操作。
    Confirm to "drop table test_restore_part_x;" (yes/no)? yes
    ## 恢复表test_restore_part_x。
    odps@ test_restore>restore table test_restore_part_x('id'='94d436523fe14ba39f33d2dee738c018');
    ## 查看表test_restore_part_x的备份数据。
    odps@ test_restore>show history for table test_restore_part_x('id'='94d436523fe14ba39f33d2dee738c018');
    ## 查看表test_restore_part_x的分区。
    odps@ test_restore>list partitions test_restore_part_x;
    返回结果如下。
    ds=20191201
    ds=20191202
    ds=20191203
    ds=20191204
    ds=20191205
    ds=20200101
    ds=20200102
  • 恢复分区。

    执行restore table table_name PARTITION('id'='xxxx')[PARTITION('id'='xxxx')];命令,示例如下。

    ## 新建表test_restore_part_y。
    odps@ test_restore>Create Table test_restore_part_y(a string) PARTITIONED BY(ds string);
    ## 更新表test_restore_part_y。
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20191201") values ("1");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20191202") values ("2");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20191203") values ("3");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20191204") values ("4");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20191205") values ("5");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20191206") values ("6");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20200101") values ("20200101");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20200102") values ("20200102");
    ## 查看表test_restore_part_y的分区。
    odps@ test_restore>list partitions test_restore_part_y;
    ## 删除表test_restore_part_y的分区。
    odps@ test_restore>alter table test_restore_part_y drop partition(ds='20191201'),partition(ds='20191202');
    ## 确认删除分区操作。
    Confirm to "alter table test_restore_part_y drop partition(ds='20191201'),partition(ds='20191202');" (yes/no)? yes
    ## 查看表test_restore_part_y的分区。
    odps@ test_restore>list partitions test_restore_part_y;
    ## 恢复表test_restore_part_y的分区。
    odps@ test_restore>restore table test_restore_part_y partition('id'='e6647109adbe44b69068a4dd83a577ad') partition('id'='bc4aaf375ab94998b02dabb0fed0b5fe');
    ## 查看表test_restore_part_y的分区。
    odps@ test_restore>list partitions test_restore_part_y;
    返回结果如下。
    ds=20191201
    ds=20191202
    ds=20191203
    ds=20191204
    ds=20191205
    ds=20191206
    ds=20200101
    ds=20200102
  • 恢复分区至指定版本。

    执行restore table table_name partition_spec1[partition_spec2 ]to LSN 'xxxx';命令,示例如下。

    ## 更新分区表test_restore_part_y。
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20200101") values ("20200101_v1");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20200102") values ("20200102_v1");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20200101") values ("20200101_v2");
    odps@ test_restore>INSERT OVERWRITE TABLE test_restore_part_y partition(ds="20200102") values ("20200102_v2");
    ## 查看test_restore_part_y指定分区的数据。
    odps@ test_restore>select * from test_restore_part_y where ds='20200101' or ds='20200102';
    ## 恢复test_restore_part_y指定分区至指定版本。
    odps@ test_restore>restore table test_restore_part_y partition(ds='20200101') partition(ds='20200102') to LSN '0000000000000010';
    ## 查看test_restore_part_y指定分区的数据。
    odps@ test_restore>select * from test_restore_part_y where ds='20200101' or ds='20200102';
    返回结果如下。
    +---+----+
    | a | ds |
    +---+----+
    | 20200101| 20200101|
    | 20200102| 20200102|
    +---+----+
  • 恢复分区至指定版本,并命名为新表。

    执行restore table table_name partition_spec1[partition_spec2 ]to LSN 'xxxx' as new_table_name;命令,示例如下。

    ## 恢复分区至指定版本,并命名为新表test_restore_part_y_v10。
    odps@ test_restore>restore table test_restore_part_y partition(ds='20200101') partition(ds='20200102') to LSN '0000000000000010' as test_restore_part_y_v10;
    ## 查看新表test_restore_part_y_v10的数据。
    odps@ test_restore>select * from test_restore_part_y_v10;
    返回结果如下。
    +---+----+
    | a | ds |
    +---+----+
    
    | 20200101| 20200101|
    | 20200102| 20200102|
    +---+----+