使用 rsync deploy hexo 博客

hexo 支持多种部署方式, 如 git repos/rsync/Heroku/FTPSync等, 如果博客使用 github pages 托管, 那么用 git repos 的方式部署最方便了, 如果是自己的vps, rsync是一种不错的方式.

使用 hexo-deployer-rsync


配置, vim $SITE_HOME/_config.yml :

deploy:
type: rsync
host: <host>
user: <user>
root: <root>
port: [port]
delete: [true|false]
verbose: [true|false]
ignore_errors: [true|false]

执行 hexo generatehexo deploy :

......

[ '--delete',
'-v',
'-az',
'/home/niko/mount/hsb_ssd_1/niko/dev/code/git_repos/HexoBlog/hexo-hello/public/',
'-e ssh',
'--port=22222',
'root@104.224.xxx.xxx:www.nikoai.pw/' ]
ssh: connect to host 104.224.xxx.xxx port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: ssh: connect to host 104.224.xxx.xxx port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: ssh: connect to host 104.224.xxx.xxx port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]

at ChildProcess.<anonymous> (/home/niko/mount/hsb_ssd_1/niko/dev/code/git_repos/HexoBlog/hexo-hello/node_modules/hexo-util/lib/spaw
n.js:37:17)
at emitTwo (events.js:100:13)
at ChildProcess.emit (events.js:185:7)
at maybeClose (internal/child_process.js:850:16)
at Socket.<anonymous> (internal/child_process.js:323:11)
at emitOne (events.js:90:13)
at Socket.emit (events.js:182:7)
at Pipe._onclose (net.js:484:12)
FATAL ssh: connect to host 104.224.xxx.xxx port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]

Error: ssh: connect to host 104.224.xxx.xxx port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.0]

at ChildProcess.<anonymous> (/home/niko/mount/hsb_ssd_1/niko/dev/code/git_repos/HexoBlog/hexo-hello/node_modules/hexo-util/lib/spaw
n.js:37:17)
at emitTwo (events.js:100:13)
at ChildProcess.emit (events.js:185:7)
at maybeClose (internal/child_process.js:850:16)
at Socket.<anonymous> (internal/child_process.js:323:11)
at emitOne (events.js:90:13)
at Socket.emit (events.js:182:7)
at Pipe._onclose (net.js:484:12)

明明配置了非22的ssh端口, 但执行deploy时仍然连接22端口, 看了下代码 node_modules/hexo-deployer-rsync/lib/deployer.js, :

'use strict';

var chalk = require('chalk');
var spawn = require('hexo-util/lib/spawn');

module.exports = function(args) {
if (!args.host || !args.user || !args.root) {
var help = '';

help += 'You should configure deployment settings in _config.yml first!\n\n';
help += 'Example:\n';
help += ' deploy:\n';
help += ' type: rsync\n';
help += ' host: <host>\n';
help += ' user: <user>\n';
help += ' root: <root>\n';
help += ' port: [port] # Default is 22\n';
help += ' delete: [true|false] # Default is true\n';
help += ' args: <rsync args>\n';
help += ' verbose: [true|false] # Default is true\n';
help += ' ignore_errors: [true|false] # Default is false\n\n';
help += 'For more help, you can check the docs: ' + chalk.underline('http://hexo.io/docs/deployment.html');

console.log(help);
return;
}

console.log("niko debug:");
console.log(args);

if (!args.hasOwnProperty('port')) args.port = 22;
if (!args.hasOwnProperty('delete')) args.delete = true;
if (!args.hasOwnProperty('verbose')) args.verbose = true;
if (!args.hasOwnProperty('ignore_errors')) args.ignore_errors = false;

var params = [
'-az',
this.public_dir,
'-e ssh',
args.user + '@' + args.host + ':' + args.root
];

if (args.port && args.port > 0 && args.port < 65536) {
params.splice(params.length - 1, 0, '--port=' + args.port);
}

if (args.verbose) params.unshift('-v');
if (args.ignore_errors) params.unshift('--ignore-errors');
if (args.delete) params.unshift('--delete');
if (args.args) params.unshift(args.args);

console.log(params);

return spawn('rsync', params, {verbose: true});
};

好像发现了问题, 从上面代码可知, deployer 也是 spawn rsync 来进行发布的, hexo 配置里的参数和值会转换成rsync的. 但是指定port的代码有点问题, 需要做以下修改:

'-e ssh',

改成

"-e 'ssh -p " + args.port + "'",

写成一个sh脚本


上面的操作略为麻烦, 不用hexo的插件, 也可以写成一个shell文件, 下次直接执行即可:

vim deploy_to_vps.sh

#!/bin/sh
rsync -rvz -e 'ssh -p 22222' --progress -a ./public user@host:/data/hexo-blog

参考


http://stackoverflow.com/questions/4549945/is-it-possible-to-specify-a-different-ssh-port-when-using-rsync
https://hexo.io/zh-cn/docs/deployment.html