一键部署Hexo环境以及恢复Blog数据

版本介绍

v0.1

  • 支持Centos
  • 支持自定义恢复目录

v0.2

  • 支持Ubuntu、Centos7
  • 新增支持出现错误终止(使用bash执行脚本,若出现错误将自动退出脚本并且返回状态码,状态码设置为出错的项的行号。)
  • 隐藏不必要输出
  • 新增clean参数(可在脚本执行完毕后清理压缩文件)
  • 新增restart参数(清理已经安装的nodejs以及解压的数据包)
  • 参数支持混合使用(使用案例:bash recovery-blog.sh clean restart)

v1.0

  • 优化脚本
  • 增加检查架构的操作
  • 支持修复npm module初始化本地仓库并连接远程仓库
    • ri 参数
  • 更多新增参数,可以使用help 参数查看使用方法)
1
2
3
4
5
6
7
8
9
Usage: <Command> [OPTION] [FILE]
env: Only install the necessary utils.
repair: Repair npm module errors.
init: Link the remote reposity with local reposity.
ri: Repair and init.
unzip: Only unpack the datafile.
clean: Clean the necessary files.
start: Start recovery nomarlly.
restart: Reinstall nodejs and re-unzip your blog.

使用

将备份好的Blog数据放在currentPath所指目录下,运行脚本即可。

Code

Version:0.1

1
2
touch recover-blog.sh
vi recover-blog.sh
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#一键部署Hexo环境以及恢复Blog数据
####
blog="myblog" #博客数据包解压后的文件夹名
currentPath="/home/www" #博客所在目录
nodeurl="https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.xz" # nodejs 版本链接(可自定义)
####
## git
yum install -y git
if [ "$?" == 0 ];then
echo "Note: 'git' installed successfully!"
else
echo "Error: Please Check 'git'"
fi
## node
#yum install -y nodejs
#if [ "$?" == 0 ];then
# echo "Note: 'nodejs' installed successfully!"
#else
# echo "Error: Please Check 'nodejs'"
#fi
### 手动安装nodejs
## wget
yum install -y wget
if [ "$?" == 0 ];then
echo "Note: 'wget' installed successfully!"
else
echo "Error: Please Check 'wget'"
fi
if [ ! -e "nodejs" -a ! -e "nodejs.tar.xz" ];then
wget $nodeurl -O nodejs.tar.xz
fi
if [ -e "nodejs.tar.xz" ];then
tar -xf $currentPath/nodejs.tar.xz
rm -f $currentPath/nodejs.tar.xz
mv `find /home/www -name node-v[0-9]*` /home/www/nodejs
fi
# 重新创建nodejs软链接
rm -f /usr/local/bin/node
ln -s $currentPath/nodejs/bin/node /usr/local/bin/node
rm -f /usr/local/bin/npm
ln -s $currentPath/nodejs/bin/npm /usr/local/bin/npm

node -v
if [ "$?" == 0 ];then
echo "Note: 'nodejs' installed successfully!"
else
echo "Error: Please Check 'nodejs'"
fi
## npm
npm -v
if [ "$?" == 0 ];then
echo "Note: 'npm' installed successfully!"
else
echo "Error: Please Check 'npm'"
fi
## 安装Hexo
npm install --location=global hexo-cli
### 链接hexo命令至环境变量中
rm -f /usr/local/bin/hexo
ln -s $currentPath/nodejs/lib/node_modules/hexo-cli/bin/hexo /usr/local/bin/hexo

## 还原数据
### 解压数据包
if [ ! -e $blog ];then
tar -xf `find [0-9]*_[0-9]*.tar.gz`
fi
if [ "$?" == 0 ];then
echo "Tips: You can use commend:cd switching to $blog and use commend:hexo server to preview your site now!"
fi

