思路

因为本博客是基于Hexo+GitHub-Pages,所以在hexo -d后生成的静态文件都存放于github,所以这是不用担心的。本地的hexo博客的源文件内存放着大量的文章markdown文件,所以问题来了。如果换电脑或者硬件损坏,或者有在其他机器更新文章的需求。就需要做一件事情就是将源文件,也就是本地hexo博客源文件的根目录整个备份。

在那么现在就必须做一些前置操作,就是把你的hexo博客源文件备份到GitHub或者其他代码托管平台,这里的操作就不写出来了,可以参考罪恶的起源,git初识里的如何将代码提交到远程仓库。

也可以使用开源中国的码云,我就是用的码云[手动滑稽],操作都是一样的。

清除hexo的public目录=>重新生成静态文件=>更新github pages的文章内容=>拉取其他机器的提交=>将所有的文章Markdown源文件改动add到本地暂存=>提交=>push源文件到另一个备份仓库

那么这个命令如何实现,在上一篇文章的额基础上,我对三个命令做出了拓展,使它有一些基本的功能,比如help,防止自己也忘了这个命令干嘛的。加入了只是本地预览博客的指令-s。已经一些友好的提示,让你知道它都做了什么。那么来看看这个命令的脚本长什么样吧!

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
#  by StruggleYang
# this is update hexo page or post and backup source
this=$0
message=$1
help="help"
server="-s"
# Always pull the latest
git pull origin master &&
if [ -n "$message" ]
then
if [ "$message" = "$help" ]
then
echo " $this>>help : "
echo " There is only one parameter: commit-message or -s [locahost server preview Blog] "
echo " After the command, add updates to this blog and submit it to the backup repository!"
elif [ "$message" = "$server" ]
then
echo " $this ====hexo >> clean >> generate >> server "
echo "==========hexo clean ...===========" &&
hexo clean &&
echo "==========hexo generate ...=========" &&
hexo g &&
echo "==========hexo server ...=========" &&
hexo s
else
echo " $this ==== [hexo] >> clean >> generate >> deploy ==|~|== [git] >> add >> commit >> push "
echo "==========hexo clean ...===========" &&
hexo clean &&
echo "==========hexo generate ...=========" &&
hexo g &&
echo "==========hexo deploy ...===========" &&
hexo d &&
echo "==========backup hexo source to git.osc ...=========" &&
git add -A . &&
echo "==========commit message: [ $message ]=========" &&
git commit -m "$message" && git push origin master
fi
else
echo " $this>>help : "
echo " There is only one parameter: commit-message or help or -s [locahost server preview Blog]"
echo " After the command, add updates to this blog and submit it to the backup repository!"
fi

echo " ==========Over==========="

假设已经将此脚本命名为up.sh

当我输入 sh up.sh时:

1
2
3
4
5
$ sh up.sh
up.sh>>help :
There is only one parameter: commit-message or help or -s [locahost server preview Blog]
After the command, add updates to this blog and submit it to the backup repository!
==========Over===========

当我输入sh up.sh help提示和以上一样,所以我是希望能够在每次更新和提交时,能够足够便捷,并且能够提示我填写commit的desc
还有一个指令是-s ,这个指令的作用显而易见只是将hexo的page重新生成并且能够本地预览。获取并不想提交更新,或者只是调试!

当我真正的需要更新文章或者博客的布局,并且备份元数据!
只需要这样写:

1
$ sh up.sh "this is commit message test"

运行如下:

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
F:\hexo (master)                                                                                  
λ sh up.sh "this is commit message test"
up.sh ==== [hexo] >> clean >> generate >> deploy ==|~|== [git] >> add >> commit >> push
==========hexo clean ...===========
INFO Deleted database.
INFO Deleted public folder.
==========hexo generate ...=========
INFO Start processing
INFO Files loaded in 1.4 s
INFO Generated: content.json
INFO Generated: index.html
INFO Generated: tools/index.html
INFO Generated: soul/index.html
........省略部分
==========hexo deploy ...===========
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
........省略部分
INFO Deploy done: git
==========backup hexo source to git.osc ...=========
warning: LF will be replaced by CRLF in source/_posts/hexo-all-update-by-shell.md.
The file will have its original line endings in your working directory.
==========commit message: [ this is commit message test ]=========
3 files changed, 34 insertions(+), 2 deletions(-)
Counting objects: 27, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (27/27), done.
Writing objects: 100% (27/27), 14.86 KiB | 0 bytes/s, done.
Total 27 (delta 14), reused 0 (delta 0)
To https://git.oschina.net/StruggleYang/hexo.git
d9838c0..50bb11d master -> master
==========Over===========

如何多机更新

等你换了电脑或者公司的电脑来更新的的时候。前提是这台电脑是需要有node.js和npm以及hexo环境的。那只需要将备份的元数据clone到你想要的位置。

执行:

1
$ npm install

然后更新文章,继续使用这个脚本来使得hexo变得更为轻松!

事实证明,这样的方式暂时是没有什么问题的。并且非常的方便!