写在前面

记一次跨设备转移+备份

首先我们要在做备份/转移的设备上安装 backup 程序

查看hexo 版本

进入博客目录,右键 Git Bash 输入查看 hexo 版本,rt我的 hexo 版本是5.4.0

1
hexo version

安装 hexo backup

参考文章中建议Hexo 版本是 3.x.x或者4.x.x(我的hexo版本是5.x.x同样也安装成功了)

使用以下命令安装

1
npm install hexo-git-backup --save

创建分支

前往你的blog仓库,如图创建(我的分支名为backup,图中加了-test只为展示):

配置

到 Hexo 博客根目录的 _config.yml 配置文件里添加以下配置(将 2427768286 改为你自己的用户名即可):

1
2
3
4
5
6
backup:
type: git
theme: hexo-theme-matery
message: Back up my www.liuyao-blog blog
repository:
github: git@github.com:2427768286/2427768286.github.io.git,backup

参数解释:

theme:你要备份的主题名称
message:自定义提交信息,随便填
repository:仓库名,注意仓库地址后面要添加一个分支名,比如我就创建了一个backup 分支

备份

最后使用以下命令备份你的博客

1
hexo backup

备份成功后可以在你的仓库分支下看到备份的原始文件:

还原

先安装好 git、ssh、node、hexo,使用 git clone 克隆 backup分支的源文件,--recursive 参数可以同步还原 submodule

1
git clone -b backup git@github.com:2427768286/2427768286.github.io.git --recursive

如果已将 backup 分支设置为默认分支,可省略 -b source 参数

也可以使用 git submodule initgit submodule update 进行子模块的还原

如果想要更新 hexo 到最新版本,可以通过 npm update 命令进行更新

转移后遇到的一系列问题

需升级node版本

报错内容
1
2
3
4
5
6
7
8
9
10
fatal: in unpopulated submodule '.deploy_git'
FATAL {
err: Error: Spawn failed
at ChildProcess.<anonymous> (D:\workplace\blog\node_modules\hexo-util\lib\spawn.js:51:21)
at ChildProcess.emit (events.js:376:20)
at ChildProcess.cp.emit (D:\workplace\blog\node_modules\cross-spawn\lib\enoent.js:34:29)
at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12) {
code: 128
}
}
1
2
3
4
5
6
7
8
9
10
fatal: the remote end hung up unexpectedly
Everything up-to-date
FATAL {
err: Error: Spawn failed
at ChildProcess.<anonymous> (E:\blog\program\2427768286.github.io\node_modules\hexo-util\lib\spawn.js:51:21)
at ChildProcess.emit (node:events:527:28)
at cp.emit (E:\blog\program\2427768286.github.io\node_modules\cross-spawn\lib\enoent.js:34:29)
at ChildProcess._handle.onexit (node:internal/child_process:291:12) {
code: 1
}
把根目录下的 .deploy_git 目录删除即可
删除用代码
1
2
3
4
5
6
7
8
9
10
## 进入站点根目录

## 删除git提交内容文件夹
rm -rf .deploy_git/

## 执行
git config --global core.autocrlf false

## 最后
hexo clean && hexo g && hexo d
报错内容
第1种
1
2
error: src refspec master does not match any
error: failed to push some refs to 'github'
第2种
1
2
error: src refspec master does not match any
error: failed to push some refs to 'github.com:2427768286/2427768286.github.io.git'
解决方法
第1种

把本地的 backup 仓库名称修改为远端的 hexo(默认分支)

最后蓝色括号内即为本地仓库名
重命名命令:

1
git branch -m oldBranchName newBranchName

将本地 backup 改为 hexo 命令:
1
git branch -m backup hexo

第2种

把本地的 hexo 仓库名称修改为远端的 master

重命名命令:

1
git branch -m oldBranchName newBranchName

将本地 hexo 改为 master 命令:
1
git branch -m hexo master

报错内容
1
2
fatal: 'github' does not appear to be a git repository
fatal: Could not read from remote repository.
解决方法

输入命令查看是否列出了报错内容中的”github”

1
git remote -v

若没有则意味着 your “github” is missing.
接下来需要使用以下命令行添加 “github”(将 2427768286 改为你的用户名即可)
1
git remote add github git@github.com:2427768286/2427768286.github.io.git

若报错

1
error: remote github already exists.

则使用以下命令行(将 2427768286 改为你的用户名即可)
1
git remote set-url github git@github.com:2427768286/2427768286.github.io.git