0%

创建自动运行脚本用来备份文件

基础

  • 输出到文件
1
echo "$1" > $logFile
  • 输出到文件:尾部插入
1
echo "$1" >> $logFile
  • 输出到文件:某行插入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 格式 ni代表第n行前插入,na代表第n行后插入
sed -i '' 'ni\
something
' $logFile

# 双引号才可以调用变量,格式如下
sed -i '' "1i\\
\\${1}\\
" $logFile

# 多行建议这样使用
file="filePath"
multiLineCode="\
\\
line 1 \\
line 2 \\
line 3 \\
\\"

sed -i '' "1i\\
${multiLineCode}
" $file
  • 删除文件/文件夹
1
2
rm -f someFile
rm -rf someDir
  • 复制文件/文件夹
1
2
cp -f aFile bFile
cp -rf aDir bDir
  • 遍历数组
1
2
3
4
for dir in $SubFolderArray
do
echo $dir
done
  • 倒序遍历数组
1
2
3
4
5
for (( i=${ArrayLength}-1; i>=0; i-- ));
do
dir=${SubFolderArray[$i]}
echo $dir
done
  • 判断字符串是否以xxx开头
1
2
3
if [[ $dir == .svn_* ]]; then
...
fi
  • 判断字符串是否包含子串
1
2
3
if [[ $dir == *svn_* ]]; then
...
fi
  • 判断数字大于xx等
1
2
3
if (( ${TotalBack} > 10 )); then
...
fi

脚本列表

  • 备份脚本
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
# 备份脚本
outputLog() {
logFile="/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackLog/svnBack.log"
if [ ! -f "$logFile" ]; then
touch "$logFile"
echo "$1" >> $logFile
else
sed -i '' "1i\\
\\${1}\\
" $logFile
fi

open -a "Sublime Text" $logFile
}


beginBackFile() {
currentTime="`date +%Y-%m-%d`"
# 删除可能含有的备份
rm -rf "/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackFile/.svn_${currentTime}"
# 备份
cp -rf "/Users/shaoweinan154/wanjia2B/.svn" "/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackFile/.svn_${currentTime}"

if [[ ! $? -eq 0 ]]; then
outputLog " 备份出错 ${currentTime} "
else
outputLog " 备份成功 ${currentTime} "
fi
}


deleteOldBackFile() {
Folder="/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackFile/"
SubFolder=$(ls -a ${Folder})
SubFolderArray=(${SubFolder})
ArrayLength=${#SubFolderArray[@]}

TotalBack=0
for (( i=${ArrayLength}-1; i>=0; i-- ));
do
dir=${SubFolderArray[$i]}
if [[ $dir == .svn_* ]]; then
let TotalBack++
if (( ${TotalBack} > 10 )); then
rm -rf "/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackFile/${dir}"
if [[ ! $? -eq 0 ]]; then
outputLog " 删除出错 ${dir} "
else
outputLog " 删除成功 ${dir} "
fi
fi
fi
done
}

beginBackFile

deleteOldBackFile
  • 恢复脚本
1
2
3
4
5
6
7
8
9
10
11
12
# 恢复脚本

rm -rf "/Users/shaoweinan154/wanjia2B/.svn"
cp -rf "$1" "/Users/shaoweinan154/wanjia2B/.svn"

if [[ ! $? -eq 0 ]]; then
echo " 恢复出错 $1 "
exit 1
else
echo " 恢复成功 $1 "
exit 0
fi

使用Mac进行自动执行备份脚本

  • 编写launchctl加载的plist文件
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
<!--
卸载 launchctl unload cn.zhong.task.plist
启动 launchctl load cn.zhong.task.plist
-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

<!-- 名称,要全局唯一 -->
<key>Label</key>
<string>wanjia2B.runBackSvnFile</string>

<!-- 要运行的程序, 如果省略这个选项,会把ProgramArguments的第一个元素作为要运行的程序 -->
<key>Program</key>
<string>/Users/shaoweinan154/wanjia2B/svnBackBash/backSVNFile.sh</string>

<!-- 命令, 第一个为命令,其它为参数-->
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>/Users/shaoweinan154/wanjia2B/svnBackBash/backSVNFile.sh</string>
</array>

<!-- 运行时间-->
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>12</integer>
<key>Minute</key>
<integer>0</integer>
</dict>

<!-- 运行间隔,与StartCalenderInterval使用其一,单位为秒 -->
<!-- <key>StartInterval</key> -->
<!-- <integer>2</integer> -->

<key>Disabled</key>
<false/>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<false/>

<key>StandardErrorPath</key>
<string>/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackLog/task.log</string>

<key>StandardOutPath</key>
<string>/Users/shaoweinan154/wanjia2B/svnBackBash/SVNBackLog/task.log</string>
</dict>
</plist>
  • 使用launchctl加载plist
1
2
3
4
5
6
7
cd /Users/shaoweinan154/wanjia2B/svnBackBash/
# 加载
launchctl load ***.plist
# 取消
launchctl unload ***.plist
# 查看
launchctl list