本文章介绍如何通过脚本自动备份并提交Hexo源码文件,包括 source
,scaffolds
,themes
目录文件,及
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
|
param ( [alias("o")] [switch]$overwrite = $false )
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$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
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
|
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$repoPath = "D:\Workspace\shiguang-coding\blog_source"
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
|
$repoPath = "D:\Desktop\Test\blog_source"
|
这段脚本其实就是执行了 git add .
, git commit -m [commitMessage]
,git push origin main
如果你的仓库住分支不是main
分支,可自行调整
执行该脚本将自动提交代码到远程仓库
数据恢复
- 克隆源码仓库到本地
- 执行
npm install
或者 cnpm install
安装依赖
- 执行
hexo clean && hexo g && hexo s
生成网页文件并运行
- 浏览器访问
http://localhost:4000
小贴士
如果执行过程中报错,可尝试清除npm缓存并删除node_moduls
目录,重新执行npm install
安装依赖
清除npm命令如下
后续
后来觉得自动备份时每次都要输入一个参数很麻烦,干脆直接覆盖了,而且好像没必要忽略.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
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$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
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" }
|
2024-07-11
感觉原来的代码还是有问题,把不需要的目录尤其是node_moduls
目录也便利了,很影响效率,而且有时目录下面会多一层空目录,做了相应调整,忽略一些目录,效率大大提升
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
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$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
Write-Host "Copying Hexo blog source to backup directory..." robocopy $HEXO_SOURCE_DIR $BACKUP_DIR /E /XD node_modules .deploy_git .idea public /XF db.json
if ($LASTEXITCODE -le 8) { Write-Host "Backup successful: $BACKUP_DIR" } else { Write-Host "Backup failed with exit code: $LASTEXITCODE" }
|