注:无git的方法参见:tar 或 7z 备份项目
首先利用homebrew安装p7zip
$ brew install p7zip
然后利用两个shell脚本:
- backupProject.sh 会在指定的项目文件夹中找到所有.git目录,对其git gc压缩,然后用7za创建两个7z文件:
- PROJECTDIR.2016-03-15-12-00-00.Src.7z
- PROJECTDIR.2016-03-15-12-00-00.gitpack.7z
- restoreProject.sh 会将上述两个文件用7za解压到目录:
- PROJECTDIR.2016-03-15-12-00-00.output
准备:
- 准备工作:首先用文本编辑工具打开 backupProject.sh 文件,修改项目名称 projPath=PROJECTDIR 和压缩密码 password=Demo 然后打开 restoreProject.sh 文件,修改解压缩密码 password=Demo
- 给两个 sh 脚本可执行权限:
chmod +x backupProject.sh restoreProject.sh
- 两个脚本与项目文件夹 PROJECTDIR 在同级目录
使用:
- 备份:
./backupProject.sh
- 还原:
./restoreProject PROJECTDIR.2016-03-15-12-00-00.Src.7z
例子:
$ ls PROJECTDIR backupProject.sh restoreProject.sh // 备份 $ ./backupProject.sh Ready... git gc: PROJECTDIR/.git Nothing new to pack. git gc: PROJECTDIR/frameworks/.git ... git gc: PROJECTDIR/library/.git ... git gc: PROJECTDIR/Universal/.git ... Backuping(1/2): PROJECTDIR.2016-03-15-12-00-00.Src.7z ... Backuping(2/2): PROJECTDIR.2016-03-15-12-00-00.gitpack.7z ... Done. $ ls PROJECTDIR PROJECTDIR.2016-03-15-12-00-00.Src.7z PROJECTDIR.2016-03-15-12-00-00.gitpack.7z backupProject.sh restoreProject.sh // 恢复 $ ./restoreProject.sh PROJECTDIR.2016-03-15-12-00-00.Src.7z Ready... Extracting PROJECTDIR.2016-03-15-12-00-00.Src.7z... ... Extracting PROJECTDIR.2016-03-15-12-00-00.gitpack.7z... ... Output Dir: PROJECTDIR.2016-03-15-12-00-00.output Done. $ ls PROJECTDIR PROJECTDIR.2016-03-15-12-00-00.Src.7z PROJECTDIR.2016-03-15-12-00-00.output PROJECTDIR.2016-03-15-12-00-00.gitpack.7z backupProject.sh restoreProject.sh $ ls PROJECTDIR.2016-03-15-12-00-00.output/ PROJECTDIR $
脚本源码如下:
backupProject.sh
1 #!/bin/sh 2 projPath=PROJECTDIR 3 password=Demo 4 ## level=0,1,3,5,7,9 Level 0 is no compression, 5 is normal, 9 is Ultra. 5 level=5 6 ## gitgc=0,1 0 is not gc, 1 is git gc 7 gitgc=1 8 now=`date +%Y-%m-%d-%H-%M-%S` 9 output=$projPath.$now.Src.7z 10 output2=$projPath.$now.gitpack.7z 11 12 function printMsg() { 13 echo "