Bboysoul's Blog

首页 公告 RSS

Introduce an operation for backing up a database

December 25, 2023 本文有 291 个字 需要花费 2 分钟阅读


When grappling with database synchronization within internal networks, we may encounter a myriad of challenges. The conventional approach involves exporting data from the source database, compressing and transferring it to the target network, and finally decompressing and importing the data. This method, albeit effective, is cumbersome and requires substantial disk space, especially for larger databases. To circumvent these dilemmas, we can utilize SSH tunnels and the MySQL data compression feature to streamline the data synchronization process.

Constructing an SSH Tunnel

Initially, we need to establish an SSH tunnel between the two networks. This can be accomplished through the following command:

ssh -L 3306: -p 22 root@x.x.x.x -i id_rsa

This command maps the local port 3306 to the database server’s ( port 3306 in the target network.

Data Synchronization

Subsequently, we can commence the data synchronization. This can be achieved through the following command:

mysqldump -u root -h --port 3306 \
    --databases db1 db2 \
    --compress \
    --single-transaction \
    --order-by-primary  \
    -ppassword | mysql -u root \
        --port=3306 \
        --host= \

This command initially exports data from the source databases (db1 and db2) located at, then pipes the data into the target database. Here, we employ the –compress option of the mysqldump command to reduce the volume of data transmitted across the network. Additionally, we utilize the –single-transaction option to ensure data consistency and the –order-by-primary option to optimize import performance.

It’s worth noting that although the target database address is, the data is actually being transported to the target network’s database server via the SSH tunnel.

By following these steps, we can simplify the data synchronization process between databases while also minimizing the required disk space.

Feel free to follow my blog at

Have Fun


本站总访问量 本站总访客数