Skip to content

Commit

Permalink
Add option to limit to path
Browse files Browse the repository at this point in the history
  • Loading branch information
bulletmark committed Sep 2, 2018
1 parent 510b45a commit 42e25bd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
15 changes: 10 additions & 5 deletions README.md
Expand Up @@ -72,7 +72,8 @@ All empty directories in the target tree are deleted.
#### B2RESTORE COMMAND LINE OPTIONS

```
usage: b2restore [-h] [-t TIME | -f FILETIME] indir outdir
usage: b2restore [-h] [-t TIME | -f FILETIME] [-s] [-g] [-p PATH]
indir [outdir]
Program to recreate Backblaze B2 file archive at specified date and time.
Expand All @@ -86,6 +87,9 @@ optional arguments:
-t TIME, --time TIME set time YYYY-MM-DDTHH:MM.SS, default=latest
-f FILETIME, --filetime FILETIME
set time based on specified file
-s, --summary just print a summary of files and versions
-g, --gitkeep preserve any top level git dir in outdir
-p PATH, --path PATH only process files under given path
```

### CREATION OF GIT REPOSITORY OF ALL SNAPSHOTS
Expand All @@ -112,9 +116,10 @@ Usage: b2restore-create-git [-options] indir outdir
Create git repository from given B2 rclone copy.
Options:
-t YYYY-MM-DDTHH:MM.SS (start git repo from given time)
-e YYYY-MM-DDTHH:MM.SS (end git repo before given time)
-p (only process files under given path)
```


### TEST RUN UTILITY

A command line utility `b2restore-create-dummy-files` is included to
Expand All @@ -136,7 +141,7 @@ an onerous huge download of your entire B2 archive.
Here is an example usage:

```
rclone lsl --b2-versions B2:mybucket | b2restore-create-dummy-files -d allfiles
rclone lsl --b2-versions B2:mybucket | b2restore-create-dummy-files allfiles
b2restore allfiles b2
du -shl b2 # (see how much storage tree of latest versions uses)
b2restore -t 2018-05-10T12:00.00 allfiles b2
Expand All @@ -146,13 +151,13 @@ du -shl b2 # (see how much storage tree of yesterdays versions uses)
#### B2RESTORE-CREATE-DUMMY-FILES COMMAND LINE OPTIONS

```
Usage: b2restore-create-dummy-files [-options]
Usage: b2restore-create-dummy-files [-options] outdir
Reads B2 file list (from lsl output) from standard input to create
dummy tree of files.
Options:
-d <outdir> default = current dir
-z (zero fill files, not with random content which is default)
-s (set files to zero length, not their actual size)
-p (only process files under given path)
```

### LICENSE
Expand Down
27 changes: 16 additions & 11 deletions b2restore-create-dummy-files
Expand Up @@ -4,41 +4,46 @@
# M.Blakeney, May 2018.

usage() {
echo "Usage: $(basename $0) [-options]"
echo "Usage: $(basename $0) [-options] outdir"
echo "Reads B2 file list (from lsl output) from standard input to create"
echo "dummy tree of files."
echo "Options:"
echo "-d <outdir> default = current dir"
echo "-z (zero fill files, not with random content which is default)"
echo "-s (set files to zero length, not their actual size)"
echo "-p (only process files under given path)"
exit 1
}

OUTDIR=""
NOSIZE=0
ZERO=""
while getopts d:zs c; do
SUBPATH=""
while getopts zsp: c; do
case $c in
d) OUTDIR=$OPTARG;;
z) ZERO="-z";;
s) NOSIZE=1;;
p) SUBPATH=$OPTARG;;
\?) usage;;
esac
done

shift $((OPTIND - 1))

