All Products
Search
Document Center

ApsaraDB RDS:Migrate full backups to the cloud

Last Updated:Aug 26, 2026

ApsaraDB RDS for SQL Server provides a solution that allows you to migrate self-managed SQL Server databases to Alibaba Cloud. You need to only upload the full backup data of your self-managed SQL Server database to Alibaba Cloud Object Storage Service (OSS) and then import the full backup data to the specified ApsaraDB RDS for SQL Server database from the ApsaraDB RDS console. This solution is suitable for scenarios such as data backup, data migration, and disaster recovery.

Prerequisites

  • The ApsaraDB RDS for SQL Server instance must meet the following requirements:

    • The remaining storage space of the instance must be greater than the size of the data files to be migrated. If the storage space is insufficient, upgrade the storage of the instance in advance.

    • If the instance runs SQL Server 2012 or later, or SQL Server 2008 R2 with cloud disks, make sure that the instance does not contain a database that has the same name as the database to be migrated.

    • If the instance runs SQL Server 2008 R2 with high-performance local disks, make sure that a database that has the same name as the database to be migrated is created on the instance.

  • If you log on as a RAM user, the following requirements must be met:

    • The RAM user is granted the AliyunOSSFullAccess and AliyunRDSFullAccess permissions. For more information about how to grant permissions to a RAM user, see Manage OSS permissions by using RAM and Manage ApsaraDB RDS permissions by using RAM.

    • Your Alibaba Cloud account (primary account) has authorized the ApsaraDB RDS official service account to access your OSS resources.

      Click to view the authorization method

      1. Go to the Restoration page of the ApsaraDB RDS instance, and then click Restore Backup Data from OSS.

      2. On the Import Guide page, click Next twice to go to the 3. Import Data step.

        If You have authorized the ApsaraDB RDS official service account to access your OSS resources appears in the lower-left corner of the page, the authorization is complete. Otherwise, the authorization is not complete. In this case, click Authorization URL on the page to grant the authorization.

        image

    • Manually create a permission policy in your Alibaba Cloud account (primary account), and then attach the policy to the RAM user.

      Click to view the policy content

      {
          "Version": "1",
          "Statement": [
              {
                  "Action": [
                      "ram:GetRole"
                  ],
                  "Resource": "acs:ram:*:*:role/AliyunRDSImportRole",
                  "Effect": "Allow"
              }
          ]
      }

Notes

  • Migration scope: This solution supports only the migration of a single database (database level). To migrate multiple databases or all databases, use the instance-level migration solution.

  • Version compatibility: A backup file that is created in a self-managed SQL Server environment cannot be migrated to an ApsaraDB RDS for SQL Server instance that runs a lower version.

  • Permission management : After you grant the ApsaraDB RDS service account the permissions to access OSS, the system creates a role named AliyunRDSImportRole in RAM. Do not modify or delete the role. Otherwise, migration tasks fail . If you accidentally modify or delete the role, grant the permissions again in the data migration wizard.

  • Account management: After the migration is complete, the accounts of the original database become unavailable. Create accounts again in the ApsaraDB RDS console.

  • OSS file retention: Before the migration task is complete, do not delete the backup file from OSS. Otherwise, the task fails.

  • Backup file requirements:

    • File name: The file name cannot contain special characters such as !@#$%^&*()_+-=. Otherwise, the migration fails.

    • File extension: The backup files supported by ApsaraDB RDS include .bak (full backup), .diff (differential backup), and .trn or .log (log backup) files. Files of other types cannot be recognized.

    • File type: Only full backup files can be uploaded. Differential backup files and log backup files are not supported.

    • File source: If the source data is a full backup file of ApsaraDB RDS for SQL Server that was previously downloaded (in .zip format by default), decompress it into a .bak file before you migrate the data to the cloud.

Billing

Only OSS-related fees are incurred in this solution. For more information, see the following:

image

Scenario

Billing

Upload the backup file of the self-managed database to OSS

Free of charge.

Store the backup file in OSS

OSS storage fees are charged. For more information about billing, see OSS pricing.

Migrate the backup file from OSS to ApsaraDB RDS

  • If you migrate the backup file to ApsaraDB RDS over the internal network, no fees are charged.

  • If you migrate the backup file over the Internet, OSS outbound traffic fees are charged. For more information about billing, see OSS pricing.

Preparation

Run the DBCC CHECKDB statement in the self-managed database environment to make sure that the database does not contain allocation errors or consistency errors. If the database is in the normal state, the following result is returned:

...
CHECKDB found 0 allocation errors and 0 consistency errors in database 'xxx'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

1. Back up the self-managed database

Select a solution based on the version of your ApsaraDB RDS for SQL Server instance.

SQL Server 2012 or later, or SQL Server 2008 R2 with cloud disks

Note

Before you create a full backup for the self-managed database, make sure that data writes are stopped. Data that is written during the backup process is not included in the backup file.

  1. Run the following command to check the current recovery model of the source database:

    SELECT
        name AS DatabaseName,
        recovery_model_desc AS RecoveryModel,
        state_desc AS State
    FROM sys.databases
    ORDER BY name;

    Before you create a production backup for migration to ApsaraDB RDS, change the recovery model of the database to FULL:

    ALTER DATABASE [db_simple] SET RECOVERY FULL WITH NO_WAIT;
  2. Download the backup script, and then open the backup script in SQL Server Management Studio (SSMS).

  3. Modify the following parameters in the SELECT statement of the script. The parameters are located below YOU HAVE TO INIT PUBLIC VARIABLES HERE in the script.

    Parameter

    Description

    @backup_databases_list

    The databases that you want to back up. Separate multiple databases with semicolons (;) or commas (,).

    @backup_type

    The backup type. Valid values:

    • FULL: full backup.

    • DIFF: differential backup.

    • LOG: log backup.

    @backup_folder

    The local directory in which the backup files are stored. If the directory does not exist, the directory is automatically created.

    @is_run

    Specifies whether to perform the backup. Valid values:

    • 1: performs the backup.

    • 0: performs only a check and does not perform the backup.

  4. Run the backup script.

