2 import sys, 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
17 bwlimit= maximum bytes/sec to transmit to server
19 o = options.Options('bup save', optspec)
20 (opt, flags, extra) = o.parse(sys.argv[1:])
22 git.check_repo_or_die()
23 if not (opt.tree or opt.commit or opt.name):
24 o.fatal("use one or more of -t, -c, -n")
26 o.fatal("no filenames given")
28 opt.progress = (istty and not opt.quiet)
29 opt.smaller = parse_num(opt.smaller or 0)
31 client.bwlimit = parse_num(opt.bwlimit)
33 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
34 if is_reverse and opt.remote:
35 o.fatal("don't use -r in reverse mode; it's automatic")
37 refname = opt.name and 'refs/heads/%s' % opt.name or None
38 if opt.remote or is_reverse:
39 cli = client.Client(opt.remote)
40 oldref = refname and cli.read_ref(refname) or None
41 w = cli.new_packwriter()
44 oldref = refname and git.read_ref(refname) or None
66 assert(len(parts) >= 1)
68 shalist = shalists.pop()
69 tree = force_tree or w.new_tree(shalist)
71 shalists[-1].append(('40000', part, tree))
72 else: # this was the toplevel, so put it back for sanity
73 shalists.append(shalist)
78 def progress_report(n):
79 global count, subcount, lastremain, lastprint
82 pct = total and (cc*100.0/total) or 0
84 elapsed = now - tstart
85 kps = elapsed and int(cc/1024./elapsed)
86 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
87 kps = int(kps/kps_frac)*kps_frac
89 remain = elapsed*1.0/cc * (total-cc)
92 if (lastremain and (remain > lastremain)
93 and ((remain - lastremain)/lastremain < 0.05)):
97 hours = int(remain/60/60)
98 mins = int(remain/60 - hours*60)
99 secs = int(remain - hours*60*60 - mins*60)
104 kpsstr = '%dk/s' % kps
106 remainstr = '%dh%dm' % (hours, mins)
108 remainstr = '%dm%d' % (mins, secs)
110 remainstr = '%ds' % secs
111 if now - lastprint > 0.1:
112 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
113 % (pct, cc/1024, total/1024, fcount, ftotal,
118 r = index.Reader(git.repo('bupindex'))
120 def already_saved(ent):
121 return ent.is_valid() and w.exists(ent.sha) and ent.sha
123 def wantrecurse_pre(ent):
124 return not already_saved(ent)
126 def wantrecurse_during(ent):
127 return not already_saved(ent) or ent.sha_missing()
131 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
132 if not (ftotal % 10024):
133 progress('Reading index: %d\r' % ftotal)
134 exists = ent.exists()
135 hashvalid = already_saved(ent)
136 ent.set_sha_missing(not hashvalid)
137 if not opt.smaller or ent.size < opt.smaller:
138 if exists and not hashvalid:
141 progress('Reading index: %d, done.\n' % ftotal)
142 hashsplit.progress_callback = progress_report
145 count = subcount = fcount = 0
148 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
149 (dir, file) = os.path.split(ent.name)
150 exists = (ent.flags & index.IX_EXISTS)
151 hashvalid = already_saved(ent)
152 wasmissing = ent.sha_missing()
158 if ent.sha == index.EMPTY_SHA:
165 log('%s %-70s\n' % (status, ent.name))
166 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
167 if not lastdir.startswith(dir):
168 log('%s %-70s\n' % (status, os.path.join(dir, '')))
177 if opt.smaller and ent.size >= opt.smaller:
178 if exists and not hashvalid:
179 add_error('skipping large file "%s"' % ent.name)
180 lastskip_name = ent.name
183 assert(dir.startswith('/'))
184 dirp = dir.split('/')
186 _pop(force_tree = None)
188 for part in dirp[len(parts):]:
192 # no filename portion means this is a subdir. But
193 # sub/parentdirectories already handled in the pop/push() part above.
194 oldtree = already_saved(ent) # may be None
195 newtree = _pop(force_tree = oldtree)
197 if lastskip_name and lastskip_name.startswith(ent.name):
200 ent.validate(040000, newtree)
202 if exists and wasmissing:
206 # it's not a directory
209 mode = '%o' % ent.gitmode
211 shalists[-1].append((mode,
212 git.mangle_name(file, ent.mode, ent.gitmode),
215 if stat.S_ISREG(ent.mode):
217 f = hashsplit.open_noatime(ent.name)
220 lastskip_name = ent.name
223 lastskip_name = ent.name
225 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
227 if stat.S_ISDIR(ent.mode):
228 assert(0) # handled above
229 elif stat.S_ISLNK(ent.mode):
231 rl = os.readlink(ent.name)
234 lastskip_name = ent.name
237 lastskip_name = ent.name
239 (mode, id) = ('120000', w.new_blob(rl))
241 add_error(Exception('skipping special file "%s"' % ent.name))
242 lastskip_name = ent.name
244 ent.validate(int(mode, 8), id)
246 shalists[-1].append((mode,
247 git.mangle_name(file, ent.mode, ent.gitmode),
249 if exists and wasmissing:
255 pct = total and count*100.0/total or 100
256 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
257 % (pct, count/1024, total/1024, fcount, ftotal))
259 while len(parts) > 1:
260 _pop(force_tree = None)
261 assert(len(shalists) == 1)
262 tree = w.new_tree(shalists[-1])
264 print tree.encode('hex')
265 if opt.commit or opt.name:
266 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
267 ref = opt.name and ('refs/heads/%s' % opt.name) or None
268 commit = w.new_commit(oldref, tree, msg)
270 print commit.encode('hex')
272 w.close() # must close before we can update the ref
276 cli.update_ref(refname, commit, oldref)
278 git.update_ref(refname, commit, oldref)
284 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))