Version:0.2

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
#一键部署Hexo环境以及恢复Blog数据
####
blog="myblog" #博客数据包解压后的文件夹名
currentPath="/home/www" #博客所在目录
dataFile="[0-9]*_[0-9]*.tar.gz" #可定义数据文件名
nodeurl="https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.xz" # nodejs版本链接(可自定义)
####
if [ "$1" == "restart" -o "$2" == "restart" ];then
echo -e "\nNote: Cleaning nodejs & $blog..."
nohup rm -rf $currentPath/nodejs &>/dev/null
nohup rm -rf $currentPath/$blog &>/dev/null
sleep 1
fi
####检查当前操作系统
echo -e "Recovery starting..."
OS=`cat /etc/os-release|grep ^NAME=".*"|awk -F "\"" '{print $2}'`
if [ "$OS" == "CentOS Linux" -o "$OS" == "CentOS" ];then
pkgm="yum"
elif [ "$OS" == "Ubuntu" ];then
pkgm="apt-get"
fi
nohup sudo $pkgm update -y --skip-broken &>/dev/null
####
## git
nohup $pkgm install -y git &>/dev/null
if [ "$?" == 0 ];then
echo -e "Note: 'git' installed successfully!"
else
echo -e "\nError: Please check 'git'\n"
exit 19
fi

## wget
nohup $pkgm install -y wget &>/dev/null
if [ "$?" == 0 ];then
echo -e "Note: 'wget' installed successfully!"
else
echo -e "\nError: Please check 'wget'\n"
exit 28
fi
if [ ! -e "nodejs" -a ! -e "nodejs.tar.xz" ];then
wget $nodeurl -O nodejs.tar.xz
fi
if [ ! -e "nodejs" -a -e "nodejs.tar.xz" ];then
tar -xf $currentPath/nodejs.tar.xz
mv `find $currentPath -name node-v[0-9]*` $currentPath/nodejs
fi
# 重新创建nodejs软链接
rm -f /usr/local/bin/node
ln -s $currentPath/nodejs/bin/node /usr/local/bin/node
rm -f /usr/local/bin/npm
ln -s $currentPath/nodejs/bin/npm /usr/local/bin/npm
if [ "$?" -eq 0 ];then
echo -e "Note: nodejs"
node -v
fi
if [ "$?" == 0 ];then
echo -e "Installed successfully!"
else
echo -e "\nError: Please check 'nodejs'\n"
exit 44
fi
## npm
if [ "$?" -eq 0 ];then
echo -e "Note: npm"
npm -v
fi
if [ "$?" == 0 ];then
echo -e "Installed successfully!"
else
echo -e "\nError: Please check 'npm'\n"
exit 57
fi
## 安装Hexo
nohup hexo &>/dev/null
if [ "$?" -ne 0 ];then
echo ""
nohup npm install --location=global hexo-cli &>/dev/null
fi
### 链接hexo命令至环境变量中
rm -f /usr/local/bin/hexo
ln -s $currentPath/nodejs/lib/node_modules/hexo-cli/bin/hexo /usr/local/bin/hexo

## 还原数据
### 解压数据包

if [ "$?" -ne 0 ];then
echo "Error: Please check Your 'DataFile'!"
elif [ ! -e $blog ];then
tar -xf $dataFile
fi
if [ "$?" -eq 0 ];then
echo -e "Note: Now,you can switch to '$blog' and use 'hexo server' to preview your site!\nDone"
else
echo -e "\nError: Please check 'tar -xf <dataFile>.'\n"
exit 80
fi
if [ "$1" == "clean" -o "$2" == "clean" ];then
echo "Cleaning Starting..."
if [ ! -e $currentPath/nodejs.tar.xz -a ! -e $currentPath/$dataFile ];then
echo "Note: Files not exist."
fi
if [ -e $currentPath/nodejs.tar.xz -a -e $currentPath/$dataFile ];then
nohup rm -f $currentPath/nodejs.tar.xz &>/dev/null
nohup rm -f $currentPath/$dataFile &>/dev/null
echo -e "Note: Compressed files was cleaned."
fi
if [ -e $currentPath/nodejs.tar.xz ];then
nohup rm -f $currentPath/nodejs.tar.xz &>/dev/null
echo -e "Note: Compressed files was cleaned."
fi
if [ -e $currentPath/$dataFile ];then
nohup rm -f $currentPath/$dataFile &>/dev/null
echo -e "Note: Compressed files was cleaned."
fi
fi