SQL Server 2008 R2 with high-performance local disks

  1. Open Microsoft SQL Server Management Studio (SSMS).

  2. Log on to the database that you want to migrate.

  3. Run the following command to check the current recovery model of the source database:

    USE master;
    GO
    SELECT name, CASE recovery_model
    WHEN 1 THEN 'FULL'
    WHEN 2 THEN 'BULK_LOGGED'
    WHEN 3 THEN 'SIMPLE' END model FROM sys.databases
    WHERE name NOT IN ('master','tempdb','model','msdb');
    GO
    • If the model value in the query result is not FULL, perform Step 4.

    • If the model value in the query result is FULL, perform Step 5.

  4. Run the following command to set the recovery model of the source database to FULL:

    ALTER DATABASE [dbname] SET RECOVERY FULL;
    GO
    ALTER DATABASE [dbname] SET AUTO_CLOSE OFF;
    GO
    Important

    After the recovery model is set to FULL, more SQL Server logs are generated. Make sure that sufficient disk space is available.

  5. Run the following command to back up the source database.

    In this example, the dbtest database is backed up to the backup.bak file.

    USE master;
    GO
    BACKUP DATABASE [dbtest] to disk ='d:\backup\backup.bak' WITH COMPRESSION,INIT;
    GO
  6. Run the following command to verify the integrity of the backup file:

    USE master
     GO
     RESTORE FILELISTONLY 
       FROM DISK = N'D:\backup\backup.bak';
    Important
    • If a result set is returned, the backup file is valid.

    • If an error is reported, perform the backup again.

  7. Optional: Run the following command to restore the recovery model of the database:

    Important

    If the recovery model of the database is already FULL, skip this step.

    ALTER DATABASE [dbname] SET RECOVERY SIMPLE;
    GO

2. Upload the backup file to OSS

Select a solution based on the version of your ApsaraDB RDS for SQL Server instance.

