Bat脚本编写用例

一次Bat脚本的尝试

记录一次Bat脚本的编写使用,

本意是在Windows服务器上用Git实现文件的备份和实时拉取, 但由于仓库的大小限制失败, 改为其他方式实现, 以下为具体bat脚本代码

1_rar_gitpush.bat

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
@echo on

rem 设置运行脚本变量
:::: 压缩执行文件路径
set rar_exe="C:\Program Files\WinRAR\Rar.exe"
:::: 文件压缩拆分大小
set file_size=49m
:::: SQL自动备份文件夹路径,要带\
set sql_back_file_path=F:\BashTest\sqlfile\
::::上传git仓库文件夹路径,要带\
set git_file_path=F:\BashTest\filename3\
::::git拉取同步分支
set git_branck=master

:: 创建日志文件
set dir_path=%~dp0
set log_path=%dir_path%push_log.txt
set now_time=%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%

echo %log_path%
if not exist %log_path% (
echo "Backup Log">%log_path%
)



rem 拉取最新代码
cd %git_file_path%
if %errorlevel% neq 0 goto fail
git reset --hard
if %errorlevel% neq 0 goto fail
git fetch --all
if %errorlevel% neq 0 goto fail
git pull origin %git_branck%
::if %errorlevel% neq 0 goto fail


:::: 删除obj文件
rem rd /s /q .git\objects
rem if %errorlevel% neq 0 goto fail
rem md .git\objects
rem if %errorlevel% neq 0 goto fail
rem rd /s /q .git/refs/original/
rem if %errorlevel% neq 0 goto fail
rem git reflog expire --expire=now --all
rem git gc --prune=now

rem 删除原有数据
del filefolder\*.rar
if %errorlevel% neq 0 goto fail
git add .
::if %errorlevel% neq 0 goto fail
git commit -m "daily backup clean"
::if %errorlevel% neq 0 goto fail
git push origin %git_branck%:%git_branck%
::if %errorlevel% neq 0 goto fail



rem rem 清除大文件
::git filter-branch --index-filter "git rm --cached --ignore-unmatch *" -- --all
git filter-branch --force --index-filter "git rm -r --cached --ignore-unmatch filefolder" --prune-empty --tag-name-filter cat -- --all
rd /s /q .git\refs\original\
if %errorlevel% neq 0 goto fail
rd /s /q .git\logs\
if %errorlevel% neq 0 goto fail
git reflog expire --expire=now --all
if %errorlevel% neq 0 goto fail
git gc --prune=now --aggressive
if %errorlevel% neq 0 goto fail
git add .
git commit -m "data clean"
git push origin --force --all
::if %errorlevel% neq 0 goto fail
rem rem 清空分支
rem git checkout %git_branck%
rem if %errorlevel% neq 0 goto fail
rem git checkout --orphan master-new-line;
rem if %errorlevel% neq 0 goto fail
rem git add -A
rem if %errorlevel% neq 0 goto fail
rem git commit -am "init project"
rem if %errorlevel% neq 0 goto fail
rem git branch -D %git_branck%
rem if %errorlevel% neq 0 goto fail
rem git branch -m %git_branck%
rem if %errorlevel% neq 0 goto fail
rem git push -f origin %git_branck%:%git_branck%
rem if %errorlevel% neq 0 goto fail


rem git pack-refs --all --prune
rem git reflog expire --all
rem git repack -d -l
rem git prune --expire
rem git rerere gc

rem git checkout -b %git_branck%
rem git fetch --all

rem 分卷压缩指定文件夹内文件
:started
set now_day_format=%date:~0,4%_%date:~5,2%_%date:~8,2%
%rar_exe% a -m5 -v%file_size% -ta%now_day_format% -agYYYYMMDD %git_file_path%filefolder\ %sql_back_file_path%
if %errorlevel% neq 0 goto fail

rem 提交gitee
:gitpush
cd %git_file_path%
if %errorlevel% neq 0 goto fail
git add .
if %errorlevel% neq 0 goto fail
git commit -m "daily backup"
if %errorlevel% neq 0 goto fail
git push origin %git_branck%:%git_branck%
if %errorlevel% neq 0 goto fail

rem rem 上传完毕,清空仓库
rem :dellocal
rem del filefolder\*.rar
goto success

:fail
echo %now_time%%now_day_format%" daily backup failed">>%log_path%
echo "daily backup failed,please retry later!" && goto exited

:success
echo %now_time%%now_day_format%" daily backup success">>%log_path%
echo "backup success" && goto exited

:exited
pause
exit

2_gitpull_move.bat

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
@echo off

rem 设置运行脚本变量

::::下载git仓库文件夹路径,要带\
set git_file_path=F:\BashTest\filename3\
::::本地存储文件夹路径,要带\
set storage_file_path=F:\BashTest\filename2\
::::git拉取同步分支
set git_branck=file

:: 创建日志文件
set dir_path=%~dp0
set log_path=%dir_path%pull_log.txt
set now_time=%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%

echo %log_path%
if not exist %log_path% (
echo "Backup Pull Log">%log_path%
)

rem 拉取最新代码
cd %git_file_path%
if %errorlevel% neq 0 goto fail

git reset --hard
if %errorlevel% neq 0 goto fail

git fetch --all
if %errorlevel% neq 0 goto fail

git pull origin %git_branck%
if %errorlevel% neq 0 goto fail

rem 移动到备份
:movefile
move %git_file_path%\test\* %storage_file_path%
if %errorlevel% neq 0 goto fail
goto success

:fail
echo %now_time%" daily backup pull failed">>%log_path%
echo "daily backup failed,please retry later!" && goto exited

:success
echo %now_time%" daily backup pull success">>%log_path%
echo "backup success" && goto exited

:exited
pause
exit