自动备份 Hexo 博客源文件

前言:

  • 用 Hexo 写博客是一件比较享受的事情,无奈如果换电脑的话,备份博客就是一件比较闹心的事情。
  • 我曾经给出过通过 Git 备份 Hexo 博客源文件的方式,这种方式虽然能够备份 Hexo 博客的源文件,但是对于博主这种懒人,每次更新博文都需要输入两三行重复的 Git 命令真是一件麻烦的事情。况且指不定哪天就搞忘 push 到 github 上了。

原理

  • 前两天博主刚刚编写过关于 Hexo 添加文章时自动打开编辑器 的相关文章,其原理就是利用 NodeJS 的事件监听机制实现监听 Hexo 的 new 事件来启动编辑器,完成自动启动编辑器的操作。

  • 那么可不可以通过通过监听 Hexo 的其它事件来完成自动执行 Git 命令完成自动备份呢?通过查阅 Hexo 文档 ,找到了 Hexo 的主要事件,见下表:

    事件名 事件发生时间
    deployBefore 在部署完成前发布
    deployAfter 在部署成功后发布
    exit 在 Hexo 结束前发布
    generateBefore 在静态文件生成前发布
    generateAfter 在静态文件生成后发布
    new 在文章文件建立后发布
  • 于是我们就可以通过监听 Hexo 的 deployAfte 事件,待上传完成之后自动运行 Git 备份命令,从而达到自动备份的目的。

实现

  1. 将 Hexo 目录加入 Git 仓库

    • 本脚本需要 提前 将 Hexo 加入 Git 仓库并与 Github 或者 Gitcafe 远程仓库绑定之后,才能正常工作。如果你不知道怎么操作,请参考这篇博文:
    • 备份 Hexo 博客源文件
  2. 安装 shelljs 模块

    • 要实现这个自动备份功能,需要依赖 NodeJs 的一个 shelljs 模块,该模块重新包装了 child_process , 调用系统命令更加的方便。(其实就是因为博主懒 (╯▽╰))该模块需要安装后使用。
    • 在命令中键入以下命令,完成 shelljs 模块的安装:
      1
      npm install --save shelljs
  3. 编写自动备份脚本

    • 待到模块安装完成,在 Hexo 根目录的 scripts 文件夹下新建一个 js 文件,文件名随意取。 如果没有 scripts 目录,请新建一个。
    • 然后在脚本中,写入以下内容:
      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
      require('shelljs/global');

      try {
      hexo.on('deployAfter', function() {//当deploy完成后执行备份
      run();
      });
      } catch (e) {
      console.log("产生了一个错误<( ̄3 ̄)> !,错误详情为:" + e.toString());
      }

      function run() {
      if (!which('git')) {
      echo('Sorry, this script requires git');
      exit(1);
      } else {
      echo("======================Auto Backup Begin===========================");
      cd('E:/Hexo'); //此处修改为Hexo根目录路径
      if (exec('git add --all').code !== 0) {
      echo('Error: Git add failed');
      exit(1);

      }
      if (exec('git commit -am "Form auto backup script\'s commit"').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("==================Auto Backup Complete============================")
      }
      }
    • 其中,需要修改第 17 行的 E:/Hexo 路径为 Hexo 的根目录路径。(脚本中的路径为博主的 Hexo 路径)
    • 如果你的 Git 远程仓库名称不为 origin 的话,还需要修改第 28 行执行的 push 命令,修改成自己的远程仓库名和相应的分支名。
    • 保存脚本并退出,然后执行 hexo deploy 命令,将会得到类似以下结果:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      INFO  Deploying: git>
      INFO Clearing .deploy folder...
      INFO Copying files from public folder...
      [master 3020788] Site updated: 2015-07-06 15:08:06
      5 files changed, 160 insertions(+), 58 deletions(-)
      Branch master set up to track remote branch gh-pages from git@github.com:smilexi
      amo/notes.git.
      To git@github.com:smilexiamo/notes.git
      02adbe4..3020788 master -> gh-pages
      On branch master
      nothing to commit, working directory clean
      Branch master set up to track remote branch gitcafe-pages from git@gitcafe.com:s
      milexiamo/smilexiamo.git.
      To git@gitcafe.com:smilexiamo/smilexiamo.git
      02adbe4..3020788 master -> gitcafe-pages
      INFO Deploy done: git
      ======================Auto Backup Begin===========================
      [master f044360] Form auto backup script's commit
      2 files changed, 35 insertions(+), 2 deletions(-)
      rewrite db.json (100%)
      To git@github.com:smilexiamo/hexo.git
      8f2b4b4..f044360 master -> master
      ==================Auto Backup Complete============================
    • 这样子,每次更新博文并 deploy 到服务器上之后,备份就自动启动并完成备份啦~是不是很方便呢?