随笔 - 272  文章 - 0  评论 - 283  阅读 - 142万

git批量备份

我用git的目的主要是为了数据的完整性,信息不丢失,虽然repository的代码服务器和本地都会存一份,但有时候自己的小片段代码很多(比如github的gist),不可能每天都用得到,需要定期的备份,以备不时之需(比如网络断开、服务器当机等因素)。

一、备份原理

通过某种方法获取需要备份repository的名称(比如静态配置等),如果在指定目录里面该repository存在(即文件夹存在), 进入文件夹,执行git pull操作; 如果不存在,执行git clone操作。

二、自建git服务器备份

git服务器ip:192.168.1.100
git账户:git
Repositories:/home/git/test1.git,/home/git/test2.git
正常访问:

git clone git@192.168.1.100:test1.git  
git clone git@192.168.1.100:test2.git  

1、配置config文件
config配置如下:

Host host100 
        Hostname 192.168.1.100
        User git
        IdentityFile C:/Users/admin/.ssh/id_rsa_gitBackup

这个不懂的可以参考这里:http://www.cnblogs.com/MikeZhang/archive/2012/11/27/gitWithSshKey_20121127.html

2、批量获取
python代码:

复制代码
import os
hostName = "host100"
dirs = [
    'test1',
    'test2'
]

for dirName in dirs:
    print dirName," : ",
    if os.path.exists(dirName): #如果存在执行pull操作
        strCmd = "cd %s && git pull && cd .." % dirName
        os.system(strCmd)
    else: #不存在则执行clone操作
        strCmd = "git clone %s:%s.git " % (hostName,dirName)
        os.system(strCmd)
复制代码

三、github备份

1、配置config文件

复制代码
Host github
        Hostname github.com
        User git
        IdentityFile C:/Users/admin/.ssh/id_rsa_github

Host gist
        Hostname gist.github.com
        User git
        IdentityFile C:/Users/admin/.ssh/id_rsa_github_gist
复制代码

2、备份Repositories(以我的github为例)

(1)命令行访问方式:

git clone github:mike-zhang/cppCallLua.git

(2)python脚本批量操作:

复制代码
#for github 
import os

hostName = "github"
userName = "mike-zhang"
repoNames = [
    'cppCallLua',
    'testCodes'
]

for repo in repoNames:
    print repo," : ",
    if os.path.exists(repo):        
        strCmd = "cd %s && git pull && cd .." % repo
        os.system(strCmd)
    else:       
        strCmd = "git clone %s:%s/%s.git " % (hostName,userName,repo)
        os.system(strCmd)
复制代码

(3)运行效果 

3、备份gist(以我的gist为例)
(1)命令行操作
git clone git@gist.github.com:4166192.git gist-4166192
(2)python脚本批量操作
这里以两个gist例子:

复制代码
View Code
#for gist 
import os

hostName = "gist"
userName = "mike-zhang"
repoNames = [
    '4166192',
    '4084385'
]

for gist in repoNames:
    print gist," : ",
    if os.path.exists(gist):        
        strCmd = "cd %s && git pull && cd .." % gist        
        os.system(strCmd)
    else:        
        strCmd = "git clone %s:%s.git " % (hostName,gist)
        os.system(strCmd)
复制代码

(3)运行效果 

我这里考虑的比较简单,更强大的功能还需读者自行扩展。
源码地址(gist):https://gist.github.com/4166192

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2012/20121129_git批量备份.md

欢迎补充

posted on   Mike_Zhang  阅读(4273)  评论(2编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2012年11月 >
28 29 30 31 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 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示