本文章介绍如何通过脚本自动备份并提交Hexo源码文件,包括 sourcescaffoldsthemes目录文件,及

package.json_config.yml等文件,当然,需要的话也可备份博客主目录下所有目录及文件。如果想要了解更多关于Hexo目录结构的内容,可以参阅文章Hexo 目录结构

自动备份

创建一个私有仓库用于存放Hexo源码文件,例如 blog_source

在博客主目录创建一个.ps1(powershell脚本)后缀的文件,例如 backup.ps1,粘贴下面的代码

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
# Author: 時光
# Blog: https://shiguang666.eu.org
# GitHub: https://github.com/shiguang

param (
[alias("o")]
[switch]$overwrite = $false
)

# 设置PowerShell的输出编码为UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 设置Hexo博客源码目录
$HEXO_SOURCE_DIR = "D:\hexo\shiguang-coding\blog"

# 设置备份目录
$BACKUP_DIR = "D:\Workspace\shiguang-coding\blog_source"

# 输出开始备份的提示信息
Write-Host "Starting Hexo blog source backup..."

# 检查备份目录是否存在
if (Test-Path -Path $BACKUP_DIR) {
if ($overwrite) {
# 覆盖同名文件,但不删除原有文件
Write-Host "Overwriting existing files in backup directory..."
Get-ChildItem -Path $HEXO_SOURCE_DIR -Recurse | ForEach-Object {
if ($_.Name -ne ".gitignore") {
$destinationPath = Join-Path -Path $BACKUP_DIR -ChildPath $_.FullName.Substring($HEXO_SOURCE_DIR.Length + 1)
if (Test-Path -Path $destinationPath) {
Remove-Item -Path $destinationPath -Force -Recurse
}
Copy-Item -Path $_.FullName -Destination $destinationPath -Force
} else {
Write-Host "Ignoring file: $($_.FullName)"
}
}
} else {
Write-Host "Backup directory already exists. Use -o parameter to overwrite it."
exit
}
} else {
# 创建备份目录
Write-Host "Creating backup directory: $BACKUP_DIR"
New-Item -ItemType Directory -Path $BACKUP_DIR -Force

# 复制Hexo博客源码到备份目录
Write-Host "Copying Hexo blog source to backup directory..."
Get-ChildItem -Path $HEXO_SOURCE_DIR -Recurse | ForEach-Object {
if ($_.Name -ne ".gitignore") {
$destinationPath = Join-Path -Path $BACKUP_DIR -ChildPath $_.FullName.Substring($HEXO_SOURCE_DIR.Length + 1)
Copy-Item -Path $_.FullName -Destination $destinationPath -Force
} else {
Write-Host "Ignoring file: $($_.FullName)"
}
}
}

# 检查复制是否成功
if ($?) {
Write-Host "Backup successful: $BACKUP_DIR"
} else {
Write-Host "Backup failed"
}

可以看到,执行文件时会忽略.gitignore 文件,防止把本地忽略文件覆盖。

我们可以在仓库创建.gitignore 文件,用于忽略git push时所提交的文件,例如

1
2
3
4
node_modules/
.deploy_git
public/
.idea/

修改脚本中Hexo博客源码目录 HEXO_SOURCE_DIR和 备份目录 D:\Workspace\shiguang-coding\blog_source

打开 powershell 终端 ,执行 backup.ps1 -o 或者 backup.ps1 -overwrite-o这个参数的含义是如果当前目录已存在则覆盖(只会覆盖目录中已存在的文件,源码目录原来存在,但是博客主目录不存在的文件不受影响)

自动提交

在源码仓库所在目录创建 push.ps1文件,粘贴以下代码

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
# Author: 時光
# Blog: https://shiguang666.eu.org
# GitHub: https://github.com/shiguang

# 获取脚本所在目录
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path

# 设置仓库所在目录
$repoPath = "D:\Workspace\shiguang-coding\blog_source"


# 切换到Git仓库目录
Set-Location -Path $repoPath

# 检查是否有未提交的更改
$status = git status --porcelain

if ($status) {
# 有未提交的更改
Write-Host "Uncommitted changes detected. Proceeding with commit..."

# 添加所有更改到暂存区
git add .
Write-Host "Changes added to staging area."

# 获取当前时间
$currentTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$commitMessage = "Auto commit at $currentTime"

# 提交更改
git commit -m $commitMessage
Write-Host "Changes committed with message: $commitMessage"

# 推送到远程仓库
git push origin main
Write-Host "Changes pushed to remote repository."
} else {
# 没有未提交的更改
Write-Host "No changes to commit."
}

Write-Host "Git operations completed."

# 切换到脚本目录
Set-Location -Path $scriptPath

当然,你也可以指定仓库目录,这样就可以在任意位置执行脚本了

1
2
3
4
5
6
7
8
9
# 获取脚本所在目录
# $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path

# # 设置Git仓库路径为脚本所在目录
# $repoPath = $scriptPath


# 设置仓库所在目录
$repoPath = "D:\Desktop\Test\blog_source"

这段脚本其实就是执行了 git add .git commit -m [commitMessage]git push origin main

如果你的仓库住分支不是main分支,可自行调整

执行该脚本将自动提交代码到远程仓库

数据恢复

  1. 克隆源码仓库到本地
  2. 执行npm install 或者 cnpm install 安装依赖
  3. 执行 hexo clean && hexo g && hexo s 生成网页文件并运行
  4. 浏览器访问http://localhost:4000

小贴士

如果执行过程中报错,可尝试清除npm缓存并删除node_moduls目录,重新执行npm install安装依赖

清除npm命令如下

1
npm cache clean --force

后续

后来觉得自动备份时每次都要输入一个参数很麻烦,干脆直接覆盖了,而且好像没必要忽略.gitignore文件,直接在博客源码目录修改此文件然后覆盖到备份目录就可以了,以下是调整后的脚本,添加了更详细的日志输出

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
# 设置PowerShell的输出编码为UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 设置Hexo博客源码目录
$HEXO_SOURCE_DIR = "D:\hexo\shiguang-coding\blog"

# 设置备份目录
$BACKUP_DIR = "D:\Workspace\shiguang-coding\blog_source"

# 输出开始备份的提示信息
Write-Host "Starting Hexo blog source backup..."

# 创建备份目录
Write-Host "Creating backup directory: $BACKUP_DIR"
New-Item -ItemType Directory -Path $BACKUP_DIR -Force

# 复制Hexo博客源码到备份目录
Write-Host "Copying Hexo blog source to backup directory..."
Get-ChildItem -Path $HEXO_SOURCE_DIR -Recurse | ForEach-Object {
$destinationPath = Join-Path -Path $BACKUP_DIR -ChildPath $_.FullName.Substring($HEXO_SOURCE_DIR.Length + 1)
Write-Host "Copying file: $($_.FullName) to $destinationPath"
Copy-Item -Path $_.FullName -Destination $destinationPath -Force
}

# 检查复制是否成功
if ($?) {
Write-Host "Backup successful: $BACKUP_DIR"
} else {
Write-Host "Backup failed"
}