Agentlessly back up and clone a MySQL environment

Updated at:

Data can be lost for many reasons, and rapid data recovery is critical for business continuity. As businesses scale online, they need to quickly set up development and testing environments based on production environments to support continuous iteration and new releases. This solution uses snapshots and custom images to provide an agentless method for backing up and efficiently cloning a MySQL environment.

Benefits

This solution offers the following benefits:

  • Agentless data backup

    This solution uses the snapshot feature of EBS to back up data without consuming the vCPU or memory resources of your ECS instance. This process does not affect production services and ensures effective, reliable, and easy backups without third-party backup tools or plug-ins.

    Alibaba Cloud's snapshot service is an agentless data backup method that creates a point-in-time, full copy of an individual disk or a disk group. You can use these snapshots for data recovery, setting up development and testing environments, or creating custom images for batch deployments. For more information, see Snapshot overview.
  • Flexible and efficient environment cloning

    Traditional manual data cloning is inefficient and error-prone. This solution uses snapshots and custom images to efficiently clone and set up a complete MySQL application and its data from a development environment to a test environment.

  • Versatile features for diverse user needs

    This solution integrates various snapshot and image features, such as cross-region replication, application consistency, snapshot encryption, and snapshot sharing. These features allow you to configure and use cloud resources based on your specific requirements and business scenarios.

Billing

During the environment setup, you are charged for the Alibaba Cloud resources you use, such as ECS instances, snapshots, and images, based on their billing methods. If you use the one-click deployment feature in Labs, the lab task itself is free, but you are charged for the underlying Alibaba Cloud resources.

The Alibaba Cloud resources used in the one-click deployment in Labs support only the pay-as-you-go billing method.

Back up and clone a MySQL environment

Manual deployment

To manually back up and clone a MySQL environment, you first need to purchase an ECS instance, deploy the MySQL service using commands, and then manually back up and clone the environment by creating a snapshot and a custom image.

When you create ECS instances, snapshots, and images, we recommend that you use distinct names for each resource to make them easier to find later.

Step 1: Create an ECS instance and deploy MySQL

  1. Create an ECS instance. The example instance has the following configuration:

    • Instance type: ecs.g8a.large.

    • Operating system: This example uses CentOS 7.9. For other operating systems, you may need to adjust the commands.

    • System disk: 40 GiB ESSD PL0.

    • The MySQL service is not installed on the ECS instance.

    For more information, see Create an instance by using the custom launch tab.

  2. Connect to the ECS instance.

    For more information, see Connect to a Linux instance by using Workbench.

  3. Deploy the MySQL service.

    1. Run the following commands to download and install the MySQL installation packages.

      # Add the official MySQL repository
      sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm
      # Install the MySQL service
      sudo yum install -y mysql-server
    2. Run the following commands to configure and start MySQL.

      sudo systemctl start mysqld
      sudo systemctl enable mysqld
    3. Run the following command to check whether MySQL started properly.

      ps aux | grep mysqld
      [ecs-user@iZbxxx            ~]$ ps aux | grep mysqld
      mysql     2389  8.5  5.1 1792164 399104 ?      Ssl  16:01   0:00 /usr/sbin/mysqld
      ecs-user  2504  0.0  0.0  12820  2268 pts/0    S+   16:01   0:00 grep --color=auto mysqld
    4. Run the following command to view the initial MySQL password.

      sudo cat /var/log/mysqld.log | grep root | grep "temporary password" | awk '{print $13}'
      [ecs-user@iZbpxxx ~]$ sudo cat /var/log/mysqld.log | grep root | grep "temporary password" | awk '{print $13}'
      f3kpR9>l9JTK_
    5. Run the following command to log in to MySQL.

      mysql -uroot -p

      Enter the password from the previous step to log in to MySQL.

      [ecs-user@iZbp1xxx          ~]$ mysql -uroot -p
      Enter password:
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 8
      Server version: 8.0.35
      Copyright (c) 2000, 2023, Oracle and/or its affiliates.
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      mysql>
      Note

      You can run the ALTER USER 'root'@'localhost' IDENTIFIED BY '<new_password>'; command to change the MySQL login password.

    6. Run the following command to exit MySQL.

      exit
  4. Run the following command to ensure all data is flushed to disk before you create a snapshot. This prevents data from remaining in the cache and ensures the data integrity of the snapshot.

    sync

Step 2: Clone the MySQL environment

In this step, you create a snapshot of the system disk and then use it to generate a custom image. You can then use the custom image to quickly clone the complete environment and data.

  1. Log in to the ECS console.

  2. Create a snapshot of the ECS instance's system disk.

  3. Create a custom image from the snapshot.

    For more information, see Create a custom image from a snapshot.

  4. Create a new ECS instance from the custom image.

    For more information, see Create an instance from a custom image.

