Hexo文件备份的正确姿势

作者:kozo4    发布于:

请在用root权限的情况下使用本文的命令

    sudo su


hexo-Git-Backup

Github项目地址

使用npm可能会出问题(你懂的)
安装cnpm并添加淘宝镜像,并安装nrm换源

安装 cnpm 后就可以正常使用了,不过这里还是用nrm把源换了,因为我有时会忘记,然后使用npm来安装,结果导致 debug

  #安装 cnpm
  npm install -g cnpm --registry=https://registry.npm.taobao.org
  #安装 nrm
  cnpm install nrm -g

查看可选的源

  nrm ls

输出以下内容

☁  ~  nrm ls

* npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/

使用use切换到淘宝镜像

  nrm use taobao

输出以下内容

☁  ~  nrm use taobao
                         verb config Skipping project config: /home/chenhonzhou/.npmrc. (matc

   Registry has been set to: https://registry.npm.taobao.org/


安装hexo备份的插件: hexo-git-backup

☁  ~  cd hexo                               
☁  hexo  sudo cnpm install hexo-git-backup --save
⠇ [0/1] Installing natives@^1.1.0[urllib:warn] [Wed Feb 21 2018 21:25:36 GMT+0800 (CST)] [18] [worker:30481] GET https://cdn.npm.taobao.org/lodash/-/lodash-2.4.2.tgz callback twice!!!
[urllib:warn] [Wed Feb 21 2018 21:25:36 GMT+0800 (CST)] [18] [worker:30481] ResponseError: read ECONNRESET (req "error")
stack: Error: read ECONNRESET
  at _errnoException (util.js:1003:13)
  at TLSWrap.onread (net.js:620:25)
[lodash@2.4.2] download ShasumNotMatchError: real sha1:230d9a56648183e8480955d29e9846341f0aa7f5 not equal to remote:fadd834b9683073da179b3eae6d9c0d15053f73e (lodash@2.4.2), fail count: 1
✔ Installed 1 packages
✔ Linked 187 latest versions
✔ Run 1 scripts
deprecate hexo-git-backup@0.1.2 › graceful-fs@^3.0.2 please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
deprecate hexo-git-backup@0.1.2 › swig@1.4.1 This package is no longer maintained
Recently updated (since 2018-02-14): 6 packages (detail see file /home/chenhonzhou/hexo/node_modules/.recently_updates.txt)
✔ All packages installed (192 packages installed from npm registry, used 7s, speed 1.2MB/s, json 188(2.48MB), tarball 5.36MB)

然后打开全局配置文件_config.yml,添加以下配置

backup:
    type: git
    repository:
       github: git@github.com:xxx/xxx.git,branchName
       gitcafe: git@github.com:xxx/xxx.git,branchName

例如我的

backup:
  type: git
  repository:
    github: git@github.com:chenhonzhou/hexo_backup.git,master

需要备份的使用只需执行

  hexo backup

或者使用短命令

  hexo b

执行了一下

INFO  Start backup: git
位于分支 master
无文件要提交,干净的工作区
Permission denied (publickey).
fatal: 无法读取远程仓库。

请确认您有正确的访问权限并且仓库存在。
INFO  Backup done: git

(你可以使用http),便可不需要下面的步骤

backup:
  type: git
  repository:
    github: http://github.com/chenhonzhou/hexo_backup.git,master

查了一下,原因没有配公钥
(我的原因,大家应该都配置好了,所以你可以直接soho)


#添加公钥
☁  hexo [master] ⚡  ssh-keygen -t rsa -C “chenhonzhou@gmail.com”
Generating public/private rsa key pair.
Enter file in which to save the key (/home/chenhonzhou/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/chenhonzhou/.ssh/id_rsa.
Your public key has been saved in /home/chenhonzhou/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:g7STIrkzExL5cGaxsFOcN+rujDDjuKmj7+ThE4SqnSA “chenhonzhou@gmail.com”
The key's randomart image is:
+---[RSA 2048]----+
|..o.             |
| =ooo            |
|*.=o ..          |
|.Oo. . +         |
|oo= . = S        |
|E.o+ . . .       |
|*+Bo             |
|*X==             |
|XBB.             |
+----[SHA256]-----+

这样就有了.ssh目录,这个目录下有id_rsa和id_rsa.pub这两个文件

这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

登陆GitHub,打开“Account settings”,“SSH Keys”页面:

然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容

这样即可成功关联远程仓库

☁  ~  cd .ssh
☁  .ssh  ls
id_rsa  id_rsa.pub  known_hosts
☁  .ssh  vi id_rsa.pub

删除目录里的.git,并使用hexo b便可备份
(有点慢,毕竟是强制更新,大家需要耐心等待)


手动备份

新建一个仓库
image
或者新建一个分支,执行

git branch hexo-bakcup
git checkout hexo-bakcup
rm -rf *

上面的操作是新建了一个hexo-bakcup分支,并删除了所有文件

然后到你的Hexo目录下,复制source | themes | _config.yml | package.json 文件到那个分支中,新建的仓库同.

(source为你的文章其中_posts为上传的文章,themes为主题,_config.yml为hexo配置文件,package.json为一些插件配置)

cp -rf source/ ~/Git/hexo-blog

cp -rf themes/ ~/Git/hexo-blog

cp _config.yml ~/Git/hexo-blog

cp package.json ~/Git/hexo-blog

注意!在此之前一定要删除themes文件下的目录的.git文件

不然上传不上去,对此我深有体会,搞这个问题搞了大半天


cd themes/youtheme
sudo rm -rf .git

上传到Github上
image
然后下次到另一台电脑上的时候执行初始化后
执行

  cp ~/Git/hexo-blog/* ~/node/hexo-blog/
  npm install

即可

format_list_numbered

(无)

  1. 1. hexo-Git-Backup
  2. 2. 手动备份
arrow_upward

Power of Kozo4 备案号: 备你他妈的案