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 if opt.name and opt.name.startswith('.'):
44 o.fatal("'%s' is not a valid branch name" % opt.name)
45 refname = opt.name and 'refs/heads/%s' % opt.name or None
46 if opt.remote or is_reverse:
47 cli = client.Client(opt.remote)
48 oldref = refname and cli.read_ref(refname) or None
49 w = cli.new_packwriter()
52 oldref = refname and git.read_ref(refname) or None
74 assert(len(parts) >= 1)
76 shalist = shalists.pop()
77 tree = force_tree or w.new_tree(shalist)
79 shalists[-1].append(('40000',
80 git.mangle_name(part, 040000, 40000),
82 else: # this was the toplevel, so put it back for sanity
83 shalists.append(shalist)
88 def progress_report(n):
89 global count, subcount, lastremain, lastprint
92 pct = total and (cc*100.0/total) or 0
94 elapsed = now - tstart
95 kps = elapsed and int(cc/1024./elapsed)
96 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
97 kps = int(kps/kps_frac)*kps_frac
99 remain = elapsed*1.0/cc * (total-cc)
102 if (lastremain and (remain > lastremain)
103 and ((remain - lastremain)/lastremain < 0.05)):
107 hours = int(remain/60/60)
108 mins = int(remain/60 - hours*60)
109 secs = int(remain - hours*60*60 - mins*60)
114 kpsstr = '%dk/s' % kps
116 remainstr = '%dh%dm' % (hours, mins)
118 remainstr = '%dm%d' % (mins, secs)
120 remainstr = '%ds' % secs
121 if now - lastprint > 0.1:
122 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
123 % (pct, cc/1024, total/1024, fcount, ftotal,
134 r = index.Reader(git.repo('bupindex'))
136 def already_saved(ent):
137 return ent.is_valid() and w.exists(ent.sha) and ent.sha
139 def wantrecurse_pre(ent):
140 return not already_saved(ent)
142 def wantrecurse_during(ent):
143 return not already_saved(ent) or ent.sha_missing()
147 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
148 if not (ftotal % 10024):
149 progress('Reading index: %d\r' % ftotal)
150 exists = ent.exists()
151 hashvalid = already_saved(ent)
152 ent.set_sha_missing(not hashvalid)
153 if not opt.smaller or ent.size < opt.smaller:
154 if exists and not hashvalid:
157 progress('Reading index: %d, done.\n' % ftotal)
158 hashsplit.progress_callback = progress_report
161 count = subcount = fcount = 0
164 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
165 (dir, file) = os.path.split(ent.name)
166 exists = (ent.flags & index.IX_EXISTS)
167 hashvalid = already_saved(ent)
168 wasmissing = ent.sha_missing()
174 if ent.sha == index.EMPTY_SHA:
181 vlog('%s %-70s\n' % (status, ent.name))
182 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
183 if not lastdir.startswith(dir):
184 vlog('%s %-70s\n' % (status, os.path.join(dir, '')))
193 if opt.smaller and ent.size >= opt.smaller:
194 if exists and not hashvalid:
195 add_error('skipping large file "%s"' % ent.name)
196 lastskip_name = ent.name
199 assert(dir.startswith('/'))
200 dirp = dir.split('/')
202 _pop(force_tree = None)
204 for part in dirp[len(parts):]:
208 # no filename portion means this is a subdir. But
209 # sub/parentdirectories already handled in the pop/push() part above.
210 oldtree = already_saved(ent) # may be None
211 newtree = _pop(force_tree = oldtree)
213 if lastskip_name and lastskip_name.startswith(ent.name):
216 ent.validate(040000, newtree)
218 if exists and wasmissing:
222 # it's not a directory
225 mode = '%o' % ent.gitmode
227 shalists[-1].append((mode,
228 git.mangle_name(file, ent.mode, ent.gitmode),
231 if stat.S_ISREG(ent.mode):
233 f = hashsplit.open_noatime(ent.name)
236 lastskip_name = ent.name
239 lastskip_name = ent.name
242 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f],
243 keep_boundaries=False)
245 add_error('%s: %s' % (ent.name, e))
246 lastskip_name = ent.name
248 if stat.S_ISDIR(ent.mode):
249 assert(0) # handled above
250 elif stat.S_ISLNK(ent.mode):
252 rl = os.readlink(ent.name)
255 lastskip_name = ent.name
258 lastskip_name = ent.name
260 (mode, id) = ('120000', w.new_blob(rl))
262 add_error(Exception('skipping special file "%s"' % ent.name))
263 lastskip_name = ent.name
265 ent.validate(int(mode, 8), id)
267 shalists[-1].append((mode,
268 git.mangle_name(file, ent.mode, ent.gitmode),
270 if exists and wasmissing:
276 pct = total and count*100.0/total or 100
277 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
278 % (pct, count/1024, total/1024, fcount, ftotal))
280 while len(parts) > 1:
281 _pop(force_tree = None)
282 assert(len(shalists) == 1)
283 tree = w.new_tree(shalists[-1])
285 print tree.encode('hex')
286 if opt.commit or opt.name:
287 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
288 commit = w.new_commit(oldref, tree, date, msg)
290 print commit.encode('hex')
292 w.close() # must close before we can update the ref
296 cli.update_ref(refname, commit, oldref)
298 git.update_ref(refname, commit, oldref)
304 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))