Version: 1.0

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
#一键部署Hexo环境以及恢复Blog数据
####
# 使用步骤
# 1.cd切换至需要恢复到的目录下
# 2.准备好从github中下载的源码(.zip),或者直接git clone(推荐)
# 3.准备脚本r.sh,赋予可执行权限,注意修改nodejs的版本
# 4.执行./r.sh start <指定压缩文件>,
# 5.
####
blog="myblog" #博客数据包解压后的文件夹名
currentPath=$PWD #博客所在目录
dataFile='blog-source.zip' #可定义数据文件名
nodeVersion="v16.16.0"
nodeurl="https://nodejs.org/dist/$nodeVersion/node-$nodeVersion-linux-$arch.tar.xz" #自动检查
####用于git配置
email=asucanyh@outlook.com
username=asucanyh-cn
remoteRepo=blog-source
####
function checkFileName() {
if [ $# -eq 2 ]; then
echo "[Info]Checking filename..."
if [ ${#2} -lt 3 ]; then
echo "[Error]Please check your name of data file!"
exit 17
fi
dataFile=$2
if [ ${dataFile:0-3} == ".gz" -o ${dataFile:0-3} == "zip" ]; then
if [ $# -eq 1 -a $1 == "start" ]; then
echo -e "[Note]You are running without data file,you'd better specify it!"
elif [ $# -eq 1 -a $1 == "restart" ]; then
echo -e "[Note]You are running without data file,you'd better specify it!"
fi
else
echo "[Error]Wrong type of compressed file.Please check your file name parameter!"
exit 32
fi
echo "[Info]$dataFile passed."
fi
}
function cleanFiles() {
echo -e "[Info]Cleaning nodejs & $blog..."
nohup rm -rf /usr/local/nodejs &>/dev/null
nohup rm -rf $currentPath/$blog &>/dev/null
sleep 1
}
function clean() {
#清除所有无用的文件
echo "Cleaning Starting..."
if [ ! -e $currentPath/nodejs.tar.xz -a ! -e $currentPath/$dataFile ]; then
echo "[Note]Files not exist."
fi
if [ -e $currentPath/nodejs.tar.xz -a -e $currentPath/$dataFile ]; then
nohup rm -f $currentPath/nodejs.tar.xz &>/dev/null
nohup rm -f $currentPath/$dataFile &>/dev/null
echo -e "[Info]Compressed files was cleaned."
fi
if [ -e $currentPath/nodejs.tar.xz ]; then
nohup rm -f $currentPath/nodejs.tar.xz &>/dev/null
echo -e "[Info]nodejs.tar.xz was cleaned."
fi
if [ -e $currentPath/$dataFile ]; then
nohup rm -f $currentPath/$dataFile &>/dev/null
echo -e "[Info]$dataFile was cleaned."
fi

}
function checkOS() {
####检查发行版本
echo -e "[Note]Recovery starting..."
OS=$(cat /etc/os-release | grep ^NAME=".*" | awk -F "\"" '{print $2}')
if [ "$OS" == "CentOS Linux" -o "$OS" == "CentOS" ]; then
pkgm="yum"
elif [ "$OS" == "Ubuntu" ]; then
pkgm="apt-get"
fi
nohup sudo $pkgm update -y --skip-broken &>/dev/null
#####检查架构
if [ "$(dpkg --print-architecture)" == "arm64" ]; then
arch="arm64"
else
arch="x64"
fi
}
function env() {
## 安装git
nohup $pkgm install -y git &>/dev/null
if [ "$?" -eq 0 ]; then
echo -e "[Info]'git' installed successfully!"
else
echo -e "\n[Error]Please check 'git'\n"
exit 19
fi

## 安装wget
nohup $pkgm install -y wget &>/dev/null
if [ "$?" -eq 0 ]; then
echo -e "[Info]'wget' installed successfully!"
else
echo -e "\n[Error]Please check 'wget'\n"
exit 28
fi
if [ ! -e "nodejs" -a ! -e "nodejs.tar.xz" ]; then
wget $nodeurl -O nodejs.tar.xz
fi
if [ ! -e "nodejs" -a -e "nodejs.tar.xz" ]; then
tar -xf $currentPath/nodejs.tar.xz
mv $(find $currentPath -name node-v[0-9]*) $currentPath/nodejs
fi
# 重新创建nodejs软链接
###
rm -rf /usr/local/nodejs
mv $currentPath/nodejs /usr/local
###
rm -f /usr/local/bin/node
ln -s /usr/local/nodejs/bin/node /usr/local/bin/node
rm -f /usr/local/bin/npm
ln -s /usr/local/nodejs/bin/npm /usr/local/bin/npm
if [ "$?" -eq 0 ]; then
nohup node -v &>/dev/null
fi
if [ "$?" == 0 ]; then
echo -e "[Info]'nodejs' installed successfully!"
else
echo -e "\n[Error]Please check 'nodejs'\n"
exit 44
fi
## npm
if [ "$?" -eq 0 ]; then
nohup npm -v &>/dev/null
fi
if [ "$?" == 0 ]; then
echo -e "[Info]'npm' installed successfully!"
else
echo -e "\n[Error]Please check 'npm'\n"
exit 57
fi
## 安装Hexo
nohup hexo &>/dev/null
if [ "$?" -ne 0 ]; then
nohup npm install --location=global hexo-cli &>/dev/null
fi
### 链接hexo命令至环境变量中
rm -f /usr/local/bin/hexo
ln -s /usr/local/nodejs/lib/node_modules/hexo-cli/bin/hexo /usr/local/bin/hexo
}
function unzip() {
if [ $# -eq 1 ]; then
echo -e "[Note]Please specify the datafile."
exit 25
fi
echo -e "[Note]Starting unpack $2."
## 还原数据
### 解压数据包 并修改文件夹为$blog
if [ ! -d "$currentPath/$blog" ]; then
if [ "${dataFile:0-3}" == ".gz" ]; then
tar -xf $currentPath/$dataFile
if [ $? -eq 0 ]; then
mv $currentPath/${dataFile%*.tar.gz} $currentPath/$blog
fi
elif [ ${dataFile:0-3} == "zip" ]; then
nohup $pkgm install zip -y &>/dev/null
nohup unzip $currentPath/$dataFile &>/dev/null
if [ $? -eq 0 ]; then
mv $currentPath/${dataFile%*.zip} $currentPath/$blog
fi
fi
fi
if [ -d $currentPath/$blog ]; then
echo -e "[Info]Unpack done."
echo -e "[Info]Now,you can switch to '$blog' and use 'hexo server' to preview your site!\n[Note]Done"
else
if [ "$1" == "start" -a ! "$#" -eq 1 ]; then
echo -e "\n[Error]Please check 'tar -xf $dataFile or 'unzip $dataFile.'\n"
exit 80
elif [ "$1" == "restart" -a ! "$#" -eq 1 ]; then
echo -e "\n[Error]Please check 'tar -xf $dataFile or 'unzip $dataFile.'\n"
exit 80
fi
fi
}
function gitConfig() {
git config --global user.email "$email"
git config --global user.name "$username"
git config --global credential.helper store
git config --global http.sslVerify false
git config --global init.defaultBranch main
}
function repairNPM() {
cd $blog
echo -e "[Info]Repairing the errors,it may take a long time,please be patient!"
rm -rf node_modules && nohup npm install --force &>/dev/null
rm -rf .deploy_git/
# git config --global core.autocrlf false
cd ../
echo -e "[Info]Errors repaired."
}
function initGit() {
cd $blog
git init
git add .
git remote add origin https://github.com/$username/$remoteRepo.git
git remote update
git checkout main
cd ../
}

##main
checkFileName $1 $2
case $1 in
"start")
checkOS
env
gitConfig
unzip $1 $2
;;
"env")
checkOS
env
;;
"restart")
checkOS
cleanFiles
env
gitConfig
unzip $1 $2
;;
"unzip")
unzip
;;
"repair")
repairNPM
;;
"init")
initGit
;;
"ri")
repairNPM
initGit
;;
"clean")
clean
;;
"help")
echo -e "Usage: <Command> [OPTION] [FILE]"
echo -e " env: Only install the necessary utils."
echo -e " repair: Repair npm module errors."
echo -e " init: Link the remote reposity with local reposity."
echo -e " ri: Repair and init."
echo -e " unzip: Only unpack the datafile."
echo -e " clean: Clean the necessary files."
echo -e " start: Start recovery nomarlly."
echo -e " restart: Reinstall nodejs and re-unzip your blog.\n"
;;
*)
echo "[!] invalid input"
echo "[Note]Use option: 'start' to began recovery."
echo "[Note]Try 'help' for more information."
;;
esac

报错解决方案

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
FATAL Something’s wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ChildProcess. (/var/www/myblog/node_modules/hexo-util/lib/spawn.js:51:21)
at ChildProcess.emit (node:events:527:28)
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)

https://blog.csdn.net/benisarookie/article/details/113114604

1
2
3
4
# 添加 私钥 到.ssh文件下
# 修改 私钥 的权限为600 (rw)
ssh-agent bash
ssh-add <私钥文件>