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)
28 opt.smaller = parse_num(opt.smaller or 0)
30 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
31 if is_reverse and opt.remote:
32 o.fatal("don't use -r in reverse mode; it's automatic")
34 refname = opt.name and 'refs/heads/%s' % opt.name or None
35 if opt.remote or is_reverse:
36 cli = client.Client(opt.remote)
37 oldref = refname and cli.read_ref(refname) or None
38 w = cli.new_packwriter()
41 oldref = refname and git.read_ref(refname) or None
63 assert(len(parts) >= 1)
65 shalist = shalists.pop()
66 tree = force_tree or w.new_tree(shalist)
68 shalists[-1].append(('40000', part, tree))
69 else: # this was the toplevel, so put it back for sanity
70 shalists.append(shalist)
74 def progress_report(n):
75 global count, subcount, lastremain
78 pct = total and (cc*100.0/total) or 0
80 elapsed = now - tstart
81 kps = elapsed and int(cc/1024./elapsed)
82 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
83 kps = int(kps/kps_frac)*kps_frac
85 remain = elapsed*1.0/cc * (total-cc)
88 if (lastremain and (remain > lastremain)
89 and ((remain - lastremain)/lastremain < 0.05)):
93 hours = int(remain/60/60)
94 mins = int(remain/60 - hours*60)
95 secs = int(remain - hours*60*60 - mins*60)
100 kpsstr = '%dk/s' % kps
102 remainstr = '%dh%dm' % (hours, mins)
104 remainstr = '%dm%d' % (mins, secs)
106 remainstr = '%ds' % secs
107 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
108 % (pct, cc/1024, total/1024, fcount, ftotal,
112 r = index.Reader(git.repo('bupindex'))
114 def already_saved(ent):
115 return ent.is_valid() and w.exists(ent.sha) and ent.sha
117 def wantrecurse_pre(ent):
118 return not already_saved(ent)
120 def wantrecurse_during(ent):
121 return not already_saved(ent) or ent.sha_missing()
125 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
126 if not (ftotal % 10024):
127 progress('Reading index: %d\r' % ftotal)
128 exists = ent.exists()
129 hashvalid = already_saved(ent)
130 ent.set_sha_missing(not hashvalid)
131 if not opt.smaller or ent.size < opt.smaller:
132 if exists and not hashvalid:
135 progress('Reading index: %d, done.\n' % ftotal)
136 hashsplit.progress_callback = progress_report
139 count = subcount = fcount = 0
142 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
143 (dir, file) = os.path.split(ent.name)
144 exists = (ent.flags & index.IX_EXISTS)
145 hashvalid = already_saved(ent)
146 wasmissing = ent.sha_missing()
152 if ent.sha == index.EMPTY_SHA:
159 log('%s %-70s\n' % (status, ent.name))
160 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
161 if not lastdir.startswith(dir):
162 log('%s %-70s\n' % (status, os.path.join(dir, '')))
171 if opt.smaller and ent.size >= opt.smaller:
172 if exists and not hashvalid:
173 add_error('skipping large file "%s"' % ent.name)
174 lastskip_name = ent.name
177 assert(dir.startswith('/'))
178 dirp = dir.split('/')
180 _pop(force_tree = None)
182 for part in dirp[len(parts):]:
186 # no filename portion means this is a subdir. But
187 # sub/parentdirectories already handled in the pop/push() part above.
188 oldtree = already_saved(ent) # may be None
189 newtree = _pop(force_tree = oldtree)
191 if lastskip_name and lastskip_name.startswith(ent.name):
194 ent.validate(040000, newtree)
196 if exists and wasmissing:
200 # it's not a directory
203 mode = '%o' % ent.gitmode
205 shalists[-1].append((mode,
206 git.mangle_name(file, ent.mode, ent.gitmode),
209 if stat.S_ISREG(ent.mode):
211 f = hashsplit.open_noatime(ent.name)
214 lastskip_name = ent.name
217 lastskip_name = ent.name
219 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
221 if stat.S_ISDIR(ent.mode):
222 assert(0) # handled above
223 elif stat.S_ISLNK(ent.mode):
225 rl = os.readlink(ent.name)
228 lastskip_name = ent.name
231 lastskip_name = ent.name
233 (mode, id) = ('120000', w.new_blob(rl))
235 add_error(Exception('skipping special file "%s"' % ent.name))
236 lastskip_name = ent.name
238 ent.validate(int(mode, 8), id)
240 shalists[-1].append((mode,
241 git.mangle_name(file, ent.mode, ent.gitmode),
243 if exists and wasmissing:
249 pct = total and count*100.0/total or 100
250 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
251 % (pct, count/1024, total/1024, fcount, ftotal))
253 while len(parts) > 1:
254 _pop(force_tree = None)
255 assert(len(shalists) == 1)
256 tree = w.new_tree(shalists[-1])
258 print tree.encode('hex')
259 if opt.commit or opt.name:
260 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
261 ref = opt.name and ('refs/heads/%s' % opt.name) or None
262 commit = w.new_commit(oldref, tree, msg)
264 print commit.encode('hex')
266 w.close() # must close before we can update the ref
270 cli.update_ref(refname, commit, oldref)
272 git.update_ref(refname, commit, oldref)
278 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))