2 import sys, re, errno, stat, time, math
3 from bup import hashsplit, git, options, index, client
4 from bup.helpers import *
8 bup save [-tc] [-n name] <filenames...>
10 r,remote= remote repository path
11 t,tree output a tree id
12 c,commit output a commit id
13 n,name= name of backup set to update (if any)
14 v,verbose increase log output (can be used more than once)
15 q,quiet don't show progress meter
16 smaller= only back up files smaller than n bytes
18 o = options.Options('bup save', optspec)
19 (opt, flags, extra) = o.parse(sys.argv[1:])
21 git.check_repo_or_die()
22 if not (opt.tree or opt.commit or opt.name):
23 o.fatal("use one or more of -t, -c, -n")
25 o.fatal("no filenames given")
27 opt.progress = (istty and not opt.quiet)
29 refname = opt.name and 'refs/heads/%s' % opt.name or None
31 cli = client.Client(opt.remote)
32 oldref = refname and cli.read_ref(refname) or None
33 w = cli.new_packwriter()
36 oldref = refname and git.read_ref(refname) or None
56 assert(len(parts) > 1)
58 shalist = shalists.pop()
59 tree = force_tree or w.new_tree(shalist)
60 shalists[-1].append(('40000', part, tree))
64 def progress_report(n):
65 global count, lastremain
67 pct = total and (count*100.0/total) or 0
69 elapsed = now - tstart
70 kps = elapsed and int(count/1024./elapsed)
71 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
72 kps = int(kps/kps_frac)*kps_frac
74 remain = elapsed*1.0/count * (total-count)
77 if (lastremain and (remain > lastremain)
78 and ((remain - lastremain)/lastremain < 0.05)):
82 hours = int(remain/60/60)
83 mins = int(remain/60 - hours*60)
84 secs = int(remain - hours*60*60 - mins*60)
89 kpsstr = '%dk/s' % kps
91 remainstr = '%dh%dm' % (hours, mins)
93 remainstr = '%dm%d' % (mins, secs)
95 remainstr = '%ds' % secs
96 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
97 % (pct, count/1024, total/1024, fcount, ftotal,
101 r = index.Reader(git.repo('bupindex'))
103 def already_saved(ent):
104 return ent.is_valid() and w.exists(ent.sha) and ent.sha
106 def wantrecurse(ent):
107 return not already_saved(ent)
111 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse):
112 if not (ftotal % 10024):
113 progress('Reading index: %d\r' % ftotal)
114 exists = (ent.flags & index.IX_EXISTS)
115 hashvalid = already_saved(ent)
116 if exists and not hashvalid:
119 progress('Reading index: %d, done.\n' % ftotal)
120 hashsplit.progress_callback = progress_report
124 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse):
125 (dir, file) = os.path.split(ent.name)
126 exists = (ent.flags & index.IX_EXISTS)
127 hashvalid = already_saved(ent)
132 if ent.sha == index.EMPTY_SHA:
138 if opt.verbose >= 2 or stat.S_ISDIR(ent.mode):
139 log('%s %-70s\n' % (status, ent.name))
148 assert(dir.startswith('/'))
149 dirp = dir.split('/')
151 _pop(force_tree = None)
153 for part in dirp[len(parts):]:
157 # sub/parentdirectories already handled in the pop/push() part above.
158 oldtree = already_saved(ent) # may be None
159 newtree = _pop(force_tree = oldtree)
161 ent.validate(040000, newtree)
168 mode = '%o' % ent.gitmode
170 shalists[-1].append((mode, file, id))
171 elif opt.smaller and ent.size >= opt.smaller:
172 add_error('skipping large file "%s"' % ent.name)
174 if stat.S_ISREG(ent.mode):
182 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
184 if stat.S_ISDIR(ent.mode):
185 assert(0) # handled above
186 elif stat.S_ISLNK(ent.mode):
188 rl = os.readlink(ent.name)
194 (mode, id) = ('120000', w.new_blob(rl))
196 add_error(Exception('skipping special file "%s"' % ent.name))
199 ent.validate(int(mode, 8), id)
201 shalists[-1].append((mode, file, id))
204 pct = total and count*100.0/total or 100
205 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
206 % (pct, count/1024, total/1024, fcount, ftotal))
208 while len(parts) > 1:
209 _pop(force_tree = None)
210 assert(len(shalists) == 1)
211 tree = w.new_tree(shalists[-1])
213 print tree.encode('hex')
214 if opt.commit or opt.name:
215 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
216 ref = opt.name and ('refs/heads/%s' % opt.name) or None
217 commit = w.new_commit(oldref, tree, msg)
219 print commit.encode('hex')
221 w.close() # must close before we can update the ref
225 cli.update_ref(refname, commit, oldref)
227 git.update_ref(refname, commit, oldref)
233 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))