-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_backup_xoops.sh
executable file
·180 lines (157 loc) · 4.95 KB
/
setup_backup_xoops.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
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
#!/bin/bash
###########################
# 用途:
# 產生備份 xoops 之腳本
# 設定備份排程 for root
###########################
#######################
# 變數區
#######################
# 樣板檔
BACKUP_TPL_FILE="backup_xoops.tpl"
RESTORE_TPL_FILE="restore_xoops.tpl"
# 產出之腳本檔
BACKUP_SH_FILE="backup_xoops.sh"
RESTORE_SH_FILE="restore_xoops.sh"
# root 之 crontab
ROOT_CRONTAB="/var/spool/cron/crontabs/root"
# 複製檔與暫存檔
ROOT_CRONTAB_COPY="/tmp/root_crontab_copy"
ROOT_CRONTAB_TMP="/tmp/root_crontab_tmp"
# 父層目錄路徑
PARENT_FOLDER=$(dirname ${PWD})
#######################
# 函數區
#######################
# 參考:http://linux-wiki.cn/wiki/zh-tw/%E7%94%A8shell%E5%AE%9E%E7%8E%B0bat%E7%9A%84pause
# 修改: if [...] => if [[...]]
function pause(){
read -n 1 -p "$*" INP
if [[ $INP != '' ]] ; then
echo -ne '\b \n'
fi
}
# 產生備份腳本檔
generateBackupScript() {
printf "\n\n"
printf "***** 產生 備份 與 還原 腳本檔 *****"
printf "\n\n"
pause ">>> 按任意鍵繼續 或 Ctrl + C 中斷...."
printf "\n\n"
read -p "備份檔保留幾份? [ 輸入數字,預設 7 ] " keep_count
if [[ "$keep_count" == "" ]]; then
keep_count=7
fi
cp $BACKUP_TPL_FILE $PARENT_FOLDER/$BACKUP_SH_FILE && chmod +x $PARENT_FOLDER/$BACKUP_SH_FILE
cp $RESTORE_TPL_FILE $PARENT_FOLDER/$RESTORE_SH_FILE && chmod +x $PARENT_FOLDER/$RESTORE_SH_FILE
# 參考 https://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed
# 因為變數值為路徑含有 /,所以將 sed 之分隔符號換成其他符號
sed -i "s:_INSTALL_PATH:$PWD:g" $PARENT_FOLDER/$BACKUP_SH_FILE
sed -i "s:_INSTALL_PATH:$PWD:g" $PARENT_FOLDER/$RESTORE_SH_FILE
sed -i "s:_MY_USERNAME:$USER:g" $PARENT_FOLDER/$BACKUP_SH_FILE
sed -i "s:_KEEP_COUNT:$keep_count:g" $PARENT_FOLDER/$BACKUP_SH_FILE
printf "\n"
printf "備份腳本檔已產生:"
printf "\n"
printf " $PARENT_FOLDER/${BACKUP_SH_FILE}"
printf "\n\n"
printf "還原腳本檔已產生:"
printf "\n"
printf " $PARENT_FOLDER/${RESTORE_SH_FILE}"
printf "\n\n"
printf "備份檔存放目錄為:"
printf "\n"
printf " $PARENT_FOLDER/xoops_backup/"
printf "\n\n"
printf "備份檔保留 ${keep_count} 份"
printf "\n\n"
pause ">>> 按任意鍵繼續...."
}
# 設定備份排程 for root
setupCrontabForRoot(){
printf "\n\n"
printf "***** 設定備份排程 *****"
printf "\n\n"
printf "會先清除原有之備份排程(有的話)"
printf "\n\n"
printf "將以 root 執行備份,每日備份一次"
printf "\n\n"
pause ">>> 按任意鍵繼續 或 Ctrl + C 中斷...."
printf "\n\n"
read -p "每天幾點執行備份? [ 0 - 23,預設 1 => 凌晨 1 點] " ans
if [[ "$ans" == "" ]]; then
ans=1
fi
# 複製原有排程
sudo crontab -l > $ROOT_CRONTAB_COPY
# 移除原有的備份排程
grep -v -E "$PARENT_FOLDER/${BACKUP_SH_FILE}|xoops備份排程" $ROOT_CRONTAB_COPY > $ROOT_CRONTAB_TMP
# 加入新的備份排程
echo "# xoops備份排程,每天 ${ans} 點備份" >> $ROOT_CRONTAB_TMP
echo "0 ${ans} * * * $PARENT_FOLDER/${BACKUP_SH_FILE}" >> $ROOT_CRONTAB_TMP
# 由暫存檔倒入排程
sudo crontab -u root $ROOT_CRONTAB_TMP
# 刪除複製檔與暫存檔
rm -rf $ROOT_CRONTAB_TMP $ROOT_CRONTAB_COPY
printf "\n\n"
printf "備份排程設定完成,每天 ${ans} 點備份一次"
printf "\n\n"
pause ">>> 按任意鍵繼續...."
}
# 查看 root 的排程
listCrontabForRoot() {
printf "\n\n"
printf "***** root 的排程 *****"
printf "\n\n"
sudo crontab -u root -l
printf "\n\n"
pause ">>> 按任意鍵繼續...."
}
# 顯示選單
showMenu() {
printf "\n\n"
printf "#########################################"
printf "\n#"
printf "\n# 產生備份、還原腳本檔與設定備份排程"
printf "\n#"
printf "\n#########################################"
printf "\n\n"
printf "1 產生備份、還原腳本檔"
printf "\n"
printf "2 設定備份排程"
printf "\n"
printf "3 查看 root 的排程"
printf "\n"
printf "q 離開"
printf "\n\n"
read -p "請輸入 [ 預設: q 離開 ] " choice
if [[ "$choice" == "" ]] || [[ "$choice" == "q" ]] || [[ "$choice" == "Q" ]]; then
printf "\n"
exit 0
fi
# 1 產生備份腳本檔
if [[ "$choice" == "1" ]]; then
generateBackupScript
clear
showMenu
fi
# 2 設定備份排程
if [[ "$choice" == "2" ]]; then
setupCrontabForRoot
clear
showMenu
fi
# 3 查看 root 的排程
if [[ "$choice" == "3" ]]; then
listCrontabForRoot
clear
showMenu
fi
# 其他
clear
showMenu
}
#######################
# 執行區
#######################
showMenu