hexo博客源码自动备份

  1. 在项目的目录打开一个控制台窗口,执行如下命令初始化Git
1
git init
  1. 在项目目录新建一个 .gitignore 文件,如果有直接打开,添加以下代码
1
2
3
4
5
6
7
8
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/
_multiconfig.yml
  1. 执行完之后到 GitHub 或者 Gitee 创建一个仓库,然后在刚才的控制台继续执行
1
2
3
git add .
# 你刚才创建的仓库地址
git remote add origin https://github.com/xxxxxxxxxxxxxxx.git
  1. 然后在你的项目创建一个 scripts 目录,在里面创建一个文件 auto_backup.js ,将一下代码添加到文件中
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
require('shelljs/global');
const path = require('path');
try {
hexo.on('deployAfter', function() {//当deploy完成后执行备份
const commitMessageIndex = process.argv.indexOf('-m');
const commitMessage = commitMessageIndex !== -1 ? process.argv.slice(commitMessageIndex + 1).join(' ') : "blog auto backup script's commit";
run(commitMessage);
});

} catch (e) {
console.log("产生了一个错误啊<( ̄3 ̄)> !,错误详情为:" + e.toString());
}
function run(commitMessage) {
if (!which('git')) {
echo('Sorry, this script requires git');
exit(1);
} else {
echo("======================自动备份源码开始======================");
echo('当前项目目录:', path.join(__dirname, '..'));
echo('本次提交信息:', commitMessage);
cd(path.join(__dirname, '..'));
if (exec('git add --all').code !== 0) {
echo('Error: Git add failed');
exit(1);
}
if (exec('git commit -am "' + commitMessage + '"').code !== 0) {
echo('Error: Git commit failed');
exit(1);
}
if (exec('git push origin master').code !== 0) {
echo('Error: Git push failed');
exit(1);
}
echo("======================自动备份源码结束======================")
}
}


  1. 这个脚本需要安装一个依赖,继续执行
1
npm install --save shelljs
  1. 然后在你执行 hexo d -g -m ‘提交信息’ 时就会自动开始备份
3a66c8606e295ee40b6e854d67655d2b.png

看到这个信息说明你已经成功了😝