Skip to content

Commit

Permalink
Allow packing of previous backups into a git repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
torfason committed Jan 21, 2013
1 parent 06f56e6 commit b7f82cb
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions bin/bitpocket
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,65 @@ function sync {
cleanup
}


# Pack backups into a git repository
function pack {
assert_dotdir

# Git is required for backup packing
if [ ! `builtin type -p git` ]; then
echo "fatal: For backup packing, git must be installed"
exit 128
fi

# If pack directory is missing, create it and prepare git repo
if [ ! -d "$DOT_DIR/pack" ]
then
mkdir $DOT_DIR/pack
git init $DOT_DIR/pack
touch $DOT_DIR/pack/.git-init-marker
(cd $DOT_DIR/pack && git add .)
(cd $DOT_DIR/pack && git commit -a -q -m "INIT")
fi

# If any backups exist, pack them into the repo
if [ -d "$DOT_DIR/backups" ] && [ "$(ls -A $DOT_DIR/backups)" ]
then
for DIR in $DOT_DIR/backups/*
do
TSTAMP=$(echo $DIR | sed "s|.*/||")
if [ "$(ls -A $DIR)" ]
then
echo -n "Processing: $TSTAMP ... "
echo -n "Moving ... "
(cp -rfl $DIR/* $DOT_DIR/pack && rm -rf $DIR) || die MV
echo -n "Adding ... "
(cd $DOT_DIR/pack && git add .) || die ADD
echo -n "Committing ... "
##(cd $DOT_DIR/pack && git commit -a -m "$TSTAMP") || die COMMIT

This comment has been minimized.

Copy link
@ku1ik

ku1ik Mar 14, 2013

I suppose this also can be removed.

This comment has been minimized.

Copy link
@torfason

torfason Mar 15, 2013

Author Owner

Done.

# Commit only if repository has uncommitted changes
(cd $DOT_DIR/pack \
&& git diff-index --quiet HEAD \
|| git commit -a -q -m "$TSTAMP" ) || die COMMIT
#mv $DIR/* $DOT_DIR/pack || die MV

This comment has been minimized.

Copy link
@ku1ik

ku1ik Mar 14, 2013

And these 2 (above and below). Btw, it doesn't remove the dir after a successful commit, right? I wonder what would be the best, removing or not... What's better from your point of view (after using it for 2 months)?

This comment has been minimized.

Copy link
@torfason

torfason Mar 15, 2013

Author Owner

Done.
The git repo dir is never removed, but the timestamped directories are in fact removed after their contents are merged into the git repo. The removal happens in line 191:
(cp -rfl $DIR/* $DOT_DIR/pack && rm -rf $DIR)

There is a lot of magic/complexity/whatever in the "cp -rfl && rm" command. It copies the contents of the timestamped dir into the git repo before removing the timestamped dir completely. A move would not work here because we need to merge it (don't want to replace all the old files in the repo) and mv just won't do that, but because of the -l parameter, this occurs through hard links, so it is (almost) as efficient as a move, even if the files are large. I say magic because I needed to learn and research quite a bit before finding this out. But it was an itch I had been dying to scratch for a while.

#rmdir $DIR || die RMDIR
echo "Done."
else
echo "Removing empty dir $DIR ..."
rmdir $DIR
fi
done
echo "Running 'git gc' on pack dir"
du -hs $DOT_DIR/pack
(cd $DOT_DIR/pack && git gc) || die GC
du -hs $DOT_DIR/pack
echo "All snapshotes packed successfully."

This comment has been minimized.

Copy link
@ku1ik

ku1ik Mar 14, 2013

snapshotes -> snapshots

This comment has been minimized.

Copy link
@torfason

torfason Mar 15, 2013

Author Owner

Done.

else
echo "No unpacked backups found ..."
fi

}

function on_slow_sync_start {
if [ -n "$SLOW_SYNC_START_CMD" ]; then
rm -rf "$SLOW_SYNC_FILE"
Expand Down Expand Up @@ -253,6 +312,9 @@ function die {

if [ "$1" = "init" ]; then
init $2 $3 $4
elif [ "$1" = "pack" ]; then
# Pack backups using git
pack
elif [ "$1" = "log" ]; then
log
elif [ "$1" = "cron" ]; then
Expand Down

0 comments on commit b7f82cb

Please sign in to comment.