shell脚本:利用7z备份git项目

注:无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

准备:

  1. 准备工作:首先用文本编辑工具打开 backupProject.sh 文件,修改项目名称 projPath=PROJECTDIR 和压缩密码 password=Demo 然后打开 restoreProject.sh 文件,修改解压缩密码 password=Demo
  2. 给两个 sh 脚本可执行权限:
    chmod +x backupProject.sh restoreProject.sh
  3. 两个脚本与项目文件夹 PROJECTDIR 在同级目录

使用:

  1. 备份:
    ./backupProject.sh
  2. 还原:
    ./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 "33[1;34m$133[0m"
14 }
15 
16 function printMsgNoColor() {
17     echo "33[1;m$133[0m"
18 }
19 
20 ## start
21 printMsg "Ready..."
22 for gitdir in `find $projPath -iname ".git"`; do
23     if [[ $gitgc -eq 1 ]]; then
24         printMsg "git gc: $gitdir"
25         git -C $gitdir/.. gc
26     else
27         printMsgNoColor "$gitdir"
28     fi
29     pack="$gitdir/objects/pack/"
30     xrdirs="$xrdirs -xr!$pack"
31     packs="$packs $pack"
32 done
33 printMsg "Backuping(1/2): $output"
34 7za a -t7z -mx=$level $output "$projPath/" -scsUTF-8 -p$password $xrdirs
35 printMsg "Backuping(2/2): $output2"
36 7za a -t7z -mx=0 $output2 $packs -scsUTF-8 -p$password
37 printMsg "Done."

restoreProject.sh

 1 #!/bin/sh
 2 password=Demo
 3 
 4 function printError() {
 5     echo "FAIL!"
 6     echo "SAMPLE1: $0 xxxxxxxx.Src.7z"
 7     echo "SAMPLE2: $0 xxxxxxxx.gitpack.7z"
 8 }
 9 
10 function printMsg() {
11     echo "33[1;34m$133[0m"
12 }
13 
14 ## start
15 printMsg "Ready..."
16 if [[ -a $1 ]]; then
17     if [[ $1 == *.Src.7z ]]; then
18         file1=$1
19         file2=`echo $1|sed -n "s/.Src.7z/.gitpack.7z/p"`
20     elif [[ $1 == *.gitpack.7z ]]; then
21         file1=`echo $1|sed -n "s/.gitpack.7z/.Src.7z/p"`
22         file2=$1
23     else
24         printError
25         exit 0
26     fi
27 
28     output=`echo $file1|sed -n "s/.Src.7z/.output/p"`
29     if [[ -a $file1 ]]; then
30         printMsg "Extracting $file1..."
31         7za x -aoa -y $file1 -o$output -p$password
32     fi
33     if [[ -a $file2 ]]; then
34         printMsg "Extracting $file2..."
35         7za x -aoa -y $file2 -o$output -p$password
36     fi
37     printMsg "Output Dir: $output"
38     printMsg "Done."
39 else
40     printError
41 fi

下载:

https://github.com/m2nlight/backupProject

原文地址:https://www.cnblogs.com/Bob-wei/p/5279272.html