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= hostname:/path/to/repo of remote repository
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 d,date= date for the commit (seconds since the epoch)
15 v,verbose increase log output (can be used more than once)
16 q,quiet don't show progress meter
17 smaller= only back up files smaller than n bytes
18 bwlimit= maximum bytes/sec to transmit to server
20 o = options.Options('bup save', optspec)
21 (opt, flags, extra) = o.parse(sys.argv[1:])
23 git.check_repo_or_die()
24 if not (opt.tree or opt.commit or opt.name):
25 o.fatal("use one or more of -t, -c, -n")
27 o.fatal("no filenames given")
29 opt.progress = (istty and not opt.quiet)
30 opt.smaller = parse_num(opt.smaller or 0)
32 client.bwlimit = parse_num(opt.bwlimit)
35 date = parse_date_or_fatal(opt.date, o.fatal)
39 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
40 if is_reverse and opt.remote:
41 o.fatal("don't use -r in reverse mode; it's automatic")
43 refname = opt.name and 'refs/heads/%s' % opt.name or None
44 if opt.remote or is_reverse:
45 if opt.remote and opt.remote.find(":") == -1:
46 o.fatal("--remote argument must contain a colon")
48 cli = client.Client(opt.remote)
49 except client.ClientError:
50 o.fatal("server exited unexpectedly; see errors above")
51 oldref = refname and cli.read_ref(refname) or None
52 w = cli.new_packwriter()
55 oldref = refname and git.read_ref(refname) or None
77 assert(len(parts) >= 1)
79 shalist = shalists.pop()
80 tree = force_tree or w.new_tree(shalist)
82 shalists[-1].append(('40000',
83 git.mangle_name(part, 040000, 40000),
85 else: # this was the toplevel, so put it back for sanity
86 shalists.append(shalist)
91 def progress_report(n):
92 global count, subcount, lastremain, lastprint
95 pct = total and (cc*100.0/total) or 0
97 elapsed = now - tstart
98 kps = elapsed and int(cc/1024./elapsed)
99 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
100 kps = int(kps/kps_frac)*kps_frac
102 remain = elapsed*1.0/cc * (total-cc)
105 if (lastremain and (remain > lastremain)
106 and ((remain - lastremain)/lastremain < 0.05)):
110 hours = int(remain/60/60)
111 mins = int(remain/60 - hours*60)
112 secs = int(remain - hours*60*60 - mins*60)
117 kpsstr = '%dk/s' % kps
119 remainstr = '%dh%dm' % (hours, mins)
121 remainstr = '%dm%d' % (mins, secs)
123 remainstr = '%ds' % secs
124 if now - lastprint > 0.1:
125 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
126 % (pct, cc/1024, total/1024, fcount, ftotal,
137 r = index.Reader(git.repo('bupindex'))
139 def already_saved(ent):
140 return ent.is_valid() and w.exists(ent.sha) and ent.sha
142 def wantrecurse_pre(ent):
143 return not already_saved(ent)
145 def wantrecurse_during(ent):
146 return not already_saved(ent) or ent.sha_missing()
150 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
151 if not (ftotal % 10024):
152 progress('Reading index: %d\r' % ftotal)
153 exists = ent.exists()
154 hashvalid = already_saved(ent)
155 ent.set_sha_missing(not hashvalid)
156 if not opt.smaller or ent.size < opt.smaller:
157 if exists and not hashvalid:
160 progress('Reading index: %d, done.\n' % ftotal)
161 hashsplit.progress_callback = progress_report
164 count = subcount = fcount = 0
167 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
168 (dir, file) = os.path.split(ent.name)
169 exists = (ent.flags & index.IX_EXISTS)
170 hashvalid = already_saved(ent)
171 wasmissing = ent.sha_missing()
177 if ent.sha == index.EMPTY_SHA:
184 vlog('%s %-70s\n' % (status, ent.name))
185 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
186 if not lastdir.startswith(dir):
187 vlog('%s %-70s\n' % (status, os.path.join(dir, '')))
196 if opt.smaller and ent.size >= opt.smaller:
197 if exists and not hashvalid:
198 add_error('skipping large file "%s"' % ent.name)
199 lastskip_name = ent.name
202 assert(dir.startswith('/'))
203 dirp = dir.split('/')
205 _pop(force_tree = None)
207 for part in dirp[len(parts):]:
211 # no filename portion means this is a subdir. But
212 # sub/parentdirectories already handled in the pop/push() part above.
213 oldtree = already_saved(ent) # may be None
214 newtree = _pop(force_tree = oldtree)
216 if lastskip_name and lastskip_name.startswith(ent.name):
219 ent.validate(040000, newtree)
221 if exists and wasmissing:
225 # it's not a directory
228 mode = '%o' % ent.gitmode
230 shalists[-1].append((mode,
231 git.mangle_name(file, ent.mode, ent.gitmode),
234 if stat.S_ISREG(ent.mode):
236 f = hashsplit.open_noatime(ent.name)
239 lastskip_name = ent.name
242 lastskip_name = ent.name
245 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f],
246 keep_boundaries=False)
248 add_error('%s: %s' % (ent.name, e))
249 lastskip_name = ent.name
251 if stat.S_ISDIR(ent.mode):
252 assert(0) # handled above
253 elif stat.S_ISLNK(ent.mode):
255 rl = os.readlink(ent.name)
258 lastskip_name = ent.name
261 lastskip_name = ent.name
263 (mode, id) = ('120000', w.new_blob(rl))
265 add_error(Exception('skipping special file "%s"' % ent.name))
266 lastskip_name = ent.name
268 ent.validate(int(mode, 8), id)
270 shalists[-1].append((mode,
271 git.mangle_name(file, ent.mode, ent.gitmode),
273 if exists and wasmissing:
279 pct = total and count*100.0/total or 100
280 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
281 % (pct, count/1024, total/1024, fcount, ftotal))
283 while len(parts) > 1:
284 _pop(force_tree = None)
285 assert(len(shalists) == 1)
286 tree = w.new_tree(shalists[-1])
288 print tree.encode('hex')
289 if opt.commit or opt.name:
290 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
291 ref = opt.name and ('refs/heads/%s' % opt.name) or None
292 commit = w.new_commit(oldref, tree, date, msg)
294 print commit.encode('hex')
296 w.close() # must close before we can update the ref
300 cli.update_ref(refname, commit, oldref)
302 git.update_ref(refname, commit, oldref)
308 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))