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
19 f,indexfile= the name of the index file (normally BUP_DIR/bupindex)
20 strip strips the path to every filename given
21 strip-path= path-prefix to be stripped when saving
23 o = options.Options('bup save', optspec)
24 (opt, flags, extra) = o.parse(sys.argv[1:])
26 git.check_repo_or_die()
27 if not (opt.tree or opt.commit or opt.name):
28 o.fatal("use one or more of -t, -c, -n")
30 o.fatal("no filenames given")
32 opt.progress = (istty and not opt.quiet)
33 opt.smaller = parse_num(opt.smaller or 0)
35 client.bwlimit = parse_num(opt.bwlimit)
38 date = parse_date_or_fatal(opt.date, o.fatal)
42 if opt.strip and opt.strip_path:
43 o.fatal("--strip is incompatible with --strip-path")
45 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
46 if is_reverse and opt.remote:
47 o.fatal("don't use -r in reverse mode; it's automatic")
49 if opt.name and opt.name.startswith('.'):
50 o.fatal("'%s' is not a valid branch name" % opt.name)
51 refname = opt.name and 'refs/heads/%s' % opt.name or None
52 if opt.remote or is_reverse:
53 cli = client.Client(opt.remote)
54 oldref = refname and cli.read_ref(refname) or None
55 w = cli.new_packwriter()
58 oldref = refname and git.read_ref(refname) or None
80 assert(len(parts) >= 1)
82 shalist = shalists.pop()
83 tree = force_tree or w.new_tree(shalist)
85 shalists[-1].append(('40000',
86 git.mangle_name(part, 040000, 40000),
88 else: # this was the toplevel, so put it back for sanity
89 shalists.append(shalist)
94 def progress_report(n):
95 global count, subcount, lastremain, lastprint
98 pct = total and (cc*100.0/total) or 0
100 elapsed = now - tstart
101 kps = elapsed and int(cc/1024./elapsed)
102 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
103 kps = int(kps/kps_frac)*kps_frac
105 remain = elapsed*1.0/cc * (total-cc)
108 if (lastremain and (remain > lastremain)
109 and ((remain - lastremain)/lastremain < 0.05)):
113 hours = int(remain/60/60)
114 mins = int(remain/60 - hours*60)
115 secs = int(remain - hours*60*60 - mins*60)
120 kpsstr = '%dk/s' % kps
122 remainstr = '%dh%dm' % (hours, mins)
124 remainstr = '%dm%d' % (mins, secs)
126 remainstr = '%ds' % secs
127 if now - lastprint > 0.1:
128 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
129 % (pct, cc/1024, total/1024, fcount, ftotal,
140 indexfile = opt.indexfile or git.repo('bupindex')
142 r = index.Reader(indexfile)
144 def already_saved(ent):
145 return ent.is_valid() and w.exists(ent.sha) and ent.sha
147 def wantrecurse_pre(ent):
148 return not already_saved(ent)
150 def wantrecurse_during(ent):
151 return not already_saved(ent) or ent.sha_missing()
155 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
156 if not (ftotal % 10024):
157 progress('Reading index: %d\r' % ftotal)
158 exists = ent.exists()
159 hashvalid = already_saved(ent)
160 ent.set_sha_missing(not hashvalid)
161 if not opt.smaller or ent.size < opt.smaller:
162 if exists and not hashvalid:
165 progress('Reading index: %d, done.\n' % ftotal)
166 hashsplit.progress_callback = progress_report
169 count = subcount = fcount = 0
172 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
173 (dir, file) = os.path.split(ent.name)
174 exists = (ent.flags & index.IX_EXISTS)
175 hashvalid = already_saved(ent)
176 wasmissing = ent.sha_missing()
182 if ent.sha == index.EMPTY_SHA:
189 vlog('%s %-70s\n' % (status, ent.name))
190 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
191 if not lastdir.startswith(dir):
192 vlog('%s %-70s\n' % (status, os.path.join(dir, '')))
201 if opt.smaller and ent.size >= opt.smaller:
202 if exists and not hashvalid:
203 add_error('skipping large file "%s"' % ent.name)
204 lastskip_name = ent.name
207 assert(dir.startswith('/'))
209 stripped_base_path = strip_base_path(dir, extra)
210 dirp = stripped_base_path.split('/')
212 dirp = strip_path(opt.strip_path, dir).split('/')
214 dirp = dir.split('/')
216 _pop(force_tree = None)
218 for part in dirp[len(parts):]:
222 # no filename portion means this is a subdir. But
223 # sub/parentdirectories already handled in the pop/push() part above.
224 oldtree = already_saved(ent) # may be None
225 newtree = _pop(force_tree = oldtree)
227 if lastskip_name and lastskip_name.startswith(ent.name):
230 ent.validate(040000, newtree)
232 if exists and wasmissing:
236 # it's not a directory
239 mode = '%o' % ent.gitmode
241 shalists[-1].append((mode,
242 git.mangle_name(file, ent.mode, ent.gitmode),
245 if stat.S_ISREG(ent.mode):
247 f = hashsplit.open_noatime(ent.name)
250 lastskip_name = ent.name
253 lastskip_name = ent.name
256 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f],
257 keep_boundaries=False)
259 add_error('%s: %s' % (ent.name, e))
260 lastskip_name = ent.name
262 if stat.S_ISDIR(ent.mode):
263 assert(0) # handled above
264 elif stat.S_ISLNK(ent.mode):
266 rl = os.readlink(ent.name)
269 lastskip_name = ent.name
272 lastskip_name = ent.name
274 (mode, id) = ('120000', w.new_blob(rl))
276 add_error(Exception('skipping special file "%s"' % ent.name))
277 lastskip_name = ent.name
279 ent.validate(int(mode, 8), id)
281 shalists[-1].append((mode,
282 git.mangle_name(file, ent.mode, ent.gitmode),
284 if exists and wasmissing:
290 pct = total and count*100.0/total or 100
291 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
292 % (pct, count/1024, total/1024, fcount, ftotal))
294 while len(parts) > 1:
295 _pop(force_tree = None)
296 assert(len(shalists) == 1)
297 tree = w.new_tree(shalists[-1])
299 print tree.encode('hex')
300 if opt.commit or opt.name:
301 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
302 commit = w.new_commit(oldref, tree, date, msg)
304 print commit.encode('hex')
306 w.close() # must close before we can update the ref
310 cli.update_ref(refname, commit, oldref)
312 git.update_ref(refname, commit, oldref)
318 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))