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 0 set compression-level to 0
25 9 set compression-level to 9
27 o = options.Options(optspec)
28 (opt, flags, extra) = o.parse(sys.argv[1:])
30 git.check_repo_or_die()
31 if not (opt.tree or opt.commit or opt.name):
32 o.fatal("use one or more of -t, -c, -n")
34 o.fatal("no filenames given")
43 opt.progress = (istty2 and not opt.quiet)
44 opt.smaller = parse_num(opt.smaller or 0)
46 client.bwlimit = parse_num(opt.bwlimit)
49 date = parse_date_or_fatal(opt.date, o.fatal)
53 if opt.strip and opt.strip_path:
54 o.fatal("--strip is incompatible with --strip-path")
59 o.fatal("--strip is incompatible with --graft")
62 o.fatal("--strip-path is incompatible with --graft")
64 for (option, parameter) in flags:
65 if option == "--graft":
66 splitted_parameter = parameter.split('=')
67 if len(splitted_parameter) != 2:
68 o.fatal("a graft point must be of the form old_path=new_path")
69 graft_points.append((realpath(splitted_parameter[0]),
70 realpath(splitted_parameter[1])))
72 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
73 if is_reverse and opt.remote:
74 o.fatal("don't use -r in reverse mode; it's automatic")
76 if opt.name and opt.name.startswith('.'):
77 o.fatal("'%s' is not a valid branch name" % opt.name)
78 refname = opt.name and 'refs/heads/%s' % opt.name or None
79 if opt.remote or is_reverse:
80 cli = client.Client(opt.remote)
81 oldref = refname and cli.read_ref(refname) or None
82 w = cli.new_packwriter()
85 oldref = refname and git.read_ref(refname) or None
86 w = git.PackWriter(compression_level=compression_level)
106 def _pop(force_tree):
107 assert(len(parts) >= 1)
109 shalist = shalists.pop()
110 tree = force_tree or w.new_tree(shalist)
112 shalists[-1].append((GIT_MODE_TREE,
113 git.mangle_name(part,
114 GIT_MODE_TREE, GIT_MODE_TREE),
116 else: # this was the toplevel, so put it back for sanity
117 shalists.append(shalist)
121 def progress_report(n):
122 global count, subcount, lastremain
124 cc = count + subcount
125 pct = total and (cc*100.0/total) or 0
127 elapsed = now - tstart
128 kps = elapsed and int(cc/1024./elapsed)
129 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
130 kps = int(kps/kps_frac)*kps_frac
132 remain = elapsed*1.0/cc * (total-cc)
135 if (lastremain and (remain > lastremain)
136 and ((remain - lastremain)/lastremain < 0.05)):
140 hours = int(remain/60/60)
141 mins = int(remain/60 - hours*60)
142 secs = int(remain - hours*60*60 - mins*60)
147 kpsstr = '%dk/s' % kps
149 remainstr = '%dh%dm' % (hours, mins)
151 remainstr = '%dm%d' % (mins, secs)
153 remainstr = '%ds' % secs
154 qprogress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
155 % (pct, cc/1024, total/1024, fcount, ftotal,
159 indexfile = opt.indexfile or git.repo('bupindex')
160 r = index.Reader(indexfile)
162 def already_saved(ent):
163 return ent.is_valid() and w.exists(ent.sha) and ent.sha
165 def wantrecurse_pre(ent):
166 return not already_saved(ent)
168 def wantrecurse_during(ent):
169 return not already_saved(ent) or ent.sha_missing()
173 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_pre):
174 if not (ftotal % 10024):
175 qprogress('Reading index: %d\r' % ftotal)
176 exists = ent.exists()
177 hashvalid = already_saved(ent)
178 ent.set_sha_missing(not hashvalid)
179 if not opt.smaller or ent.size < opt.smaller:
180 if exists and not hashvalid:
183 progress('Reading index: %d, done.\n' % ftotal)
184 hashsplit.progress_callback = progress_report
187 count = subcount = fcount = 0
190 for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
191 (dir, file) = os.path.split(ent.name)
192 exists = (ent.flags & index.IX_EXISTS)
193 hashvalid = already_saved(ent)
194 wasmissing = ent.sha_missing()
200 if ent.sha == index.EMPTY_SHA:
207 log('%s %-70s\n' % (status, ent.name))
208 elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
209 if not lastdir.startswith(dir):
210 log('%s %-70s\n' % (status, os.path.join(dir, '')))
219 if opt.smaller and ent.size >= opt.smaller:
220 if exists and not hashvalid:
221 add_error('skipping large file "%s"' % ent.name)
222 lastskip_name = ent.name
225 assert(dir.startswith('/'))
227 stripped_base_path = strip_base_path(dir, extra)
228 dirp = stripped_base_path.split('/')
230 dirp = strip_path(opt.strip_path, dir).split('/')
232 grafted = graft_path(graft_points, dir)
233 dirp = grafted.split('/')
235 dirp = dir.split('/')
237 _pop(force_tree = None)
239 for part in dirp[len(parts):]:
243 # no filename portion means this is a subdir. But
244 # sub/parentdirectories already handled in the pop/push() part above.
245 oldtree = already_saved(ent) # may be None
246 newtree = _pop(force_tree = oldtree)
248 if lastskip_name and lastskip_name.startswith(ent.name):
251 ent.validate(GIT_MODE_TREE, newtree)
253 if exists and wasmissing:
257 # it's not a directory
261 shalists[-1].append((ent.gitmode,
262 git.mangle_name(file, ent.mode, ent.gitmode),
265 if stat.S_ISREG(ent.mode):
267 f = hashsplit.open_noatime(ent.name)
268 except (IOError, OSError), e:
270 lastskip_name = ent.name
273 (mode, id) = hashsplit.split_to_blob_or_tree(
274 w.new_blob, w.new_tree, [f],
275 keep_boundaries=False)
276 except (IOError, OSError), e:
277 add_error('%s: %s' % (ent.name, e))
278 lastskip_name = ent.name
280 if stat.S_ISDIR(ent.mode):
281 assert(0) # handled above
282 elif stat.S_ISLNK(ent.mode):
284 rl = os.readlink(ent.name)
285 except (OSError, IOError), e:
287 lastskip_name = ent.name
289 (mode, id) = (GIT_MODE_SYMLINK, w.new_blob(rl))
291 add_error(Exception('skipping special file "%s"' % ent.name))
292 lastskip_name = ent.name
294 ent.validate(mode, id)
296 shalists[-1].append((mode,
297 git.mangle_name(file, ent.mode, ent.gitmode),
299 if exists and wasmissing:
305 pct = total and count*100.0/total or 100
306 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
307 % (pct, count/1024, total/1024, fcount, ftotal))
309 while len(parts) > 1:
310 _pop(force_tree = None)
311 assert(len(shalists) == 1)
312 tree = w.new_tree(shalists[-1])
314 print tree.encode('hex')
315 if opt.commit or opt.name:
316 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
317 commit = w.new_commit(oldref, tree, date, msg)
319 print commit.encode('hex')
321 w.close() # must close before we can update the ref
325 cli.update_ref(refname, commit, oldref)
327 git.update_ref(refname, commit, oldref)
333 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))