我有一个有效的备份脚本,每个文件使用一个rsync命令进行保存。
现在我想向mysql数据库报告每个rsync命令的进度。因此,我需要将--info = progress2的输出拆分为一个数组。
我使用的解决方案使用命名管道和从该命名管道读取第二个脚本并写入mysql的方法。但是我认为没有命名管道,必须有更好的解决方案。是否可以将rsync的输出直接写入IFS?
这些是我现在使用的(短)脚本:
备份脚本:
#! /bin/bash
# /root/pipewriter.sh
pipe=/root/rpipe
/root/pipereader.sh $pipe &
# Waiting for pipereader to come up
while [ ! -p $pipe ]
do
# do nothing, just waiting for the pipe to be ready
:
done
rsync --info=progress2 /root/source.file /root/destination.file > $pipe
echo "quit" > $pipe
exit 0
脚本从管道读取并将进度拆分为数组:
#! /bin/bash
# /root/pipereader.sh
pipe=$1
trap "rm -f $pipe" EXIT
if [ ! -p $pipe ]; then
mknod $pipe p
fi
while true
do
if read line < $pipe ; then
if [[ "$line" == "quit" ]]; then
break
fi
IFS=' ' read -ra vals <<< "$line"
for i in "${vals[@]}"; do
echo $i >> foobar.txt
# doing here mysql commands with $i later
done
fi
done
exit 0
它有效,但是我不认为这是最好的解决方案?