2 import sys, stat, time, math
3 from bup import hashsplit, git, options, index, client
4 from bup.helpers import *
5 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE, GIT_MODE_SYMLINK
9 bup save [-tc] [-n name] <filenames...>
11 r,remote= hostname:/path/to/repo of remote repository
12 t,tree output a tree id
13 c,commit output a commit id
14 n,name= name of backup set to update (if any)
15 d,date= date for the commit (seconds since the epoch)
16 v,verbose increase log output (can be used more than once)
17 q,quiet don't show progress meter
18 smaller= only back up files smaller than n bytes
19 bwlimit= maximum bytes/sec to transmit to server
20 f,indexfile= the name of the index file (normally BUP_DIR/bupindex)
21 strip strips the path to every filename given
22 strip-path= path-prefix to be stripped when saving
23 graft= a graft point *old_path*=*new_path* (can be used more than once)
24 #,compress= set compression level to # (0-9, 9 is highest) [1]
26 o = options.Options(optspec)
27 (opt, flags, extra) = o.parse(sys.argv[1:])
29 git.check_repo_or_die()
30 if not (opt.tree or opt.commit or opt.name):
31 o.fatal("use one or more of -t, -c, -n")
33 o.fatal("no filenames given")
35 opt.progress = (istty2 and not opt.quiet)
36 opt.smaller = parse_num(opt.smaller or 0)
38 client.bwlimit = parse_num(opt.bwlimit)
41 date = parse_date_or_fatal(opt.date, o.fatal)
45 if opt.strip and opt.strip_path:
46 o.fatal("--strip is incompatible with --strip-path")
51 o.fatal("--strip is incompatible with --graft")
54 o.fatal("--strip-path is incompatible with --graft")
56 for (option, parameter) in flags:
57 if option == "--graft":
58 splitted_parameter = parameter.split('=')
59 if len(splitted_parameter) != 2:
60 o.fatal("a graft point must be of the form old_path=new_path")
61 graft_points.append((realpath(splitted_parameter[0]),
62 realpath(splitted_parameter[1])))
64 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
65 if is_reverse and opt.remote:
66 o.fatal("don't use -r in reverse mode; it's automatic")
68 if opt.name and opt.name.startswith('.'):
69 o.fatal("'%s' is not a valid branch name" % opt.name)
70 refname = opt.name and 'refs/heads/%s' % opt.name or None
71 if opt.remote or is_reverse:
72 cli = client.Client(opt.remote)
73 oldref = refname and cli.read_ref(refname) or None
74 w = cli.new_packwriter()
77 oldref = refname and git.read_ref(refname) or None
78 w = git.PackWriter(compression_level=opt.compress)
99 assert(len(parts) >= 1)
101 shalist = shalists.pop()
102 tree = force_tree or w.new_tree(shalist)
104 shalists[-1].append((GIT_MODE_TREE,
105 git.mangle_name(part,
106 GIT_MODE_TREE, GIT_MODE_TREE),
108 else: # this was the toplevel, so put it back for sanity
109 shalists.append(shalist)
113 def progress_report(n):
114 global count, subcount, lastremain
116 cc = count + subcount
117 pct = total and (cc*100.0/total) or 0
119 elapsed = now - tstart
120 kps = elapsed and int(cc/1024./elapsed)
121 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
122 kps = int(kps/kps_frac)*kps_frac
124 remain = elapsed*1.0/cc * (total-cc)
127 if (lastremain and (remain > lastremain)
128 and ((remain - lastremain)/lastremain < 0.05)):
132 hours = int(remain/60/60)
133 mins = int(remain/60 - hours*60)
134 secs = int(remain - hours*60*60 - mins*60)
139 kpsstr = '%dk/s' % kps
141 remainstr = '%dh%dm' % (hours, mins)
143 remainstr = '%dm%d' % (mins, secs)
145 remainstr = '%ds' % secs
146 qprogress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
147 % (pct, cc/1024, total/1024, fcount, ftotal,
151 indexfile = opt.indexfile or git.repo('bupindex')
152 r = index.Reader(indexfile)
154 def already_saved(ent):
155 return ent.is_valid() and w.exists(ent.sha) and ent.sha
157 def wantrecurse_pre(ent):
158 return not already_saved(ent)
160 def wantrecurse_during(ent):
161 return not already_saved(ent) or ent.sha_missing()
165 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
166 if not (ftotal % 10024):
167 qprogress('Reading index: %d\r' % ftotal)
168 exists = ent.exists()
169 hashvalid = already_saved(ent)
170 ent.set_sha_missing(not hashvalid)
171 if not opt.smaller or ent.size < opt.smaller:
172 if exists and not hashvalid:
175 progress('Reading index: %d, done.\n' % ftotal)
176 hashsplit.progress_callback = progress_report
179 count = subcount = fcount = 0
182 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
183 (dir, file) = os.path.split(ent.name)
184 exists = (ent.flags & index.IX_EXISTS)
185 hashvalid = already_saved(ent)
186 wasmissing = ent.sha_missing()
192 if ent.sha == index.EMPTY_SHA:
199 log('%s %-70s\n' % (status, ent.name))
200 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
201 if not lastdir.startswith(dir):
202 log('%s %-70s\n' % (status, os.path.join(dir, '')))
211 if opt.smaller and ent.size >= opt.smaller:
212 if exists and not hashvalid:
213 add_error('skipping large file "%s"' % ent.name)
214 lastskip_name = ent.name
217 assert(dir.startswith('/'))
219 stripped_base_path = strip_base_path(dir, extra)
220 dirp = stripped_base_path.split('/')
222 dirp = strip_path(opt.strip_path, dir).split('/')
224 grafted = graft_path(graft_points, dir)
225 dirp = grafted.split('/')
227 dirp = dir.split('/')
229 _pop(force_tree = None)
231 for part in dirp[len(parts):]:
235 # no filename portion means this is a subdir. But
236 # sub/parentdirectories already handled in the pop/push() part above.
237 oldtree = already_saved(ent) # may be None
238 newtree = _pop(force_tree = oldtree)
240 if lastskip_name and lastskip_name.startswith(ent.name):
243 ent.validate(GIT_MODE_TREE, newtree)
245 if exists and wasmissing:
249 # it's not a directory
253 shalists[-1].append((ent.gitmode,
254 git.mangle_name(file, ent.mode, ent.gitmode),
257 if stat.S_ISREG(ent.mode):
259 f = hashsplit.open_noatime(ent.name)
260 except (IOError, OSError), e:
262 lastskip_name = ent.name
265 (mode, id) = hashsplit.split_to_blob_or_tree(
266 w.new_blob, w.new_tree, [f],
267 keep_boundaries=False)
268 except (IOError, OSError), e:
269 add_error('%s: %s' % (ent.name, e))
270 lastskip_name = ent.name
272 if stat.S_ISDIR(ent.mode):
273 assert(0) # handled above
274 elif stat.S_ISLNK(ent.mode):
276 rl = os.readlink(ent.name)
277 except (OSError, IOError), e:
279 lastskip_name = ent.name
281 (mode, id) = (GIT_MODE_SYMLINK, w.new_blob(rl))
283 add_error(Exception('skipping special file "%s"' % ent.name))
284 lastskip_name = ent.name
286 ent.validate(mode, id)
288 shalists[-1].append((mode,
289 git.mangle_name(file, ent.mode, ent.gitmode),
291 if exists and wasmissing:
297 pct = total and count*100.0/total or 100
298 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
299 % (pct, count/1024, total/1024, fcount, ftotal))
301 while len(parts) > 1:
302 _pop(force_tree = None)
303 assert(len(shalists) == 1)
304 tree = w.new_tree(shalists[-1])
306 print tree.encode('hex')
307 if opt.commit or opt.name:
308 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
309 commit = w.new_commit(oldref, tree, date, msg)
311 print commit.encode('hex')
313 w.close() # must close before we can update the ref
317 cli.update_ref(refname, commit, oldref)
319 git.update_ref(refname, commit, oldref)
325 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))