SQL Server 2012 or later, or SQL Server 2008 R2 with cloud disks

  1. Before you upload backup files to OSS, you must create a bucket in OSS.

    • If a bucket already exists in OSS, make sure that the bucket meets the following requirements:

      • The storage class of the bucket is Standard. The IA, Archive, Cold Archive, and Deep Cold Archive storage classes are not supported.

      • Server-side encryption is not enabled for the bucket.

    • If no bucket exists in OSS, create a bucket first. (Make sure that OSS is activated.)

      1. Log on to the OSS console, click Buckets, and then click Create bucket.

      2. Configure the following key parameters and keep the default values for other parameters.

        Important
        • The bucket is used only for this data migration, so you need to only configure the key parameters. After the migration is complete, delete the bucket in a timely manner to prevent data leaks and additional fees.

        • Do not enable server-side encryption when you create the bucket.

        Parameter

        Description

        Example

        Bucket Name

        The name of the bucket. The name must be globally unique and cannot be changed after the bucket is created.

        Naming rules:

        • The name can contain only lowercase letters, digits, and hyphens (-).

        • The name must start and end with a lowercase letter or a digit.

        • The name must be 3 to 63 characters in length.

        migratetest

        Region

        The region in which the bucket resides. If you upload data to the bucket from an ECS instance over the internal network and restore the data to the ApsaraDB RDS instance over the internal network, make sure that the ECS instance, bucket, and ApsaraDB RDS instance reside in the same region.

        China (Hangzhou)

        Storage Type

        Select Standard. The migration operation described in this topic does not support buckets of other storage classes.

        Standard

  2. Upload the backup file to OSS.

    After the backup of the self-managed database is complete, upload the backup file to an OSS bucket that resides in the same region as your ApsaraDB RDS instance. If the bucket and the ApsaraDB RDS instance reside in the same region, the bucket and the instance can communicate over the internal network. In this case, no Internet traffic fees are charged and data is uploaded at a faster speed. You can use one of the following methods:

    Upload files by using ossbrowser (recommended)

    1. Download ossbrowser.

    2. In this example, Windows x64 is used. Decompress the downloaded oss-browser-win32-x64.zip package, and then double-click the oss-browser.exe application.

    3. Select AK as the logon method, configure the Access Key ID and Access Key Secret parameters, keep the default values for other parameters, and then click Log On.

      Note

      An AccessKey is used for identity authentication to ensure data security. Keep your AccessKey confidential.

      Log on to ossbrowser

    4. Click the destination bucket to go to the bucket.Open the bucket

    5. Click Upload icon, select the backup file that you want to upload, and then click Open. The local file is uploaded to OSS.

    Upload files by using the OSS console

    Note

    If the backup file is smaller than 5 GB in size, upload the backup file directly in the OSS console.

    1. Log on to the OSS console.

    2. Click Buckets, and then click the name of the destination bucket.Open the bucket in the console

    3. In the Files, click Upload File.Upload a file in the console

    4. Drag the backup file to the Files to Upload section, or click Select Files and select the backup file that you want to upload.Select files to upload

    5. Click Upload File at the bottom of the page. The local backup file is uploaded to OSS.

    Upload files by using the OSS API (Python 3 project example)

    Note

    If the backup file is larger than 5 GB in size, call OSS API operations to upload the backup file to the OSS bucket by using multipart upload.

    # -*- coding: utf-8 -*-
    """
    Alibaba Cloud OSS Python SDK v2
    Dependency: pip install alibabacloud-oss-v2
    """
    
    import os
    import sys
    from pathlib import Path
    import alibabacloud_oss_v2 as oss
    from alibabacloud_oss_v2 import exceptions as oss_ex
    
    
    def get_client_from_env(region: str, endpoint: str | None = None) -> oss.Client:
        """
        Create a v2 client from environment variables.
        - Prioritize using Region (recommended), but also support custom Endpoints (optional).
        - Compatible with both AK and STS:
            * AK: Requires OSS_ACCESS_KEY_ID / OSS_ACCESS_KEY_SECRET
            * STS: Also requires OSS_SESSION_TOKEN (compatible with the old variable OSS_SECURITY_TOKEN)
        """
        # Compatibility: If the user uses the old variable OSS_SECURITY_TOKEN, map it to the v2 expected OSS_SESSION_TOKEN
        sec_token_legacy = os.getenv("OSS_SECURITY_TOKEN")
        if sec_token_legacy and not os.getenv("OSS_SESSION_TOKEN"):
            os.environ["OSS_SESSION_TOKEN"] = sec_token_legacy
    
        ak = os.getenv("OSS_ACCESS_KEY_ID")
        sk = os.getenv("OSS_ACCESS_KEY_SECRET")
        st = os.getenv("OSS_SESSION_TOKEN")  # STS Token (optional)
    
        if not (ak and sk):
            raise ValueError("No valid AK found. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables. "
                             "If using STS, also set OSS_SESSION_TOKEN (or the old name OSS_SECURITY_TOKEN).")
    
        # Indicate the type of credential used
        if st:
            print("STS Token (OSS_SESSION_TOKEN) detected. Using STS credentials.")
        else:
            print("No STS Token detected. Using AccessKey (AK) credentials.")
    
        credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
        cfg = oss.config.load_default()
        cfg.credentials_provider = credentials_provider
    
        # Basic network configuration
        cfg.region = region  # Example: 'cn-hangzhou'
        if endpoint:
            # Optional: Custom Endpoint (e.g., internal network, accelerated, dedicated domain)
            cfg.endpoint = endpoint
    
        # You can also add other configurations here, such as: cfg.use_accelerate_endpoint = True
        return oss.Client(cfg)
    
    
    def resumable_upload_file_v2(
        client: oss.Client,
        bucket_name: str,
        object_key: str,
        file_path: str,
        part_size: int = 1 * 1024 * 1024,
        parallel_num: int = 4,
        checkpoint_dir: str | None = None,
    ):
        """
        Implement concurrent multipart upload with resumable upload.
    
        :param client: Initialized oss.Client
        :param bucket_name: Destination bucket name
        :param object_key: Destination object key (without bucket name)
        :param file_path: Full path of the local file
        :param part_size: Part size in bytes, default is 1 MB
        :param parallel_num: Number of concurrent upload threads, default is 4
        :param checkpoint_dir: Directory to store breakpoint information; if None, resumable upload is disabled
        """
        file_path = str(file_path)
        if not Path(file_path).exists():
            raise FileNotFoundError(f"Error: Local file not found. Check the file_path configuration: {file_path}")
    
        # Construct the Uploader; enable resumable upload based on whether checkpoint_dir is provided
        if checkpoint_dir:
            uploader = client.uploader(
                enable_checkpoint=True,
                checkpoint_dir=checkpoint_dir,
                part_size=part_size,
                parallel_num=parallel_num,
            )
        else:
            uploader = client.uploader(
                part_size=part_size,
                parallel_num=parallel_num,
            )
    
        print(f"Starting to upload file: {file_path}")
        print(f"Destination Bucket: {bucket_name}")
        print(f"Destination Object: {object_key}")
        print(f"Part size: {part_size} bytes, Concurrency: {parallel_num}")
        if checkpoint_dir:
            print(f"Resumable upload: Enabled (checkpoint_dir={checkpoint_dir})")
        else:
            print("Resumable upload: Disabled (set checkpoint_dir to enable)")
    
        # Execute the upload (Uploader automatically chooses between multi/single part concurrent upload based on size)
        result = uploader.upload_file(
            oss.PutObjectRequest(bucket=bucket_name, key=object_key),
            filepath=file_path,
        )
    
        print("-" * 30)
        print("File uploaded successfully!")
        print(f"HTTP Status: {result.status_code}")
        print(f"ETag: {result.etag}")
        print(f"Request ID: {result.request_id}")
        # CRC-64 checksum; v2 enables data validation by default
        print(f"CRC64: {result.hash_crc64}")
        print("-" * 30)
    
    
    def main():
        # Before running the code example, make sure you have set the corresponding environment variables.
        # macOS/Linux:
        #   AK method:
        #     export OSS_ACCESS_KEY_ID=YOUR_AK_ID
        #     export OSS_ACCESS_KEY_SECRET=YOUR_AK_SECRET
        #   STS method:
        #     export OSS_ACCESS_KEY_ID=YOUR_STS_ID
        #     export OSS_ACCESS_KEY_SECRET=YOUR_STS_SECRET
        #     export OSS_SECURITY_TOKEN=YOUR_STS_TOKEN
        #
        # Windows:
        #   Powershell: $env:OSS_ACCESS_KEY_ID="YOUR_AK_ID"
        #   cmd: set OSS_ACCESS_KEY_ID=YOUR_AK_ID
    
        # ===================== Parameters (modify as needed) =====================
        # Region example: 'cn-hangzhou'; we recommend using Region first
        region = "cn-hangzhou"
    
        # Optional: Custom Endpoint (for internal network, dedicated domain, accelerated domain name, etc.)
        # Example: 'https://oss-cn-hangzhou.aliyuncs.com'
        endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
    
        # Bucket and Object
        bucket_name = "examplebucket"
        object_key = "test.bak"
    
        # Full path of the local file to upload.
        # Windows example: r'D:\localpath\examplefile.txt'  (note the r at the beginning)
        # macOS/Linux example: '/Users/test/examplefile.txt'
        file_path = r"D:\oss\test.bak"
    
        # Sharding and concurrency
        part_size = 1 * 1024 * 1024  # Default is 1 MB; OSS requires a minimum part size of 100 KB
        parallel_num = 4
    
        # Resumable upload directory (pass None to disable; we recommend specifying a writable directory)
        checkpoint_dir = str(Path.cwd() / ".oss_checkpoints")
        # =================== End of parameters ===================
    
        print("Script execution starts...")
        try:
            client = get_client_from_env(region=region, endpoint=endpoint)
            # If resumable upload is enabled, make sure the directory exists
            if checkpoint_dir:
                Path(checkpoint_dir).mkdir(parents=True, exist_ok=True)
    
            resumable_upload_file_v2(
                client=client,
                bucket_name=bucket_name,
                object_key=object_key,
                file_path=file_path,
                part_size=part_size,
                parallel_num=parallel_num,
                checkpoint_dir=checkpoint_dir,
            )
        except FileNotFoundError as e:
            print(e)
        except oss_ex.ServiceError as e:
            # Error returned by the OSS server
            print("\nAn OSS server-side error occurred.")
            print(f"HTTP Status: {getattr(e, 'status_code', 'N/A')}")
            print(f"Error Code: {getattr(e, 'code', 'N/A')}")
            print(f"Message: {getattr(e, 'message', 'N/A')}")
            print(f"Request ID: {getattr(e, 'request_id', 'N/A')}")
            print(f"Endpoint: {getattr(e, 'request_target', 'N/A')}")
        except oss_ex.BaseError as e:
            # SDK local/serialization/deserialization/credential errors
            print("\nAn OSS SDK client-side error occurred.")
            print(str(e))
        except Exception as e:
            print(f"\nAn unknown error occurred: {e}")
    
    
    if __name__ == "__main__":
        main()

