0%

排程備份記錄

要注意系統預設的日期格式,當時在開發的時候是在這個格式底下進行開發的,如果有切換到別的格式日期,那腳本的跟有日期計算的要再重新疏理一遍。

1. MYSQL 資料庫備份

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
@echo off
chcp 65001
@rem =========================================
@rem 設定與備份
@rem =========================================
set CUR_DAY=%date:~11,2%
set START_DATETIME="%date% %time%"

set DB_NAME=exam2018_api
set DATA_DEST_ROOT_PATH=D:/TQE_backup/TQE_SERVER/DB_BAK/
set DATA_DEST_FOLDER_PATH=%DATA_DEST_ROOT_PATH%%CUR_DAY%/
set DATA_DEST_FILE_NAME=%DB_NAME%_db__D%date:~11,2%

cd /d %DATA_DEST_ROOT_PATH%

if not exist "%DATA_DEST_FOLDER_PATH%/" mkdir %CUR_DAY%

"C:\xampp\mysql\bin\mysqldump" --add-drop-database --add-drop-table --allow-keywords -QqceKf --routines --create-options --flush-privileges --insert-ignore --databases -r "%DATA_DEST_FOLDER_PATH%%DATA_DEST_FILE_NAME%.sql" -uroot -prootNaerTqa %DB_NAME%
"C:\Program Files\7-Zip\7z" a -t7z -mx9 "%DATA_DEST_FOLDER_PATH%%DATA_DEST_FILE_NAME%.7z" -p"ej;2u,654yji4j06t/6vu;" -mhe "%DATA_DEST_FOLDER_PATH%%DATA_DEST_FILE_NAME%.sql"
if exist "%DATA_DEST_FOLDER_PATH%%DATA_DEST_FILE_NAME%.7z" cd /d %DATA_DEST_FOLDER_PATH% && del %DATA_DEST_FILE_NAME%.sql

set END_DATETIME="%date% %time%"


@rem =========================================
@rem 寫LOG
@rem =========================================
(
echo 資料庫名稱 %DB_NAME%
echo 檔案備份_來源路徑 mysqldump database output
echo 檔案備份_目的路徑 %DATA_DEST_FOLDER_PATH%
echo 檔案備份_開始時間 %START_DATETIME%
echo 檔案備份_結束時間 %END_DATETIME%
echo 工作排程檔名 %0
) >%DATA_DEST_FILE_NAME%.log




@rem =========================================
@rem 擷取前 N 天日期
@rem =========================================
@echo wscript.echo dateadd("d",-5,date) > %tmp%\tmp.vbs
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
@if %m% LSS 9 set m=0%m%
@if %d% LSS 9 set d=0%d%
@set yesterday=%y%%m%%d%


@rem =========================================
@rem 釋放硬碟空間,進行刪除
@rem LOCAL SERVER 保留五天資料
@rem NAS 保留31天資料
@rem =========================================
rmdir /s/q "%DATA_DEST_ROOT_PATH%%d%"

2. WEB 程式備份

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

@rem =========================================
@rem 設定與備份
@rem =========================================

set CUR_DAY=%date:~11,2%
set START_DATETIME="%date% %time%"

set DATA_SOURCE_PATH=C:/xampp/htdocs/exam2018*
set DATA_DEST_ROOT_PATH=D:/TQE_backup/TQE_SERVER/WEB_BAK/
set DATA_DEST_FOLDER_PATH=%DATA_DEST_ROOT_PATH%%CUR_DAY%/
set DATA_DEST_FILE_NAME=exam_web__D%date:~11,2%

cd /d %DATA_DEST_ROOT_PATH%

if not exist "%DATA_DEST_FOLDER_PATH%/" mkdir %CUR_DAY%

cd /d %DATA_DEST_FOLDER_PATH% && "C:\Program Files\7-Zip\7z" a -t7z -mx9 %DATA_DEST_FILE_NAME%.7z -p"ej;2u,654yji4j06t/6vu;" -mhe "%DATA_SOURCE_PATH%"


set END_DATETIME="%date% %time%"

@rem =========================================
@rem 寫LOG
@rem =========================================
(
echo 應用程式名稱 exam2018*
echo 檔案備份_來源路徑 %DATA_SOURCE_PATH%
echo 檔案備份_目的路徑 %DATA_DEST_ROOT_PATH%
echo 檔案備份_開始時間 %START_DATETIME%
echo 檔案備份_結束時間 %END_DATETIME%
echo 工作排程檔名 %0
) >%DATA_DEST_FILE_NAME%.log


@rem =========================================
@rem 擷取前 N 天日期
@rem =========================================
@echo wscript.echo dateadd("d",-5,date) > %tmp%\tmp.vbs
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
@if %m% LSS 9 set m=0%m%
@if %d% LSS 9 set d=0%d%
@set yesterday=%y%%m%%d%


@rem =========================================
@rem 釋放硬碟空間,進行刪除
@rem LOCAL SERVER 保留五天資料
@rem NAS 保留31天資料
@rem =========================================
rmdir /s/q "%DATA_DEST_ROOT_PATH%%d%"


3. 檔案備份

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
@echo off
chcp 65001
@rem =========================================
@rem 設定與備份
@rem =========================================

set CUR_DAY=%date:~11,2%
set START_DATETIME="%date% %time%"

set DATA_SOURCE_PATH=D:/掃描文件/
set DATA_DEST_ROOT_PATH=D:/TQE_backup/OES_DATA/掃描文件/
set DATA_DEST_FOLDER_PATH=%DATA_DEST_ROOT_PATH%%CUR_DAY%/
set DATA_DEST_FILE_NAME=掃描文件_data__D%CUR_DAY%


cd /d %DATA_DEST_ROOT_PATH%

if not exist "%DATA_DEST_FOLDER_PATH%/" mkdir %CUR_DAY%

"C:\Program Files\7-Zip\7z" a -t7z -mx=0 -v1024m "%DATA_DEST_FOLDER_PATH%%DATA_DEST_FILE_NAME%.7z" -p"ej;2u,654yji4j06t/6vu;" -mhe "%DATA_SOURCE_PATH%"

set END_DATETIME="%date% %time%"

@rem =========================================
@rem 寫LOG
@rem =========================================
(
echo 檔案備份_來源路徑 %DATA_SOURCE_PATH%
echo 檔案備份_目的路徑 %DATA_DEST_ROOT_PATH%
echo 檔案備份_開始時間 %START_DATETIME%
echo 檔案備份_結束時間 %END_DATETIME%
echo 工作排程檔名 %0
) >%DATA_DEST_FOLDER_PATH%%DATA_DEST_FILE_NAME%.log


@rem =========================================
@rem 擷取前 N 天日期
@rem =========================================
@echo wscript.echo dateadd("d",-5,date) > %tmp%\tmp.vbs
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j
@for /f "tokens=1,2,3 delims=/- " %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
@if %m% LSS 9 set m=0%m%
@if %d% LSS 9 set d=0%d%
@set yesterday=%y%%m%%d%


@rem =========================================
@rem 釋放硬碟空間,進行刪除
@rem LOCAL SERVER 保留五天資料
@rem NAS 保留31天資料
@rem =========================================
rmdir /s/q "%DATA_DEST_ROOT_PATH%%d%"




4. 參考連結

初學者的命令行