注:无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.-----.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(/): PROJECTDIR.-----.Src.7z
...
Backuping(/): PROJECTDIR.-----.gitpack.7z
...
Done. $ ls
PROJECTDIR
PROJECTDIR.-----.Src.7z
PROJECTDIR.-----.gitpack.7z
backupProject.sh
restoreProject.sh // 恢复
$ ./restoreProject.sh PROJECTDIR.-----.Src.7z
Ready...
Extracting PROJECTDIR.-----.Src.7z...
...
Extracting PROJECTDIR.-----.gitpack.7z...
...
Output Dir: PROJECTDIR.-----.output
Done. $ ls
PROJECTDIR
PROJECTDIR.-----.Src.7z
PROJECTDIR.-----.output
PROJECTDIR.-----.gitpack.7z
backupProject.sh
restoreProject.sh
$ ls PROJECTDIR.-----.output/
PROJECTDIR
$

脚本源码如下:

backupProject.sh

 #!/bin/sh
projPath=PROJECTDIR
password=Demo
## level=,,,,, Level is no compression, is normal, is Ultra.
level=
## gitgc=, is not gc, is git gc
gitgc=
now=`date +%Y-%m-%d-%H-%M-%S`
output=$projPath.$now.Src.7z
output2=$projPath.$now.gitpack.7z function printMsg() {
echo "\033[1;34m$1\033[0m"
} function printMsgNoColor() {
echo "\033[1;m$1\033[0m"
} ## start
printMsg "Ready..."
for gitdir in `find $projPath -iname ".git"`; do
if [[ $gitgc -eq ]]; then
printMsg "git gc: $gitdir"
git -C $gitdir/.. gc
else
printMsgNoColor "$gitdir"
fi
pack="$gitdir/objects/pack/"
xrdirs="$xrdirs -xr!$pack"
packs="$packs $pack"
done
printMsg "Backuping(1/2): $output"
7za a -t7z -mx=$level $output "$projPath/" -scsUTF- -p$password $xrdirs
printMsg "Backuping(2/2): $output2"
7za a -t7z -mx= $output2 $packs -scsUTF- -p$password
printMsg "Done."

restoreProject.sh

 #!/bin/sh
password=Demo function printError() {
echo "FAIL!"
echo "SAMPLE1: $0 xxxxxxxx.Src.7z"
echo "SAMPLE2: $0 xxxxxxxx.gitpack.7z"
} function printMsg() {
echo "\033[1;34m$1\033[0m"
} ## start
printMsg "Ready..."
if [[ -a $ ]]; then
if [[ $ == *.Src.7z ]]; then
file1=$
file2=`echo $|sed -n "s/\.Src\.7z/\.gitpack\.7z/p"`
elif [[ $ == *.gitpack.7z ]]; then
file1=`echo $|sed -n "s/\.gitpack\.7z/\.Src\.7z/p"`
file2=$
else
printError
exit
fi output=`echo $file1|sed -n "s/\.Src\.7z/.output/p"`
if [[ -a $file1 ]]; then
printMsg "Extracting $file1..."
7za x -aoa -y $file1 -o$output -p$password
fi
if [[ -a $file2 ]]; then
printMsg "Extracting $file2..."
7za x -aoa -y $file2 -o$output -p$password
fi
printMsg "Output Dir: $output"
printMsg "Done."
else
printError
fi

下载:

https://github.com/m2nlight/backupProject

shell脚本:利用7z备份git项目的更多相关文章

  1. Shell脚本,自动化发布tomcat项目【转载】

    Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...

  2. Shell脚本,自动化发布tomcat项目【转】

    Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...

  3. 使用shell脚本定时执行备份mysql数据库

    使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...

  4. Centos上通过shell脚本实现数据库备份和还原

    最近有个这样的需求,通过shell脚本实现数据库备份还原,最后通过网上查询自己测试实现,将脚本分享给大家 1.数据库备份脚本 #!/bin/bash ds=`` list=`date +%Y`/`da ...

  5. 用shell脚本实现定时备份数据库

    1.备份数据库的方法 可以使用命令查看 ls  /usr/local/mysql/bin 这个mysqldump就是系统内置的用来备份数据库的工具. 2.实现方法 ①先随便进入一个位置创建一个目录 ② ...

  6. shell脚本每天自动备份mysql数据库

    一.mysql提供了一个mysqldump的工具可以方便的导出导入数据库信息: 二.使用命令行shell测试执行mysqldump,理解必备的参数,查看生成的sql备份文件是否符合需求: /usr/b ...

  7. 写一个shell脚本利用wget抓取股票历史数据

    今天,大数据部老大交给我一项任务——抓取股票历史数据.于是乎,我自行在网上找了一下,发现wget真真是一个非常强大的linux下载工具.我已经被深深震撼到了.下面叙述今天的一些过程,还是比较坎坷的. ...

  8. shell脚本实现定时备份某文件

    1:目标       实现在图像化界面输入需要备份的源文件路径.目标路径,定时的时间.然后通过输入的信息,把需要备份的源文件打包放到指定的目标路径下以执行定时任务的时间为子目录       把/she ...

  9. JAVA调用shell脚本利用ansible修改多节点上的redis参数

    创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...

随机推荐

  1. python基础里的那些为什么?

    一.执行python脚本的两种方式? 直接在解释器里编写并在解释器里执行 文件编写,并在终端通过 python 路径  这种方式执行 好,我们就以输出hello world这个例子来比较两种方式的不同 ...

  2. 我的Android进阶之旅------>Android项目运行报java.lang.NoClassDefFoundError错误的解决办法

    今天在运行一个Android项目的时候,报了以下错误: D/AndroidRuntime( 3859): Shutting down VM E/AndroidRuntime( 3859): FATAL ...

  3. vuejs组件通信

    <body> <div id="example"> <father></father> </div> </body ...

  4. 标准c时间与日期函数

    标准c时间与日期函数 asctime 语法:     #include <time.h>   char *asctime( const struct tm *ptr ); 功能: 函数将p ...

  5. 标准c内存函数的使用方法

    标准c内存函数 calloc 语法:     #include <stdlib.h>   void *calloc( size_t num, size_t size ); 功能: 函数返回 ...

  6. oracle 函数 截取 连接 替换 判断

    一个处理不规范日期的函数,廖记一下吧,以免再忘. --注意全角半角 CREATE OR REPLACE function f_str2form( date_string in varchar2 ) r ...

  7. c# 执行批处理文件

    ProcessStartInfo proc = new ProcessStartInfo(); proc.UseShellExecute = false; proc.CreateNoWindow = ...

  8. python的PIL模块安装

    一.Centos安装PIL #尤其重要,否则会报错 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel ...

  9. python之路 正则表达式,模块导入的方法,hashlib加密

    一.正则表达式re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的 ...

  10. ssh登陆virtualbox虚拟机