One-click deployment

Important

The one-click deployment feature in Labs is typically used for rapid validation and feature testing. It is not recommended for production environments. For production, we recommend using manual deployment to ensure system security and stability.

The one-click deployment feature in Labs automatically creates resources and completes the entire backup and cloning process. This lab scenario automatically performs the following actions:

  • Create an ECS instance with a name prefixed with ECS_Instance.

  • Create a snapshot of the system disk.

  • Create a custom image from the snapshot with a name prefixed with MySQLImage.

  • Clone a new ECS instance from the custom image with a name prefixed with ECS_Instance_Clone.

Procedure

  1. Go to the Lab Scenarios page.

    1. Log on to the EBS console.

      If you are logging on to the EBS console for the first time, create an EBS service-linked role as prompted. For more information, see EBS service-linked role.
    2. In the left-side navigation pane, choose Labs > Lab Scenarios.

    3. In the upper-left corner of the top navigation bar, select a region.

    4. On the Agentless ECS data backup and efficient environment construction scenario card, click Create Lab Task.

  2. On the Lab scenario page, configure the basic information for the lab scenario and click Next.

    Parameter

    Description

    Lab scenario

    Defaults to Agentless ECS data backup and efficient environment construction.

    Lab task name

    The name is pre-filled by default. You can change it as needed.

    Description

    (Optional) Enter a description for the lab task.

  3. On the dependency check page, review the dependencies. If the status is No anomalies found, click Next.

  4. On the Resource parameters page, modify the parameters for the lab task and click Next.

    Parameter

    Description

    ECS instance type

    Select an instance type as needed. This example uses ecs.g8a.large (2 vCPUs, 8 GiB memory).

    Instance password

    The password is pre-filled by default. You can change it as needed.

    Important

    Make sure to remember the instance password. You will need it to connect to the ECS instance later.

    Zone

    Select the availability zone for the instance. This example uses cn-hangzhou-k.

  5. Review and confirm the configuration parameters and billing information, then click OK. In the Create Lab Task dialog box that appears, click OK.

    After the task is successfully created, the system begins to prepare the resources and set up the lab scenario. This process takes approximately 10 to 15 minutes.

    Lab tasks rely on the orchestration capabilities of ROS. Each lab task corresponds to one stack. You can create a maximum of 200 lab tasks.
  6. When the task status changes to Created, you can click the Lab Task ID to view the detailed configuration of the task.

Verification and cleanup

Verify the solution

  1. Go to ECS console - Instances. In the top-left corner, select the region and resource group for the target resource.

  2. On the instance, snapshot, and image pages, check the created snapshots, images, and cloned ECS instances.

    Note
    • For manual deployment, find the resources by the custom names you assigned.

    • For Labs deployment:

      • The ECS instance name is prefixed with ECS_Instance.

      • The cloned instance name is prefixed with ECS_Instance_Clone.

      • The image name is prefixed with MySQLImage.

  3. Connect to the newly cloned ECS instance.

    Go to the instance details page, click Connect, and select Workbench. Follow the prompts to log in and open the terminal.

  4. Run the following command to check if the MySQL service is running properly.

    ps aux | grep mysqld

    The following output indicates that the MySQL service has started properly, meaning the MySQL environment was successfully cloned.

    [ecs-user@iZbp18xxx ~]$ ps aux | grep mysqld
    mysql     2389  0.2  5.3 1810368 419288 ?        Ssl  16:01   0:14 /usr/sbin/mysqld
    ecs-user 30358  0.0  0.0 112824   2384 pts/1    S+   17:43   0:00 grep --color=auto mysqld

Clean up resources

If you deployed the resources using the one-click deployment in Labs, we recommend that you clean up the resources after testing to avoid further charges.

  1. Log on to the EBS console.

  2. In the left-side navigation pane, choose Labs > Lab records.

  3. In the upper-left corner of the top navigation bar, select a region.

  4. In the Actions column of the target lab task, click Delete. This process takes about 5 minutes.

    You can also select multiple lab tasks and click Batch Delete at the bottom of the list.

    Warning

    By default, deleting a lab task does not release the associated resources. If you select the Delete Resource checkbox, the system also releases all resources within the lab task, such as ECS instances and database instances. This action cannot be undone, so proceed with caution.

  5. Go to ECS console - Instances. In the top-left corner, select the region and resource group for the target resource.

  6. Delete a custom image and any related snapshot resources.