Home Operation and Maintenance CentOS How to backup Docker on CentOS

How to backup Docker on CentOS

Apr 14, 2025 pm 03:09 PM
mysql centos docker ai

Guide to backup data of Docker containers under CentOS

This article introduces two methods to use Docker to backup data on CentOS systems: running container data backup and Docker image backup.

1. Backup of container data during operation

  1. Create a backup directory: Create a directory on the server to store backup files, for example:

    1

    mkdir -p /home/docker/mysql/data_back && chmod -R 777 /home/docker/mysql/data_back

    Copy after login
  2. Confirm the container ID: Use the following command to get the ID of the container named mysql :

    1

    docker ps -aqf "name=mysql"

    Copy after login
  3. Create a backup script (backup.sh): Create a backup.sh script in the /home/docker/mysql/data_back directory and add the following content:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    #!/bin/bash

    # Get the container ID

    container_id=$(docker ps -aqf "name=mysql")

    echo "MySQL container ID: $container_id"

     

    # Check if the container exists if [ -z "$container_id" ]; then

        echo "No container named mysql was found!"

        exit 1

    fi

     

    # MySQL login information (please replace it with your actual information)

    MYSQL_USER="root"

    MYSQL_PASSWORD="your_password" # Please replace it with your MySQL passwordMYSQL_PORT="3306"

     

    # Whether to delete expired backups (true/false)

    expire_backup_delete="true"

    expire_days=7

     

    # Backup file storage path backup_location="/home/docker/mysql/data_back"

     

    # Backup timestamp backup_time=$(date %Y%m%d%H%M)

    backup_Ymd=$(date %Y-%m-%d)

    backup_dir="$backup_location/$backup_Ymd"

     

    # Get the database list (exclude system database)

    DATABASES=$(docker exec $container_id mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e 'SHOW DATABASES;' | grep -vE '^(Database|information_schema|performance_schema)$')

    echo "Database List: $DATABASES"

     

    # Backup database if [ -n "$DATABASES" ]; then

        mkdir -p "$backup_dir"

        while read dbname; do

            echo "Start backup database: $dbname..."

            docker exec $container_id mysqldump --defaults-extra-file=/etc/mysql/conf.d -F -B --default-character-set=utf8 "$dbname" | gzip > "$backup_dir/bak-$dbname-$backup_time.sql.gz"

            if [ $? -eq 0 ]; then

                echo "Database $dbname backup was successful: $backup_dir/bak-$dbname-$backup_time.sql.gz"

            else

                echo "Database $dbname backup failed!"

            fi

        done

    Copy after login
  4. Configure MySQL (my.cnf): Make sure there is the correct configuration file in the /etc/mysql/conf.d directory, and set the correct MySQL user and password. Be sure to replace your_password with your actual MySQL password.

  5. Grant script execution permissions:

    1

    chmod x /home/docker/mysql/data_back/backup.sh

    Copy after login
  6. Set timed tasks (crontab): Use crontab -e to edit crontab and add timed tasks, such as performing backups at 2 a.m. every day:

    1

    <code>0 2 * * * /home/docker/mysql/data_back/backup.sh</code>

    Copy after login

2. Docker image backup

  1. Use the docker save command: Save the image as a tar package using the following command:

    1

    docker save -o mycentos.tar mycentos_new:1.1

    Copy after login

Please modify the parameters in the script according to the actual situation, such as container name, MySQL password, backup path and timing tasks. After the backup is completed, please properly store the backup files and regularly test the recovery process. Remember to check the integrity and effectiveness of backups regularly.

The above is the detailed content of How to backup Docker on CentOS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
Quantitative Exchange Ranking 2025 Top 10 Recommendations for Digital Currency Quantitative Trading APPs Quantitative Exchange Ranking 2025 Top 10 Recommendations for Digital Currency Quantitative Trading APPs Apr 30, 2025 pm 07:24 PM

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How does deepseek official website achieve the effect of penetrating mouse scroll event? How does deepseek official website achieve the effect of penetrating mouse scroll event? Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

Easeprotocol.com directly implements ISO 20022 message standard as a blockchain smart contract Easeprotocol.com directly implements ISO 20022 message standard as a blockchain smart contract Apr 30, 2025 pm 05:06 PM

This groundbreaking development will enable financial institutions to leverage the globally recognized ISO20022 standard to automate banking processes across different blockchain ecosystems. The Ease protocol is an enterprise-level blockchain platform designed to promote widespread adoption through easy-to-use methods. It announced today that it has successfully integrated the ISO20022 messaging standard and directly incorporated it into blockchain smart contracts. This development will enable financial institutions to easily automate banking processes in different blockchain ecosystems using the globally recognized ISO20022 standard, which is replacing the Swift messaging system. These features will be tried soon on "EaseTestnet". EaseProtocolArchitectDou

See all articles