if [[ $# -ne 0 ]]; then
if [[ $# -ne 1 ]]; then
usage
fi

if [[ -n $OUTDIR ]]; then
OUTDIR=$OUTDIR/
fi
OUTDIR=${1%/}

while read size date time file; do
mkdir -p "$OUTDIR""$(dirname $file)"
OUTFILE="$OUTDIR""$file"
# Filter out all but required paths if specified ..
if [[ -n $SUBPATH ]]; then
if ! echo $file | grep -q "^$SUBPATH"; then
continue
fi
fi

mkdir -p "$OUTDIR"/"$(dirname $file)"
OUTFILE="$OUTDIR"/"$file"
if [[ $NOSIZE -eq 0 ]]; then
echo "creating ($size bytes):" "$OUTFILE"
fallocate $ZERO -l $size "$OUTFILE"
Expand Down
15 changes: 12 additions & 3 deletions b2restore-create-git
Expand Up @@ -7,13 +7,19 @@ usage() {
echo "Create git repository from given B2 rclone copy."
echo "Options:"
echo "-t YYYY-MM-DDTHH:MM.SS (start git repo from given time)"
echo "-e YYYY-MM-DDTHH:MM.SS (end git repo before given time)"
echo "-p (only process files under given path)"
exit 1
}

STRTTIME=""
while getopts t: c; do
ENDTIME=""
SUBPATH=""
while getopts t:e:p: c; do
case $c in
t) STRTTIME=$OPTARG;;
e) ENDTIME=$OPTARG;;
p) SUBPATH="-p $OPTARG";;
\?) usage;;
esac
done
Expand Down Expand Up @@ -43,12 +49,15 @@ if ! git init .; then
exit 1
fi

$MYPROG -s "$INDIR" | sed -n '/^ \+/s/^ *//p' | sort -u | while read t b; do
$MYPROG $SUBPATH -s "$INDIR" | sed -n '/^ \+/s/^ *//p' | sort -u | while read t b; do
if [[ -n "$STRTTIME" && "$t" < "$STRTTIME" ]]; then
continue
fi
if [[ -n "$ENDTIME" && ! "$t" < "$ENDTIME" ]]; then
break
fi
echo "=== Creating tree at $t ==="
$MYPROG -g -t "$t" "$INDIR" .
$MYPROG $SUBPATH -g -t "$t" "$INDIR" .
git add .
git commit -m "Snapshot of files at $t" --date="$t"
done
22 changes: 14 additions & 8 deletions b2restore.py
Expand Up @@ -65,23 +65,27 @@ def __init__(self, path, subpath):
self.name = match.group(1) + match.group(3)
self.version = time.mktime(fver)

def parsefile(path):
def parsefile(args, path):
'Parse given file'
fver = FileVersion(path, path.relative_to(indir))
subpath = path.relative_to(indir)
if args.path and not str(subpath).startswith(args.path):
return

fver = FileVersion(path, subpath)
fname = FileName.namemap.get(fver.name)
if not fname:
fname = FileName(fver.name)

# Add this file instance into the list of versions
fname.add(fver)

def parsedir(dirpath, func):
def parsedir(args, dirpath, func):
'Parse given dir and apply func() to files found'
for f in dirpath.iterdir():
if f.is_dir():
parsedir(f, func)
parsedir(args, f, func)
else:
func(f)
func(args, f)

# Keep valid file list
validfiles = set()
Expand All @@ -105,7 +109,7 @@ def addfile(fp, infile, outfile):
print('{} {}: {}'.format(action, fmttime(fp.time), fp.name))
os.link(infile, outfile)

def delfile(path):
def delfile(args, path):
'Delete given file if not needed anymore'
ipath = path.relative_to(outdir)
if ipath.parts[0] in exgit:
Expand All @@ -129,6 +133,8 @@ def main():
help='just print a summary of files and versions')
opt.add_argument('-g', '--gitkeep', action='store_true',
help='preserve any top level git dir in outdir')
opt.add_argument('-p', '--path',
help='only process files under given path')
opt.add_argument('indir',
help='input B2 archive containing all file versions '
' (from --b2-versions)')
Expand Down Expand Up @@ -175,7 +181,7 @@ def main():
argstime = None

# Parse all files in the versioned indir
parsedir(indir, parsefile)
parsedir(args, indir, parsefile)

if args.summary:
fnames = sorted(FileName.namemap)
Expand Down Expand Up @@ -206,7 +212,7 @@ def main():
addfile(fp, indir / fp.path, outdir / fname.name)

# Delete any leftover files
parsedir(outdir, delfile)
parsedir(args, outdir, delfile)

# Delete all leftover empty dirs
for root, dirs, files in os.walk(outdir, topdown=False):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -13,7 +13,7 @@

setup(
name=name,
version='1.9.1',
version='1.9.2',
description='Program to recreate Backblaze B2 file archive at'
'specified date+time',
long_description=readme,
Expand Down

0 comments on commit 42e25bd

Please sign in to comment.