SQL Server 2008 R2 with high-performance local disks

  1. Before you upload backup files to OSS, you must create a bucket in OSS.

    • If a bucket already exists in OSS, make sure that the bucket meets the following requirements:

      • The storage class of the bucket is Standard. The IA, Archive, Cold Archive, and Deep Cold Archive storage classes are not supported.

      • Server-side encryption is not enabled for the bucket.

    • If no bucket exists in OSS, create a bucket first. (Make sure that OSS is activated.)

      1. Log on to the OSS console, click Buckets, and then click Create bucket.

      2. Configure the following key parameters and keep the default values for other parameters.

        Important
        • The bucket is used only for this data migration, so you need to only configure the key parameters. After the migration is complete, delete the bucket in a timely manner to prevent data leaks and additional fees.

        • Do not enable server-side encryption when you create the bucket.

        Parameter

        Description

        Example

        Bucket Name

        The name of the bucket. The name must be globally unique and cannot be changed after the bucket is created.

        Naming rules:

        • The name can contain only lowercase letters, digits, and hyphens (-).

        • The name must start and end with a lowercase letter or a digit.

        • The name must be 3 to 63 characters in length.

        migratetest

        Region

        The region in which the bucket resides. If you upload data to the bucket from an ECS instance over the internal network and restore the data to the ApsaraDB RDS instance over the internal network, make sure that the ECS instance, bucket, and ApsaraDB RDS instance reside in the same region.

        China (Hangzhou)

        Storage Type

        Select Standard. The migration operation described in this topic does not support buckets of other storage classes.

        Standard

  2. Upload the backup file to OSS.

    After the backup of the self-managed database is complete, upload the backup file to an OSS bucket that resides in the same region as your ApsaraDB RDS instance. If the bucket and the ApsaraDB RDS instance reside in the same region, the bucket and the instance can communicate over the internal network. In this case, no Internet traffic fees are charged and data is uploaded at a faster speed. You can use one of the following methods:

    Upload files by using ossbrowser (recommended)

    1. Download ossbrowser.

    2. In this example, Windows x64 is used. Decompress the downloaded oss-browser-win32-x64.zip package, and then double-click the oss-browser.exe application.

    3. Select AK as the logon method, configure the Access Key ID and Access Key Secret parameters, keep the default values for other parameters, and then click Log On.

      Note

      An AccessKey is used for identity authentication to ensure data security. Keep your AccessKey confidential.

      Log on to ossbrowser

    4. Click the destination bucket to go to the bucket.Open the bucket

    5. Click Upload icon, select the backup file that you want to upload, and then click Open. The local file is uploaded to OSS.

    Upload files by using the OSS console

    Note

    If the backup file is smaller than 5 GB in size, upload the backup file directly in the OSS console.

    1. Log on to the OSS console.

    2. Click Buckets, and then click the name of the destination bucket.Open the bucket in the console

    3. In the Files, click Upload File.Upload a file in the console

    4. Drag the backup file to the Files to Upload section, or click Select Files and select the backup file that you want to upload.Select files to upload

    5. Click Upload File at the bottom of the page. The local backup file is uploaded to OSS.

    Upload files by using the OSS API (Python 3 project example)

    Note

    If the backup file is larger than 5 GB in size, call OSS API operations to upload the backup file to the OSS bucket by using multipart upload.

    # -*- coding: utf-8 -*-
    """
    Alibaba Cloud OSS Python SDK v2
    Dependency: pip install alibabacloud-oss-v2
    """
    
    import os
    import sys
    from pathlib import Path
    import alibabacloud_oss_v2 as oss
    from alibabacloud_oss_v2 import exceptions as oss_ex
    
    
    def get_client_from_env(region: str, endpoint: str | None = None) -> oss.Client:
        """
        Create a v2 client from environment variables.
        - Prioritize using Region (recommended), but also support custom Endpoints (optional).
        - Compatible with both AK and STS:
            * AK: Requires OSS_ACCESS_KEY_ID / OSS_ACCESS_KEY_SECRET
            * STS: Also requires OSS_SESSION_TOKEN (compatible with the old variable OSS_SECURITY_TOKEN)
        """
        # Compatibility: If the user uses the old variable OSS_SECURITY_TOKEN, map it to the v2 expected OSS_SESSION_TOKEN
        sec_token_legacy = os.getenv("OSS_SECURITY_TOKEN")
        if sec_token_legacy and not os.getenv("OSS_SESSION_TOKEN"):
            os.environ["OSS_SESSION_TOKEN"] = sec_token_legacy
    
        ak = os.getenv("OSS_ACCESS_KEY_ID")
        sk = os.getenv("OSS_ACCESS_KEY_SECRET")
        st = os.getenv("OSS_SESSION_TOKEN")  # STS Token (optional)
    
        if not (ak and sk):
            raise ValueError("No valid AK found. Set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables. "
                             "If using STS, also set OSS_SESSION_TOKEN (or the old name OSS_SECURITY_TOKEN).")
    
        # Indicate the type of credential used
        if st:
            print("STS Token (OSS_SESSION_TOKEN) detected. Using STS credentials.")
        else:
            print("No STS Token detected. Using AccessKey (AK) credentials.")
    
        credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
        cfg = oss.config.load_default()
        cfg.credentials_provider = credentials_provider
    
        # Basic network configuration
        cfg.region = region  # Example: 'cn-hangzhou'
        if endpoint:
            # Optional: Custom Endpoint (e.g., internal network, accelerated, dedicated domain)
            cfg.endpoint = endpoint
    
        # You can also add other configurations here, such as: cfg.use_accelerate_endpoint = True
        return oss.Client(cfg)
    
    
    def resumable_upload_file_v2(
        client: oss.Client,
        bucket_name: str,
        object_key: str,
        file_path: str,
        part_size: int = 1 * 1024 * 1024,
        parallel_num: int = 4,
        checkpoint_dir: str | None = None,
    ):
        """
        Implement concurrent multipart upload with resumable upload.
    
        :param client: Initialized oss.Client
        :param bucket_name: Destination bucket name
        :param object_key: Destination object key (without bucket name)
        :param file_path: Full path of the local file
        :param part_size: Part size in bytes, default is 1 MB
        :param parallel_num: Number of concurrent upload threads, default is 4
        :param checkpoint_dir: Directory to store breakpoint information; if None, resumable upload is disabled
        """
        file_path = str(file_path)
        if not Path(file_path).exists():
            raise FileNotFoundError(f"Error: Local file not found. Check the file_path configuration: {file_path}")
    
        # Construct the Uploader; enable resumable upload based on whether checkpoint_dir is provided
        if checkpoint_dir:
            uploader = client.uploader(
                enable_checkpoint=True,
                checkpoint_dir=checkpoint_dir,
                part_size=part_size,
                parallel_num=parallel_num,
            )
        else:
            uploader = client.uploader(
                part_size=part_size,
                parallel_num=parallel_num,
            )
    
        print(f"Starting to upload file: {file_path}")
        print(f"Destination Bucket: {bucket_name}")
        print(f"Destination Object: {object_key}")
        print(f"Part size: {part_size} bytes, Concurrency: {parallel_num}")
        if checkpoint_dir:
            print(f"Resumable upload: Enabled (checkpoint_dir={checkpoint_dir})")
        else:
            print("Resumable upload: Disabled (set checkpoint_dir to enable)")
    
        # Execute the upload (Uploader automatically chooses between multi/single part concurrent upload based on size)
        result = uploader.upload_file(
            oss.PutObjectRequest(bucket=bucket_name, key=object_key),
            filepath=file_path,
        )
    
        print("-" * 30)
        print("File uploaded successfully!")
        print(f"HTTP Status: {result.status_code}")
        print(f"ETag: {result.etag}")
        print(f"Request ID: {result.request_id}")
        # CRC-64 checksum; v2 enables data validation by default
        print(f"CRC64: {result.hash_crc64}")
        print("-" * 30)
    
    
    def main():
        # Before running the code example, make sure you have set the corresponding environment variables.
        # macOS/Linux:
        #   AK method:
        #     export OSS_ACCESS_KEY_ID=YOUR_AK_ID
        #     export OSS_ACCESS_KEY_SECRET=YOUR_AK_SECRET
        #   STS method:
        #     export OSS_ACCESS_KEY_ID=YOUR_STS_ID
        #     export OSS_ACCESS_KEY_SECRET=YOUR_STS_SECRET
        #     export OSS_SECURITY_TOKEN=YOUR_STS_TOKEN
        #
        # Windows:
        #   Powershell: $env:OSS_ACCESS_KEY_ID="YOUR_AK_ID"
        #   cmd: set OSS_ACCESS_KEY_ID=YOUR_AK_ID
    
        # ===================== Parameters (modify as needed) =====================
        # Region example: 'cn-hangzhou'; we recommend using Region first
        region = "cn-hangzhou"
    
        # Optional: Custom Endpoint (for internal network, dedicated domain, accelerated domain name, etc.)
        # Example: 'https://oss-cn-hangzhou.aliyuncs.com'
        endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
    
        # Bucket and Object
        bucket_name = "examplebucket"
        object_key = "test.bak"
    
        # Full path of the local file to upload.
        # Windows example: r'D:\localpath\examplefile.txt'  (note the r at the beginning)
        # macOS/Linux example: '/Users/test/examplefile.txt'
        file_path = r"D:\oss\test.bak"
    
        # Sharding and concurrency
        part_size = 1 * 1024 * 1024  # Default is 1 MB; OSS requires a minimum part size of 100 KB
        parallel_num = 4
    
        # Resumable upload directory (pass None to disable; we recommend specifying a writable directory)
        checkpoint_dir = str(Path.cwd() / ".oss_checkpoints")
        # =================== End of parameters ===================
    
        print("Script execution starts...")
        try:
            client = get_client_from_env(region=region, endpoint=endpoint)
            # If resumable upload is enabled, make sure the directory exists
            if checkpoint_dir:
                Path(checkpoint_dir).mkdir(parents=True, exist_ok=True)
    
            resumable_upload_file_v2(
                client=client,
                bucket_name=bucket_name,
                object_key=object_key,
                file_path=file_path,
                part_size=part_size,
                parallel_num=parallel_num,
                checkpoint_dir=checkpoint_dir,
            )
        except FileNotFoundError as e:
            print(e)
        except oss_ex.ServiceError as e:
            # Error returned by the OSS server
            print("\nAn OSS server-side error occurred.")
            print(f"HTTP Status: {getattr(e, 'status_code', 'N/A')}")
            print(f"Error Code: {getattr(e, 'code', 'N/A')}")
            print(f"Message: {getattr(e, 'message', 'N/A')}")
            print(f"Request ID: {getattr(e, 'request_id', 'N/A')}")
            print(f"Endpoint: {getattr(e, 'request_target', 'N/A')}")
        except oss_ex.BaseError as e:
            # SDK local/serialization/deserialization/credential errors
            print("\nAn OSS SDK client-side error occurred.")
            print(str(e))
        except Exception as e:
            print(f"\nAn unknown error occurred: {e}")
    
    
    if __name__ == "__main__":
        main()
  3. Set the validity period of the backup file URL and obtain the URL of the backup file.

    1. Log on to the OSS console.

    2. Click Buckets, and then click the name of the destination bucket.

    3. In the left-side navigation pane, choose File Management > Files.

    4. Click Details in the Actions column of the destination database backup file, and then change Expiration (Seconds) to 28800 seconds, which is 8 hours, in the panel that appears.

      Important

      The URL of the backup file is required when you migrate the backup file from OSS to ApsaraDB RDS. If the URL expires before the migration is complete, the data migration fails.

    5. Click Copy Object URL to obtain the URL of the backup file.

      Copy file URL

    6. Modify the URL of the backup file.

      The URL that you obtain is the public URL of the file by default. To migrate data over the internal network, change the endpoint in the file URL to the internal endpoint.

      For example, if the URL of the backup file is http://rdstest.oss-cn-shanghai.aliyuncs.com/testmigraterds_20170906143807_FULL.bak?Expires=15141****&OSSAccessKeyId=TMP****, change oss-cn-shanghai.aliyuncs.com in the URL to oss-cn-shanghai-internal.aliyuncs.com.

      Important

      Internal endpoints vary based on network type and region. For more information, see Regions and endpoints.

