Use a CentOS 7.9 instance with MySQL 8.0 to create and verify application-consistent snapshots that prevent data corruption during backup.
Prerequisites
-
The ECS instance uses ESSDs, and the multi-attach feature is disabled for the disks.
The ECS instance is in the Running state. Cloud Assistant is in Normal state on the instance. To view Cloud Assistant status, see Check Cloud Assistant status and troubleshoot exceptions.
-
MySQL is installed, and you have the database username and password. See Manually deploy a MySQL database (Linux).
A RAM role with a custom policy for application-consistent snapshots is attached to the ECS instance. See Create a RAM role and attach it to an ECS instance.
NoteCloud Assistant requires specific permissions to access the ECS instance and run commands. Grant these permissions through the RAM role.
Configure a custom RAM role, such as AppSnapshotRoleName.
Attach a custom policy to the RAM role. The following sample policy grants permissions to query snapshots, create snapshots, add tags, and query disk information.
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:DescribeSnapshot*", "ecs:CreateSnapshot*", "ecs:TagResources", "ecs:DescribeDisks" ], "Resource": [ "*" ], "Condition": {} } ] }
Workflow
The following workflow creates an application-consistent snapshot and verifies that the database restores to the exact state captured at snapshot time:
-
Step 1: Create the prescript.sh and postscript.sh scripts
Create a prescript (prescript.sh) to pause database writes and a postscript (postscript.sh) to resume them. These scripts are required for application-consistent snapshots.
-
Step 2: Prepare the database verification environment
Create a database table and a stored procedure (TestPIT) that simulates continuous writes, so you can verify the snapshot's effectiveness after rollback.
-
Step 3: Create an application-consistent snapshot in the console
Create an application-consistent snapshot for the Linux instance that hosts MySQL. The prescript and postscript temporarily pause and resume database writes to ensure data consistency.
-
Step 4: Verify that the application-consistent snapshot is created
Check the Cloud Assistant command execution results. Note the database pause and resume times.
-
Step 5: Verify the data restoration from the application-consistent snapshot
Roll back the disk using the snapshot. Compare the last data write time in MySQL with the prescript.sh execution time to verify correct data restoration.
-
If the last write time is earlier than the prescript execution time, no writes occurred during the pause. The restored data matches the snapshot state.
-
If the last write time is the same as or later than the prescript execution time, writes occurred during the pause, indicating the snapshot did not work as expected.
-
Procedure
Step 1: Create the prescript.sh and postscript.sh scripts
The prescript.sh and postscript.sh scripts in this topic are for demonstration only. Write scripts based on your actual business needs. See Create an application-consistent snapshot.
-
Connect to the ECS instance as the root user.
-
Create /tmp/prescript.sh and add the script content.
-
Create /tmp/prescript.sh as root.
vim /tmp/prescript.sh -
Press
ito enter edit mode. -
Customize the prescript.sh content for your application.
-
Press
Esc, enter:wq, and press Enter to save and exit. -
Set permissions to 700 (root only).
ImportantOnly the root user must have read, write, and execute permissions (700). Otherwise, the script execution fails.
chmod 700 /tmp/prescript.sh
-
-
Create /tmp/postscript.sh and add the script content.
-
Create /tmp/postscript.sh as root.
vim /tmp/postscript.sh -
Press
ito enter edit mode. -
Customize the postscript.sh content for your application.
-
Press
Esc, enter:wq, and press Enter to save and exit. -
Set permissions to 700 (root only).
ImportantOnly the root user must have read, write, and execute permissions (700). Otherwise, the script execution fails.
chmod 700 /tmp/postscript.sh
-
-
Go to /tmp and verify the script permissions.
cd /tmp ls -lThe following output shows correct permissions.

