# 加速Time Machine备份速度

\[Time Machine]\(<https://en.wikipedia.org/wiki/Time_Machine_(OS_X))是苹果操作系统非常独特而且关键的特性，提供了系统完全的备份以及恢复到任意时间点的功能。>

但是，Time Machine备份也是非常缓慢的过程，对于几百G的数据，动辄备份十几小时也非藏常见。

[Time Machine ridiculously slow after El Capitan upgrade](http://apple.stackexchange.com/questions/212537/time-machine-ridiculously-slow-after-el-capitan-upgrade) 提供了一个解决建议。

> 不过，我的实践测试下来并没有改善备份速度，看来还有什么我没有找到的解决问题。

## 实际使用的方法

[Fix Slow Time Machine Backups on a Mac](http://osxdaily.com/2016/03/19/fix-slow-time-machine-backups-mac/)建议可以尝试启动到安全模式进行备份。安全模式的启动方法是在启动的时候按住`shift`键，启动后会看到右上角有一行红色小字显示"sofe boot"。

我的验证确实发现，第一次对整个系统备份采用这个方法是成功的（没有使用sofe boot时候备份了十几个小时都没有完成）。

## 关闭限流

* 有可能是系统的高负载瓶颈设置了低的IO，可以通过在终端中输入以下命令查看

```
sudo fs_usage backupd
```

这个命令中如果看到 `THROTTLED` 项就是表示备份是受到限流的。

> 上述命令会检查`backupd`服务的文件系统使用活动情况，可以实时看到`backupd`备份的文件。不过，我没有看到`THROTTLED`内容。

* 如果有大量文件，特别是笑文件，会有大量的I/O任务，因为需要对`xattrs`执行一系列的I/O操作。使用以下命令，关闭限流：

```
sudo sysctl debug.lowpri_throttle_enabled=0
```

关闭限流可以大幅缩短备份时间

* 备份完成后，使用如下命令恢复限流：

```
sudo sysctl debug.lowpri_throttle_enabled=1
```

## 限流持久化配置

* 如果要在重启后依然使得上述设置生效，可以创建一个名为 `/Library/LaunchDaemons/fix-el-capitan-slow-time-machine-speed.plist` 文件，内容如下

```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>fix-el-capitan-slow-time-machine-speed</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/sbin/sysctl</string>
      <string>debug.lowpri_throttle_enabled=0</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>
```

* 确保文件属于root用户

```
sudo chown root /Library/LaunchDaemons/fix-el-capitan-slow-time-machine-speed.plist
```

* 确认命令

```
sudo launchctl load /Library/LaunchDaemons/fix-el-capitan-slow-time-machine-speed.plist
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/mac/time_machine_backup_speed.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