3. Import the OSS backup data to ApsaraDB RDS

Select a solution based on the version of your ApsaraDB RDS for SQL Server instance.

SQL Server 2012 or later, or SQL Server 2008 R2 with cloud disks

  1. Go to the Instances page. In the top navigation bar, select the region in which the RDS instance resides. Then, find the RDS instance and click the ID of the instance.

  2. In the left-side navigation pane, choose Restoration.

  3. Click Restore Backup Data from OSS in the upper part of the page.

  4. On the Import Guide page, click Next twice to go to the data import step.

    Note
    • When you use the OSS backup data migration feature for the first time, you must authorize the ApsaraDB RDS service account to access OSS. Click Authorization URL and confirm the authorization. Otherwise, the OSS Bucket drop-down list is empty due to a permission issue.

    • If the destination file is not displayed on the page, check whether the file extension of the backup file in OSS meets the requirements (you can view the extension requirements in Notes in this topic), and make sure that the ApsaraDB RDS instance and the OSS bucket reside in the same region.

  5. Configure the following parameters.

    Parameter

    Description

    Database Name

    The name of the database to which the backup data is imported on the destination ApsaraDB RDS instance. The name must comply with the official SQL Server naming rules.

    Important
    • Before you migrate data to the cloud, make sure that no database that has the same name as the database to be restored from the backup file exists on the destination instance and no unattached database file that has the same name exists. If neither of them exists, you can restore the database by using the database file that has the same name as the target database name in the backup set.

    • If a database that has the same name as the database to be restored from the backup file exists on the destination instance or an unattached database file that has the same name exists, the migration fails.

    OSS Bucket

    Select the OSS bucket in which the backup file is stored.

    OSS File

    Click the Search icon icon on the right to search for backup files by file name prefix. The file name, file size, and update time of each file are displayed. Select the backup file that you want to migrate.

    Cloud Migration Method

    • Immediate Access (Only One Full Backup): full migration. This option is suitable for scenarios in which only one full backup file is used for migration. In this example, Immediate Access (Only One Full Backup) is selected. In this case, BackupMode = FULL and IsOnlineDB = True in the CreateMigrateTask operation.

    • Access Pending (Incremental Backup or Log Files): incremental migration. This option is suitable for scenarios in which a full backup file together with log backup files or differential backup files is used for migration. In this case, BackupMode = UPDF and IsOnlineDB = False in the CreateMigrateTask operation.

    Consistency Check Mode

    • Asynchronous DBCC: The system does not run DBCC CHECKDB when the database is opened. Instead, the system asynchronously runs DBCC CHECKDB after the database opening task is complete. This reduces the time overhead of opening the database (DBCC CHECKDB is time-consuming for large databases) and shortens the service downtime. If your service is highly sensitive to downtime and you do not care about DBCC CHECKDB results, use asynchronous DBCC. In this case, CheckDBMode = AsyncExecuteDBCheck in the CreateMigrateTask operation.

    • Synchronous DBCC: Compared with asynchronous DBCC, synchronous DBCC is suitable for users who care about DBCC CHECKDB results and want to identify data consistency errors in self-managed databases. In this case, the time required to open the database increases. In this case, CheckDBMode = SyncExecuteDBCheck in the CreateMigrateTask operation.

  6. Click OK.

    Wait until the migration task is complete. You can click Refresh to view the latest status of the task. If the migration task fails, troubleshoot the issue based on the message in the task description. For more information, see Common errors in this topic.

    Note

    After the data migration is complete, the system initiates a backup at the specified backup time based on the automatic backup policy of the ApsaraDB RDS instance. You can manually change the backup time. The generated backup set contains the migrated data. You can view the backup set on the Restoration page of the ApsaraDB RDS instance.

    If the backup time is not reached but you want to generate a backup in the cloud at the earliest opportunity, you can perform a manual backup.

