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
准备:
- 准备工作:首先用文本编辑工具打开 backupProject.sh 文件,修改项目名称 projPath=PROJECTDIR 和压缩密码 password=Demo 然后打开 restoreProject.sh 文件,修改解压缩密码 password=Demo
- 给两个 sh 脚本可执行权限:
chmod +x backupProject.sh restoreProject.sh
- 两个脚本与项目文件夹 PROJECTDIR 在同级目录
使用:
- 备份:
./backupProject.sh
- 还原:
./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项目的更多相关文章
- Shell脚本,自动化发布tomcat项目【转载】
Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...
- Shell脚本,自动化发布tomcat项目【转】
Shell脚本,自动化发布tomcat项目脚本. 1. vko2c_auto_build_by_scp.sh 文件内容: #---------------------start------------ ...
- 使用shell脚本定时执行备份mysql数据库
使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...
- Centos上通过shell脚本实现数据库备份和还原
最近有个这样的需求,通过shell脚本实现数据库备份还原,最后通过网上查询自己测试实现,将脚本分享给大家 1.数据库备份脚本 #!/bin/bash ds=`` list=`date +%Y`/`da ...
- 用shell脚本实现定时备份数据库
1.备份数据库的方法 可以使用命令查看 ls /usr/local/mysql/bin 这个mysqldump就是系统内置的用来备份数据库的工具. 2.实现方法 ①先随便进入一个位置创建一个目录 ② ...
- shell脚本每天自动备份mysql数据库
一.mysql提供了一个mysqldump的工具可以方便的导出导入数据库信息: 二.使用命令行shell测试执行mysqldump,理解必备的参数,查看生成的sql备份文件是否符合需求: /usr/b ...
- 写一个shell脚本利用wget抓取股票历史数据
今天,大数据部老大交给我一项任务——抓取股票历史数据.于是乎,我自行在网上找了一下,发现wget真真是一个非常强大的linux下载工具.我已经被深深震撼到了.下面叙述今天的一些过程,还是比较坎坷的. ...
- shell脚本实现定时备份某文件
1:目标 实现在图像化界面输入需要备份的源文件路径.目标路径,定时的时间.然后通过输入的信息,把需要备份的源文件打包放到指定的目标路径下以执行定时任务的时间为子目录 把/she ...
- JAVA调用shell脚本利用ansible修改多节点上的redis参数
创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...
随机推荐
- 基于Cpython的 GIL(Global Interpreter Lock)
一 介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- random模块一些常用的东西
import random#一.随机小数# (1)大于0且小于1之间的小数print(random.random())# (2)大于1且小于9之间的小数print(random.uniform(0,9 ...
- 转载:阮一峰 RESTful API设计指南
阮一峰先生的文章对我理解启发很大,转载到我的博客 http://www.ruanyifeng.com/blog/2014/05/restful_api.html 网络应用程序,分为前端和后端两个部分. ...
- 通过js代码来制作数据库增删改查插件
代码流程 1.订制表头:table_config 2.订制显示内容: table_config,data_list 3.加载框: 图片,position:fixed 4.-字符串格式化 ...
- 3.6中的range()
在python3中range()是这样的: >>> range(4) range(0, 4) #额,列表跑哪去了 在之前的python2中是这样的: >>> ran ...
- OSI参考模型与排错
OSI参考模型中底层为其上层服务,因此排除网络故障应该也从底层到高层依次排查. 首先检查网络连接是否正常(物理层检查).例如网卡没有接好网线,将会看到带红叉的本地连接,属于物理层故障. 连接好网卡后, ...
- HBase基本知识介绍及典型案例分析
本次分享的内容主要分为以下五点: HBase基本知识: HBase读写流程: RowKey设计要点: HBase生态介绍: HBase典型案例分析. 首先我们简单介绍一下 HBase 是什么. HBa ...
- php任务管理器 —— Jobby
通过一个主crontab任务去维护别的任务 自定义的计划任务完全由PHP编写 任务的执行计划时间表设置与crontab的时间表设置语法一致 在指定的时间内只会运行一个任务 邮件告警异常退出任务 在ro ...
- Python编程-多态、封装、特性
一.多态与多态性 1.多态 (1)什么是多态 多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承) 序列类型有多种形态:字符串,列表,元组. 动物有多种形态:人,狗,猪 文 ...
- Shell编程之变量进阶
一.变量知识进阶 1.特殊的位置参数变量 实例1:测试$n(n为1...15) [root@codis-178 ~]# cat p.sh echo $1 [root@codis-178 ~]# sh ...