今天,我想分享一种简单的方法来检查BTRFS快照的状态。
用法
# btrfs -du /path/
如果 路径 被省略,它将默认为 / .
我们可以轻松列出快照,并清楚地了解它们共有多少数据以及持有多少总开销。
仅需7.48GB,即可获得四个月的快照!!
安装
获取脚本 并使其可执行。您可以分两行执行此操作,但最好先对其进行检查。唐’不要盲目地相信任何人。
sudo wget //raw.githubusercontent.com/nachoparker/btrfs-du/master/btrfs-du -O /usr/local/sbin/btrfs-du sudo chmod +x /usr/local/sbin/btrfs-du
细节
通常,您只能通过qgroups中的配额信息获取磁盘使用情况信息
由于以下几个原因,这是不理想的:
- 首先,我们需要启用配额,这可以有一个 性能影响 有许多子卷。
- 其次,我们仍然必须列出子卷以将子卷ID与子卷名称相关联。两步简单的操作。
我受到启发 btrfs 大小 由Kyle Agronick撰写,但不仅输出’我的口味很干净,但是在我的根子卷上,它的运行速度非常慢,其中docker使用BTRFS作为存储驱动程序,并且我有很多子卷。我正在讲近一分钟。
通过研究代码来加快速度,我认为重写它会更干净,因为它比需要的复杂。另外,配额不’不需要启用 btrfs -du 上班。
请记住,如果未为该子卷启用配额,则该命令可能需要几秒钟来计算大小。我们可以通过预先启用配额来加快执行速度,但是如前所述,这可能会对具有许多快照的大型子卷产生影响。
码
!/bin/bash # # Script that outputs the filesystem usage of snapshots in a location ( root if omited ) # # 用法 : # sudo btrfs -du ( 路径 ) # # Copyleft 2017 通过 Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * Use at your own risk! # # Based on btrfs 大小 通过 Kyle Agronick # # More at //crazypuppy.net/2017/12/06/check-disk-space-of-your-btrfs-snapshots-with-btrfs-du/ # LOCATION=${1:-/} # checks [[ ${EUID} -ne 0 ]] && { printf "Must be run as root. Try 'sudo $( basename "$0" )'\n" exit 1 } [[ -d "$LOCATION" ]] || { echo "$LOCATION not found" exit 1 } [[ "$( stat -fc%T "$LOCATION" )" != " btrfs " ]] && { echo "$LOCATION is not in a BTRFS system" exit 1 } # quota management sync btrfs qgroup show "$LOCATION" 2>&1 | grep -q "quotas not enabled" && { QFLAG=1 btrfs quota enable "$LOCATION" } # if we just enabled quota, might have to wait for rescan OUT=$( btrfs qgroup show --si --sort=qgroupid "$LOCATION" 2>&1 ) grep -q -e "rescan is running" -e "data inconsistent" <<< "$OUT" && { echo "INFO:配额已禁用。等待重新扫描完成..." while true; do sleep 2 OUT=$( btrfs qgroup show --si --sort=qgroupid "$LOCATION" 2>&1 ) grep -q -e "rescan is running" -e "data inconsistent" <<< "$OUT" || break done } # data declare -A TOT EXCL NAME ## qgroup data OUT=$( sed '1,3d;s|^.*/||' <<< "$OUT" ) ID__=( $( awk '{ print $1 }' <<< "$OUT" ) ) TOT_=( $( awk '{ print $2 }' <<< "$OUT" ) ) EXC_=( $( awk '{ print $3 }' <<< "$OUT" ) ) for (( i = 0 ; i < ${#ID__[@]} ; i++ )); do TOT[${ID__[$i]}]=${TOT_[$i]} EXC[${ID__[$i]}]=${EXC_[$i]} done ## naming data OUT=$( btrfs subvolume list --sort=rootid "$LOCATION" | cut -d ' ' -f 2,9 ) ID__=( $( awk '{ print $1 }' <<< "$OUT" ) ) NAM_=( $( awk '{ print $2 }' <<< "$OUT" ) ) for (( i = 0 ; i < ${#ID__[@]} ; i++ )); do NAME[${ID__[$i]}]=${NAM_[$i]} done EXCL_RAW=( $( btrfs qgroup show --raw "$LOCATION" | sed '1,2d' | awk '{ print $3 }' ) ) EXCL_TOTAL=0 [[ "$QFLAG" == "1" ]] && btrfs quota disable "$LOCATION" # output printf "%-60s %-10s %-10s %-10s\n" "Subvolume" "Total" "Exclusive" "ID" printf "─────────────────────────────────────────────────────────────────────────────────────────\n" ## matching 通过 IDs in btrfs subvolume list for (( i = 0 ; i < ${#ID__[@]} ; i++ )); do printf "%-60s %-10s %-10s %-10s\n" ${NAME[${ID__[$i]}]} ${TOT[${ID__[$i]}]} ${EXC[${ID__[$i]}]} ${ID__[$i]} EXCL_TOTAL=$(( EXCL_TOTAL + ${EXCL_RAW[$i]} )) done EXCL_TOTAL=$( awk '{ sum=$1 ; hum[1024^4]="TB";hum[1024^3]="GB";hum[1024^2]="MB";hum[1024]="KB"; for (x=1024^4; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f%s\n",sum/x,hum[x];break } }}' \ <<< "$EXCL_TOTAL" ) printf "─────────────────────────────────────────────────────────────────────────────────────────\n" printf "%86s\n" "Total exclusive data: $EXCL_TOTAL" # License # # This script is free 软件 ; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published 通过 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this script; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA
我收到错误/ home / miguel / bin / btrfs -du :第88行:EXCL_TOTAL +:语法错误:预期的操作数(错误令牌为“+ “)
请在github上打开一个问题,以便我们来看一下
//github.com/nachoparker/btrfs-du/issues
#btrfs-du / home
INFO:配额已禁用。等待重新扫描完成…
那正常吗?
是。需要启用配额进行计算。如果启用了它们,那么它将会很快,但是如果没有启用,它们将被启用,然后btrfs-du将等到扫描完成后,报告磁盘使用情况,最后再次禁用它们。这可能需要几分钟。
您可以通过启用配额来加快此过程,如文章所述
通过阅读以下文章,我被误导了:
“Also, quotas don’不需要启用 btrfs -du 上班。 ”
不知何故,这意味着btrfs-du可以在没有配额的情况下工作,但是在我的debian安装中,如果未启用配额,它将无法启动,只是说“检测到旧的btrfs工具,需要启用配额并等待重新扫描以显示准确的结果”
启用配额后,它确实可以工作,但是比起传统方法,它有什么好处,就像您说的警告:它正在使用配额…
也许我弄错了,让我知道