SQL Server 2008 R2 with high-performance local disks

  1. Log on to the ApsaraDB RDS console and go to the Instances page. In the top navigation bar, select the region in which the RDS instance resides. Then, find the RDS instance and click the ID of the instance.

  2. In the left-side navigation pane, click Databases.

  3. Find the destination database and click Migrate Backup Files from OSS in the Actions column.

  4. In the Import Guide dialog box, read the message, and then click Next.

  5. Read the message about OSS upload, and then click Next.

  6. In the OSS URL of the Backup File field, enter the OSS URL of the backup file, and then click OK.

    Enter the OSS URL of the backup file

    Note

    ApsaraDB RDS for SQL Server instances that run SQL Server 2008 R2 with high-performance local disks support only the solution that migrates full backup files to the cloud at a time.

4. View the backup migration progress

Select a solution based on the version of your ApsaraDB RDS for SQL Server instance.

SQL Server 2012 or later, or SQL Server 2008 R2 with cloud disks

Go to the Restoration page in the left-side navigation pane of the ApsaraDB RDS instance, and then view the backup migration records on the Cloud Migration Records of Backup Data tab. The records include the task status, task start time, and task end time. By default, the records of the last week are displayed. You can change the time range as needed.

image

Note

If Task Status is Failed, check Task Description or click View File Details next to the destination migration task to identify and fix the cause of the failure, and then perform the data migration again.

