如何解决通过隧道在mysql转储期间直观地更新文件的大小
我有一个名为copydata
的bash脚本,该脚本执行以下操作以将特定表的MySQL转储从生产MySQL服务器复制到本地文件,然后将其推送到本地MySQL数据库中。
#!/bin/sh
#set up tunnel
ssh -f -i ~/.ssh/ec2-eu-keypair.pem -o CompressionLevel=9 -o ExitOnForwardFailure=yes -L 3307:elr2.our-id.eu-west-1.rds.amazonaws.com:3306 username@example.com
echo "Dumping tables \"$@\" to /tmp/data.sql"
#dump tables to local file
mysqldump -u root -h 127.0.0.1 -pmypass -P 3307 live_db_name --extended-insert --single-transaction --default-character-set=utf8 --skip-set-charset $@ > /tmp/data.sql
pv /tmp/data.sql | mysql -u root local_db_name --default-character-set=utf8 --binary-mode --force
因此,它的名称类似于copydata table1 table2
它可以工作,但是mysqldump
部分可能会花费很长时间,并且最好对进度进行一些视觉反馈。我想到的一件事是,我可以在进行转储时显示/tmp/data.sql
的大小-例如,如果我继续执行以下操作,则在单独的选项卡中,可以看到它在每秒约2mb的速率:
ls -lh /tmp/data.sql
我可以在上面的脚本中添加上面的命令或类似的命令,以便在等待mysqldump行完成时看到文件大小的更新吗?
解决方法
感谢@YuriLachin的评论,我做到了以下几点:
-
在mysqldump行中添加了
&
,因此它变得异步,即,脚本继续进行到下一行,而mysqldump在后台继续运行 -
添加了以下行,以重复调用本地文件上的
ls -lh
:
pid=$!; while [ -d "/proc/$pid" ] ; do echo -n "$(ls -lh /tmp/data.sql)"\\r; sleep 1; done
让我们分解一下,以尽可能多地帮助我自己学习:
#get the process id of that last backgrounded task (the mysqldump) so we
#can tell when it's finished running
pid=$!
#while it *is* still running
while [ -d "/proc/$pid" ] ; do
#get the size of the file,with ls,but do it inside an echo command.
#Wrapping it like this allows us to use the `-n` option which means "omit newline",#or don't go onto the next line. Then,at the end,do \\r which is a carriage return,#meaning 'go back to the start of the current line',so the next line will
#overwrite the first one.
#Now it updates in place rather than spewing out loads of lines.
echo -n "$(ls -lh /tmp/data.sql)"\\r
#then do nothing for 1 second,to avoid wasting cpu time.
sleep 1
done
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。