Step 2: Prepare the database verification environment
-
Create the test script /root/test.sql.
-
Create and open /root/test.sql.
vim /root/test.sql -
Press
ito enter edit mode. -
Add the verification SQL script.
The script creates a table (PointInTime) and a stored procedure (TestPIT):
USE AdventureWorks; CREATE TABLE PointInTime(id int, t datetime); DELIMITER $$ CREATE PROCEDURE `TestPIT`() BEGIN DECLARE i int; SET i=1; WHILE i < 180 DO INSERT INTO PointInTime VALUES(i, now()); SELECT SLEEP(1); SET i=i+1; END WHILE; END $$ DELIMITER ; -
Press
Esc, enter:wq, and press Enter to save and exit.
-
-
Log on to MySQL.
Run the following command and enter the MySQL password when prompted.
mysql -u <mysqlUserName> -pReplace
<mysqlUserName>with your MySQL username. -
Create a database named AdventureWorks.
CREATE DATABASE AdventureWorks; -
Run the test script.
source /root/test.sql -
Call the stored procedure (TestPIT).
CALL TestPIT;ImportantYou must create the application-consistent snapshot before TestPIT finishes. The procedure runs for about 3 minutes.
Step 3: Create an application-consistent snapshot in the console
In the upper-left corner of the page, select a region and resource group.
-
On the Snapshot-consistent Groups tab, click Create Snapshot-consistent Group.
-
In the Create Snapshot dialog box, configure the snapshot-consistent group.
-
Resource Types defaults to Instance.
-
Select the instance and its ESSD disks.
-
Expand Advanced Settings and enable the application-consistent snapshot.
-
Select Enable Application-consistent Snapshot.
-
Set the prescript.sh and postscript.sh paths to the scripts created in Step 1.
-
Set the duration for File System I/O Pause and Resume.
-
-
-
Click OK.
The command returns a Cloud Assistant execution ID. Use this ID to view the execution result.
Step 4: Verify that the application-consistent snapshot is created
-
Click the Cloud Assistant execution ID to view the result.

An Exit Code of
0indicates the snapshot was created successfully. The output displays the snapshot and snapshot-consistent group IDs.NoteIf the Exit Code is not
0, troubleshoot based on the Exit Code error code. See Error codes. -
View the database pause and resume times in the Cloud Assistant output.
Find the prescript.sh start time and the postscript.sh completion time.
-
The prescript.sh ran at
2024-08-27 15:27:55, indicating the database paused writes at this time.
-
The postscript.sh ran at
2024-08-27 15:27:57, indicating the database resumed writes at this time.
-
-
View the snapshot-consistent group and disk snapshot.
-
On the Snapshot-consistent Groups tab, click the snapshot-consistent group ID to view details.
-
In the Snapshot Information section, verify the snapshot tag confirms application consistency.
The disk snapshot tag
APPConsistent:Trueconfirms an application-consistent snapshot was created.
-
Connect to MySQL and check the data commit pause time.
-
Connect to the ECS instance.
-
Log on to MySQL.
Run the following command and enter the MySQL password when prompted.
mysql -u <mysqlUserName> -pReplace
<mysqlUserName>with your MySQL username. -
Query the PointInTime table.
USE AdventureWorks; SELECT * FROM PointInTime; -
View the database pause time in the query results.
No data was inserted between
2024-08-27 15:27:55and2024-08-27 15:27:58.
-
Step 5: Verify the data restoration from the application-consistent snapshot
-
Roll back the disk using the snapshot-consistent group.
-
Log on to MySQL and query the PointInTime table.
-
Connect to the ECS instance.
-
Log on to MySQL.
Run the following command and enter the MySQL password when prompted.
mysql -u <mysqlUserName> -pReplace
<mysqlUserName>with your MySQL username. -
Query the PointInTime table.
USE AdventureWorks; SELECT * FROM PointInTime; -
View the last record timestamp after data restoration.
The last record was inserted at
2024-08-27 15:27:54, earlier than the pause time2024-08-27 15:27:55noted in Step 4. This confirms the data was restored correctly from the application-consistent snapshot.
-