SQL Server 2008 R2 with high-performance local disks

Go to the data migration page in the left-side navigation pane of the ApsaraDB RDS instance, and then find the destination migration task to view the progress of the data migration.

Note

If Task Status is Failed, check Task Description or click View File Details next to the destination migration task to identify and fix the cause of the failure, and then perform the data migration again.

Common errors

Each backup migration record contains a task description. You can identify the causes of task failures and errors from the task description. The following error messages are common:

  • A database with the same name already exists

    • Error message 1: The database (xxx) is already exist on RDS, please backup and drop it, then try again.

    • Error message 2: Database 'xxx' already exists. Choose a different database name.

    • Cause: To ensure data security on ApsaraDB RDS for SQL Server, ApsaraDB RDS for SQL Server does not support the migration of data to a database that has the same name as an existing database.

    • Solution: If you want to overwrite the data of the existing database, back up the existing data, delete the database, and then perform the data migration task again.

  • A differential backup file is used

    • Error message: Backup set (xxx.bak) is a Database Differential backup, we only accept a FULL Backup.

    • Cause: The backup file that you provided is a differential backup file instead of a full backup file. One-time migration of full backup files supports only full backup files and does not support differential backup files.

  • A log backup file is used

    • Error message: Backup set (xxx.trn) is a Transaction Log backup, we only accept a FULL Backup.

    • Cause: The backup file that you provided is a log backup file instead of a full backup file. One-time migration of full backup files supports only full backup files and does not support log backup files.

  • Backup file verification failure

    • Error message: Failed to verify xxx.bak, backup file was corrupted or newer edition than RDS.

    • Cause: The backup file is corrupted, or the version of the SQL Server instance in the self-managed environment is later than the version of the ApsaraDB RDS for SQL Server instance. As a result, the verification fails. For example, this error is reported when you restore a backup file of SQL Server 2016 to an ApsaraDB RDS for SQL Server instance that runs SQL Server 2012.

    • Solution: If the backup file is corrupted, create another full backup in the self-managed environment and create another migration task. If the version of the SQL Server instance in the self-managed environment is later than the version of the ApsaraDB RDS for SQL Server instance, use an ApsaraDB RDS for SQL Server instance that runs the same version or a later version.

      Note

      To upgrade an existing ApsaraDB RDS for SQL Server instance to a later version, see Upgrade database version.

  • DBCC CHECKDB failure

    • Error message: DBCC checkdb failed.

    • Cause: The DBCC CHECKDB operation failed. This indicates that errors have occurred in the database in the self-managed environment.

    • Solution: Run the following command to fix the database errors in the self-managed environment, and then migrate the data again.

      Important

      The process of fixing errors by using this command may result in data loss.

      DBCC CHECKDB (DBName, REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS, ALL_ERRORMSGS
  • Insufficient storage space 1

    • Error message: Not Enough Disk Space for restoring, space left (xxx MB) < needed (xxx MB).

    • Cause: The remaining storage space of the ApsaraDB RDS instance does not meet the minimum storage space that is required to migrate the backup file.

    • Solution: Upgrade the storage space of the instance.

  • Insufficient storage space 2

    • Error message: Not Enough Disk Space, space left xxx MB < bak file xxx MB.

    • Cause: The remaining storage space of the ApsaraDB RDS instance is less than the size of the backup file and does not meet the minimum storage space requirement.

    • Solution: Upgrade the storage space of the instance.

  • Insufficient permissions of the logon account

  • No privileged account

    • Error message: Your RDS doesn’t have any init account yet, please create one and grant permissions on RDS console to this migrated database (xxx).

    • Cause: No privileged account exists on the ApsaraDB RDS instance. The migration task cannot determine the account to which the permissions on the migrated database must be granted. However, the backup file is restored to the destination instance. Therefore, the task status is Success.

    • Solution: Create a privileged account.

  • Insufficient operation permissions of the RAM user

    • Q1: When I perform Step 5 of the data migration task creation procedure, all parameters are configured, but the OK button is grayed out and cannot be clicked. What do I do?

    • A1: The button cannot be clicked possibly because you are logged on as a RAM user and your account has insufficient permissions. Check Prerequisites in this topic to make sure that the required permissions are granted.

    • Q2: What do I do if the permission denied message appears when I use a RAM user to grant permissions on AliyunRDSImportRole?

    • A2: Use your Alibaba Cloud primary account to temporarily grant the AliyunRAMFullAccess permission to the RAM user.

  • The database name is the same as the name of a SQL Server system database

    • Error message: The database (xxx) is mssql system db, change your database name and try again.

    • Cause: The specified database name is the same as the name of a SQL Server system database such as master, msdb, tempdb, or model.

    • Solution: Specify another database name.

  • The database name is the same as the name of a system database of the RDS instance

    • Error message: The database (xxx) is RDS system db, change your database name and try again.

    • Cause: The specified database name is the same as the name of a management database of the ApsaraDB RDS for SQL Server instance, such as rdscore.

    • Solution: Specify another database name.

  • The number of databases exceeds the upper limit

    • Error message: The database (xxx) migration failed due to databases count limitation: xxx.

    • Cause: The number of databases exceeds the upper limit.

    • Solution: Reduce the number of databases on the instance and try again.

  • OSS download URL issues

    • Error message 1: Failed to download backup (xxx) since OSS URL was expired.

    • Error message 2: Failed to download since could not find backup file (xxx) on OSS.

    • Cause: The OSS download URL expired, the ApsaraDB RDS service account was not authorized, the RAM user had insufficient permissions, or the file does not exist.

    • Solution: Check whether the file exists in OSS, whether permissions are properly granted, and whether the authorization is valid. Then, try again.

  • Striped backup issues

    • Error message: Failed to verify (xxx.bak), error message:The media set has xxx media families but only 1 are provided. All members must be provided. VERIFY DATABASE is terminating abnormally.

    • Cause: The striped backup feature was used when the source database was backed up. In this case, a full backup is split into multiple .bak files, but only one file is provided when the data is migrated from OSS to ApsaraDB RDS. ApsaraDB RDS does not support migration based on multiple files at a time.

    • Solution: Back up the source database to a single .bak file and try again.

Common return messages

Task type

Task status

Task description

Description

One-time migration of full backup files

Success

success

The migration is successful.

Failed

Failed to download backup file since OSS URL was expired.

The validity period of the OSS download URL expired, which caused the migration to fail.

Your backup is corrupted or newer than RDS, failed to verify.

The backup file is corrupted or created on a later version than the RDS version, which caused the migration to fail.

DBCC checkdb failed

The DBCC checkdb operation failed, which caused the migration to fail.

autotest_2008r2_std_testmigrate_log.trn is a Transaction Log backup, we only accept a FULL Backup.

A log backup file was used, which caused the migration to fail.

autotest_2008r2_std_testmigrate_diff.bak is a Database Differential backup, we only accept a FULL Backup.

A differential backup file was used, which caused the migration to fail.

Related API operations

API

Description

CreateMigrateTask

Restores backup files from OSS to an ApsaraDB RDS for SQL Server instance and creates a data migration task.

CreateOnlineDatabaseTask

Opens the database of a backup data migration task on an ApsaraDB RDS for SQL Server instance.

DescribeMigrateTasks

Queries the list of backup data migration tasks on an ApsaraDB RDS for SQL Server instance.

DescribeOssDownloads

Queries the details of the backup files of a backup data migration task on an ApsaraDB RDS for SQL Server instance.