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:

     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 :

     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:

     #!/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:

     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:

     <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:

     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
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
How to understand DMA operations in C? How to understand DMA operations in C? Apr 28, 2025 pm 10:09 PM

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

How to use the chrono library in C? How to use the chrono library in C? Apr 28, 2025 pm 10:18 PM

Using the chrono library in C can allow you to control time and time intervals more accurately. Let's explore the charm of this library. C's chrono library is part of the standard library, which provides a modern way to deal with time and time intervals. For programmers who have suffered from time.h and ctime, chrono is undoubtedly a boon. It not only improves the readability and maintainability of the code, but also provides higher accuracy and flexibility. Let's start with the basics. The chrono library mainly includes the following key components: std::chrono::system_clock: represents the system clock, used to obtain the current time. std::chron

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 measure thread performance in C? How to measure thread performance in C? Apr 28, 2025 pm 10:21 PM

Measuring thread performance in C can use the timing tools, performance analysis tools, and custom timers in the standard library. 1. Use the library to measure execution time. 2. Use gprof for performance analysis. The steps include adding the -pg option during compilation, running the program to generate a gmon.out file, and generating a performance report. 3. Use Valgrind's Callgrind module to perform more detailed analysis. The steps include running the program to generate the callgrind.out file and viewing the results using kcachegrind. 4. Custom timers can flexibly measure the execution time of a specific code segment. These methods help to fully understand thread performance and optimize code.

How to handle high DPI display in C? How to handle high DPI display in C? Apr 28, 2025 pm 09:57 PM

Handling high DPI display in C can be achieved through the following steps: 1) Understand DPI and scaling, use the operating system API to obtain DPI information and adjust the graphics output; 2) Handle cross-platform compatibility, use cross-platform graphics libraries such as SDL or Qt; 3) Perform performance optimization, improve performance through cache, hardware acceleration, and dynamic adjustment of the details level; 4) Solve common problems, such as blurred text and interface elements are too small, and solve by correctly applying DPI scaling.

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.

What is real-time operating system programming in C? What is real-time operating system programming in C? Apr 28, 2025 pm 10:15 PM

C performs well in real-time operating system (RTOS) programming, providing efficient execution efficiency and precise time management. 1) C Meet the needs of RTOS through direct operation of hardware resources and efficient memory management. 2) Using object-oriented features, C can design a flexible task scheduling system. 3) C supports efficient interrupt processing, but dynamic memory allocation and exception processing must be avoided to ensure real-time. 4) Template programming and inline functions help in performance optimization. 5) In practical applications, C can be used to implement an efficient logging system.

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

See all articles