NAS里的TimeMachine备份文件太大了,偶然发现了少数派提供了一个脚本还不错,特此记录和备份一下。
首先,执行brew install findutils来安装gfind命令,否则后面会报错。

然后,只需要运行 tmcleanup.sh,后面依次跟上 Time Machine 磁盘挂载路径、计算机名称、要保留的备份的天数即可。例如下面的命令,能够实现清理 /Volumes/时间机器备份/Backups.backupdb 路径下,计算机 blanboom-studio30 天前的所有备份:

$ sudo ./tmcleanup.sh /Volumes/时间机器备份/Backups.backupdb blanboom-studio 30

最后附上脚本内容。

#!/bin/bash
#
# Pre-requesite is that GNU linux findutils package is installed (brew install findutils) for gfind
# Should be possible to just use find but I have not tested it (should just omit -daystart)
#
# USEAGE:
#   ./tmcleanup.sh BACKUP_LOCATION MACHINE_NAME DAY_AGE
#
# EXAMPLE:
#   ./tmcleanup.sh /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30
#

if [ "$#" -ne 3 ] ; then
    echo "USEAGE:" 
    echo "  $0 BACKUP_LOCATION MACHINE_NAME DAY_AGE"
    echo "EXAMPLE:"
    echo "  $0 /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30"
    exit
fi


BACKUP_LOCATION=$1
MACHINE_NAME=$2
DAY_AGE=$3

if [ ! -d "${BACKUP_LOCATION}" ]; then
    echo "${BACKUP_LOCATION} is currently not accessible. Please connect your Time Machine disk or change the path."
    exit
fi

echo "*-* Removing old Time Machine Backups for ${MACHINE_NAME} older than ${DAY_AGE} days *-*"
# Get a list of all the 
result=$(gfind "${BACKUP_LOCATION}"/"${MACHINE_NAME}"/ -daystart -maxdepth 1 -mindepth 1 -mmin +$((60*24*${DAY_AGE})))

echo "$result"

if [ -n "$result" ]; then
    while read -r line; do
        START_TIME=$(date +%s)
        tmutil delete "$line"
        END_TIME=$(date +%s)
        echo "Elapsed time: $(($END_TIME - $START_TIME)) secs."
    done <<< "$result"

    echo "Run hdiutil compact /path/to/timemachine/sparsebundle if necessary"

    echo "Finished!"
else
    echo "Did not find any Time Machine Backups older than ${DAY_AGE} days old."
fi
Last modification:October 2, 2020
If you think my article is useful to you, please feel free to appreciate