I was wondering if there’s any way to determine, while mysqldump is running, how much of the backup has completed or how much is remaining?
I’d like to provide my users with a console progress bar to show them the status of their backups…
by the way I wanna write the above program in standard c++ and the platform is gonna be linux.
Yes, a patch was committed on March 27th, 2010:
This new patch has an extra parameter –show-progress-size which by
default is set to 10,000. So when –verbose is used, every 10,000
lines you will get a regular status output of the number of rows for a
particular table dumped.
So check your version, update if needed and enjoy.
Install and use
pv (it is available as a yum package for CentOS)
PV (“Pipe Viewer”) is a tool for monitoring the progress of data
through a pipeline. It can be inserted into any normal pipeline
between two processes to give a visual indication of how quickly data
is passing through, how long it has taken, how near to completion it
is, and an estimate of how long it will be until completion.
Assuming the expect size of the resulting dumpfile.sql file is 100m (100 megabytes), the use of
pv would be as follows:
mysqldump <parameters> | pv --progress --size 100m > dumpfile.sql
The console output will look like:
[===> ] 20%
Look at the man page
man pv for more options. You can display the transfer rate, or how much time has elapsed, or how many bytes have transferred, and more.
If you do not know the size of your dump file, there is a way to obtain a size of the MySQL database from the table_schema – it will not be the size of your dump file, but it may be close enough for your needs:
SELECT table_schema AS "Database", ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema;
db_size=$(mysql -h"$DB_HOST" \ -u"$DB_USERNAME" \ -p"$DB_PASSWORD" \ --silent \ --skip-column-names \ -e "SELECT ROUND(SUM(data_length) / 1024 / 1024, 0) \ FROM information_schema.TABLES \ WHERE table_schema='$DB_NAME';")
Create a backup into timestamped filename:
mysqldump -h"$DB_HOST" \ -u"$DB_USERNAME" \ -p"$DB_PASSWORD" \ --single-transaction \ --order-by-primary \ --compress \ $DB_NAME | pv --progress --size "$db_size"m > "$(date +%Y%m%d)"_backup.sql