-
Notifications
You must be signed in to change notification settings - Fork 2
/
gib.py
executable file
·278 lines (256 loc) · 10.3 KB
/
gib.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python
# gib.py
# A backup script that uses git
# Mark Tully
# 2/9/12
#===============================================================================
# Copyright (C) 2012 by Mark Tully
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#===============================================================================
import pbs
import sys,os,datetime
# set this to specify the dir dir should be somewhere other than the current directory
gitdir='.git'
def git(*args):
return pbs.git('--git-dir=%s'%gitdir,*args)
def git_pipe(input,*args):
return pbs.git(input,'--git-dir=%s'%gitdir,*args)
def clearIndex():
index=git('ls-files')
if index.strip()!='':
git('rm','--cached','-rf','.')
def getEmptyDirs(path):
""" generator func to list all the empty dirs in a dir """
for (dirpath,dirnames,filenames) in os.walk(path):
if len(filenames)==0 and len(dirnames)==0:
yield dirpath
def makeTreeFromDir(backupname,paths):
""" imports a path into the git repro and makes a tree from it. returns the tree sha """
clearIndex()
topLevel=[]
filesInTree=[]
dirsInTree=[]
emptyFileHash=None
for path in paths:
if not os.path.exists(path):
fatal("Path '%s' does not exist, cannot backup"%path)
bn=os.path.basename(path)
if bn in topLevel:
fatal("Multiple paths ending in '%s' are being backed up, not supported"%bn)
if os.path.isdir(path):
# add current dir to the index (also import all objects)
git('--work-tree',path,'add','-f','.')
# empty dirs are not added by the above, pretend there is a .gibkeep file in each
# (we will delete this when extracting backups)
for emptyDir in getEmptyDirs(path):
if not emptyFileHash:
emptyFileHash=git_pipe(pbs.echo('-n',''),'hash-object','-w','--stdin').strip()
git('update-index','--add','--cacheinfo','10644',emptyFileHash,os.path.join(os.path.relpath(emptyDir,path),'.gibkeep'))
# write the tree for this and get the tree sha
tree=git('write-tree').strip()
clearIndex()
dirsInTree.append((bn,tree))
else:
fileHash=git('hash-object','-w',path).strip()
filesInTree.append((bn,fileHash))
clearIndex()
# now make the final snapshot index
for (dir,tree) in dirsInTree:
git('read-tree','-i',tree,'--prefix=%s/'%dir)
for (file,hash) in filesInTree:
# see http://git-scm.com/book/en/Git-Internals-Git-Objects
# adding with 10644 means normal file (TODO perhaps check if it should be marked as executable)
# cacheinfo means we have the hash, but no file in our work dir corresponding to it
git('update-index','--add','--cacheinfo','10644',hash,file)
tree=git('write-tree').strip()
clearIndex()
return tree
def getLatestSnapshot(backupname):
""" returns the (sha,refname) pair for the last snapshot. returns None if there is no last snapshot """
allRefs=getAllRefs()
snapshots=[]
for (ref,sha) in allRefs.items():
if ref.startswith('refs/gib/%s/snapshots/'%backupname):
snapshots.append((sha,ref))
snapshots.sort(key=lambda tup : tup[1],reverse=True)
return snapshots[0] if len(snapshots)>0 else None
def snapshot(args):
if len(args)<2:
fatal('Wrong number of parameters for snapshot command')
backupname=args[0]
backuppaths=args[1:]
last=getLatestSnapshot(backupname)
tree=makeTreeFromDir(backupname,backuppaths)
if not last or tree!=last[0]:
ref='refs/gib/%s/snapshots/%s'%(backupname,datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
git('update-ref',ref,tree)
print 'Made snapshot %s = %s'%(ref,tree)
else:
print "Didn't make snapshot, no changes since last snapshot on %s"%(last[1])
def delete_():
if len(sys.argv)!=4:
fatal('Wrong number of arguments for delete command')
ref='refs/gib/%s/snapshots/%s'%(sys.argv[2],sys.argv[3])
allRefs=getAllRefs()
if ref in allRefs:
git('update-ref','-d',ref)
else:
print 'Ref "%s" does not exist'%ref
def list_():
if len(sys.argv)!=2 and len(sys.argv)!=3:
fatal('Wrong number of arguments for list command')
if len(sys.argv)==2:
# list all snapshots
startwith='refs/gib/'
else:
startwith='refs/gib/%s/'%sys.argv[2]
allRefs=getAllRefs()
toPrint=[]
for (ref,sha) in allRefs.items():
if ref.startswith(startwith):
toPrint.append(ref[9:])
toPrint.sort()
print '\n'.join(toPrint)
def listremote():
if len(sys.argv)!=2 and len(sys.argv)!=3:
fatal('Wrong number of arguments for listremote command')
startswith='refs/gib/'
if len(sys.argv)==3:
startswith+=sys.argv[2]
for x in str(git('ls-remote','origin')).splitlines():
(has,ref)=x.split()
if ref.startswith(startswith):
print ref[9:]
def fetch():
if len(sys.argv)!=2 and len(sys.argv)!=3 and len(sys.argv)!=4:
fatal('Wrong number of arguments for fetch command')
startswith='refs/gib/'
if len(sys.argv)>=3:
startswith+=sys.argv[2]
if len(sys.argv)>=4:
startswith+='/snapshots/'+sys.argv[3]
for x in str(git('ls-remote','origin')).splitlines():
(has,ref)=x.split()
if ref.startswith(startswith):
os.system('git --git-dir=%s fetch origin %s:%s'%(gitdir,ref,ref))
def getAllRefs():
""" returns a dict with all refs in it, keyed by reference name """
allRefs={}
try:
for x in str(git('show-ref')).splitlines():
(sha,ref)=x.split(' ',1)
allRefs[ref]=sha
except pbs.ErrorReturnCode_1:
pass
return allRefs
def extract():
if len(sys.argv)!=5:
fatal('Wrong number of arguments for extract command')
(backupname,snapshotname,destdir)=sys.argv[2:5]
ref='refs/gib/%s/snapshots/%s'%(backupname,snapshotname)
allRefs=getAllRefs()
if ref in allRefs:
tree=allRefs[ref]
if os.path.exists(destdir):
fatal("Destination %s' already exists - cannot extract!"%(destdir))
clearIndex()
git('read-tree',tree)
if not destdir.endswith(os.sep):
destdir+=os.sep
if not os.path.isdir(destdir):
os.makedirs(destdir)
git('--work-tree=%s'%destdir,'checkout-index','-a')
# get rid of the .gibkeep files that were only added to track empty dirs
for file in str(git('ls-files','--cached')).splitlines():
if file.endswith('.gibkeep'):
pbs.rm(os.path.join(destdir,file))
clearIndex()
print "Extracted backup of '%s' snapshot '%s' to '%s'"%(backupname,snapshotname,destdir)
else:
fatal('Snapshot %s for backup %s does not exist\nTested %s'%(snapshotname,backupname,ref))
def usage():
print 'gib 0.1'
print ' A backup tool that uses git. Run from inside a git repro to backup'
print ' files into the repro'
print
print 'Usage:'
print
print 'gib snapshot <backupname> <path to backup>+'
print ' will take a snapshot of the given path(s) and save it'
print ' directories will be recursively backed up and placed in a dir at the'
print ' root level of the snapshot'
print ' if a path is a file, the file will be backed up to the root level of'
print ' the snapshot'
print ' it will write the tree to refs/gib/backupname/snapshots/YYYYMMDD_HHMMSS'
print
print 'gib list [backupname]'
print ' will list all available snapshots for [backupname]'
print ' if backupname is ommitted, it will list all available snapshots for all'
print ' backups'
print
print 'gib extract <backupname> <snapshotname> <destdir>'
print ' extract a backup to the directory <destdir>'
print ' <destdir> must not already exist'
print
print 'gib delete <backupname> <snapshotname>'
print ' removes a backup from the system'
print ' space won\'t be reclaimed until a "git gc" is done'
print
print 'gib list-remote [backupname]'
print ' list all remote backups for backupname'
print ' if backupname is omitted, lists all remote backups'
print ' always lists the git remote named "origin"'
print
print 'gib fetch <backupname> <snapshotname>'
print ' fetches a remote branch'
print ' after fetching, you can extract it'
print ' always fetches from the git remote named "origin"'
print
invokedFromShell=False
def fatal(x):
print x
if invokedFromShell:
sys.exit(1)
else:
raise(Exception(x))
if __name__ == "__main__":
invokedFromShell=True
if not (os.path.isdir(os.path.join(gitdir,'objects')) and os.path.isdir(os.path.join(gitdir,'refs'))):
fatal("Should be ran from inside the git repro")
if len(sys.argv)==1:
usage()
sys.exit(1)
if sys.argv[1]=='snapshot':
snapshot(sys.argv[2:])
elif sys.argv[1]=='list':
list_()
elif sys.argv[1]=='extract':
extract()
elif sys.argv[1]=='delete':
delete_()
elif sys.argv[1]=='list-remote':
listremote()
elif sys.argv[1]=='fetch':
fetch()
elif sys.argv[1]=='help' or sys.argv[1]=='--help':
usage()
else:
print 'Unknown command %s'%sys.argv[1]
sys.exit(1)