我使用bash清除母版上的二进制日志,前提是奴隶已经完成了这些日志。相反,我想在服务器上留下更多的日志。你能帮我把mysql-bin.000345变成mysql-bin.000295 (从345减去50 )吗?
这是我的剧本:
# Fetch the current `Relay_Master_Log_File` from Slave
relay_master_log_file=$(ssh user@slave "mysql -hlocalhost -uroot -e 'SHOW SLAVE STATUS\G' | grep Relay_Master_Log_File | cut -f2- -d':' | sed -e 's/^[ \t]*//'")
# Purge binary logs on Master up to the current `Relay_Master_Log_File`
purge_cmd="PURGE BINARY LOGS TO '$relay_master_log_file';"
ssh user@master <<EOF
mysql -e "$purge_cmd"
EOF但是我想将50个(或n)二进制日志保存在Master上。
发布于 2016-08-29 14:52:09
假设变量中有mysql-bin.000345,则可以执行以下步骤:
$((...))从345中减去nprintf格式化用零填充的结果例如:
oldname=mysql-bin.000345
num=$(shopt -s extglob; echo ${filename##mysql-bin.+(0)})
newname=mysql-bin.$(printf '%06d' $((num - n)))发布于 2016-08-29 14:55:09
您可以在第一个命令本身中使用awk和作为奖励,跳过使用grep、cut和sed。
# Fetch the current `Relay_Master_Log_File` from Slave
relay_master_log_file=$(ssh user@slave "mysql -hlocalhost -uroot -e 'SHOW SLAVE STATUS\G' |
awk -F':[[:blank:]]*' '$1~/Relay_Master_Log_File/{split($2, a, /\./); printf "%s.%06d", a[1], a[2]-50}'
)根据以下评论使用:
ssh user@slave <<-'EOF'
mysql -hlocalhost -uroot -e 'SHOW SLAVE STATUS\G' |
awk -F':[[:blank:]]*' '$1~/Relay_Master_Log_File/{split($2, a, /\./);
printf "%s.%06d\n", a[1], a[2]-50}'
EOFhttps://stackoverflow.com/questions/39208833
复制相似问题