Mac 微信聊天记录迁移到外置 SSD

背景

  1. 我的 MBP 只有 500GB, 多年用下来多余空间已经不足, 时不时就只有几 GB
  2. 我买了个 2 TB 的 SSD 做外置硬盘(尿袋), 每天插着用

发现微信占用了将近 50 GB

今天用 DaisyDisk 分析磁盘使用时, 发现微信占用了快 50 GB.

决定把迁移到 SSD 上, 因为动了沙盒环境, 微信迁移稍微麻烦一点, 上代码

注意: 先停止微信

1
2
3
4
5
6
7
8
9
10
11
# 将整个文件夹复制走
cp /Users/apple/Library/Containers/com.tencent.xinWeChat/Data/Documents /Volumes/Store/local/wechat/Documents

# 备份一下
mv /Users/apple/Library/Containers/com.tencent.xinWeChat/Data/Documents /Users/apple/Library/Containers/com.tencent.xinWeChat/Data/Documents_backup

# 创建软链接
ln -s /Volumes/Store/local/wechat/Documents $HOME/Library/Containers/com.tencent.xinWeChat/Data

# 重新签名一下
sudo codesign --sign - --force --deep /Applications/WeChat.app

这时候就可以启动 wechat 了, 无痛迁移, 无影响就可以把 Users/apple/Library/Containers/com.tencent.xinWeChat/Data/Documents_backup 删除了.

对了, 需要允许一下微信反问外置硬盘

我的微信版本

最后的最后

附上我目前使用的一个脚本, 将目前 macOS 常用缓存等文件夹搬家到 SSD 里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash

# 设置目标目录
TARGET_DIR="/Volumes/Store/local"

# 预设要移动的文件夹列表
# 格式为: FOLDERS_TO_MOVE["<source_path>"]="<destination_name>"
# 如果 <destination_name> 为空, 则使用 source_path 的 basename
declare -A FOLDERS_TO_MOVE
FOLDERS_TO_MOVE=(
["$HOME/.pyenv"]=""
["$HOME/.ollama"]=""
["$HOME/.npm"]=""
["$HOME/.m2"]=""
["$HOME/.gradle"]=""
["$HOME/.cargo"]=""
["$HOME/Downloads/images"]=""
["$HOME/Downloads/lark"]=""
["$HOME/Library/Containers/com.tencent.xinWeChat/Data/Documents"]="wechat/Documents"
# ["$HOME/.go"]=""
# 在此添加更多文件夹
)

# 创建目标目录(如果不存在)
mkdir -p "$TARGET_DIR"

# 日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# 为每个文件夹创建软链接
for folder in "${!FOLDERS_TO_MOVE[@]}"; do
dest_name="${FOLDERS_TO_MOVE[$folder]}"

# 如果 dest_name 为空, 使用默认行为
if [ -z "$dest_name" ]; then
dest_name=$(basename "$folder")
fi

# 目标路径
target_path="$TARGET_DIR/$dest_name"

# 检查原始文件夹是否存在
if [ ! -d "$folder" ]; then
log "警告: 文件夹 $folder 不存在,跳过"
continue
fi

# 检查目标位置是否已存在
if [ -e "$target_path" ]; then
log "警告: 目标位置 $target_path 已存在,跳过"
continue
fi

# 检查原始位置是否已经是软链接
if [ -L "$folder" ]; then
log "警告: $folder 已经是软链接,跳过"
continue
fi

# 创建目标路径的父目录 (如果需要)
mkdir -p "$(dirname "$target_path")"

# 移动文件夹到目标位置
log "移动 $folder$target_path"
mv "$folder" "$target_path"

# 检查移动是否成功
if [ $? -ne 0 ]; then
log "错误: 无法移动 $folder$target_path"
continue
fi

# 创建软链接
log "创建从 $target_path$folder 的软链接"
ln -s "$target_path" "$folder"

# 检查链接是否创建成功
if [ $? -ne 0 ]; then
log "错误: 无法创建从 $target_path$folder 的软链接"
# 尝试恢复原始文件夹
mv "$target_path" "$folder"
log "已尝试恢复原始文件夹"
else
log "成功: $folder 已移动并创建软链接"
fi
done

log "操作完成"