Skip to main content

Data Backup and Restore in MySQL

Data Backup

  1. Execute the backup command.

    docker exec -it $(docker ps | grep community | awk '{print $1}') bash -c 'source /entrypoint.sh && backup mysql'

    If successful, the following message will be output (the storage path in the container)

    backup mysql saved to /data/backup/20240117130609/mysql
    backup log saved to /data/backup/20240117130609/backupMysql.log
  2. Copy the backup file to the directory in the host (/data/backup).

    mkdir -p /data/backup
    docker cp $(docker ps | grep community | awk '{print $1}'):/data/backup/20240117130609 /data/backup/

Data Restore

  1. Stop the HAP service. Execute the following command in the root directory of the manager.

    bash ./service.sh stopall
  2. Remove the original data directory from MySQL (Default: /data/mingdao/script/volume/data/mysql).

    mv /data/mingdao/script/volume/data/mysql /data/backup/mysql_$(date +%Y%m%d%H%M%S)
  3. Start the temporary container and mount the data directory (Replaced with the actual mirror address and version number).

    docker run -it --rm --entrypoint bash -e ENV_MYSQL_HOST="127.0.0.1" -e ENV_MYSQL_PORT="3306" -e ENV_MYSQL_USERNAME="root" -e ENV_MYSQL_PASSWORD="123456" -v /data/mingdao/script/volume/data/:/data/ -v /data/backup/:/data/backup/ registry.cn-hangzhou.aliyuncs.com/mdpublic/mingdaoyun-sc:3.0.0
  4. Create directory.

    mkdir -p /data/{logs,mysql}
  5. Start MySQL in the temporary container.

    source /entrypoint.sh && mysqlStartup &
  6. Restore MySQL database (/data/backup/20240117130609/mysql, replaced with actual path).

    source /entrypoint.sh && restore mysql /data/backup/20240117130609/mysql
  7. Execution completed. Exit the temporary container.

    exit