Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying only a push and only a pull from a remote server. #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions bin/bitpocket
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ fi
# Specify certain files to include
if [ -f "$DOT_DIR/include" ]; then
user_include="--include-from $DOT_DIR/include"
fi
fi

# Specify rsync filter rules
if [ -f "$DOT_DIR/filter" ]; then
# The underscore (_) is required for correct operation
user_filter="--filter merge_$DOT_DIR/filter"
fi
fi

USER_RULES="$user_filter $user_include $user_exclude"

TIMESTAMP=$(date "+%Y-%m-%d.%H%M%S")

export RSYNC_RSH
Expand Down Expand Up @@ -98,6 +98,14 @@ function log {
tail -f "$DOT_DIR/log"
}

function pull {
sync onlypull
}

function push {
sync onlypush
}

# Do the actual synchronization
function sync {
assert_dotdir
Expand All @@ -119,7 +127,7 @@ function sync {
# Must be done with rsync itself (rather than find) to respect includes/excludes
# Order of includes/excludes/filters is EXTREMELY important
echo "# Saving current state and backing up files (if needed)"
echo " | Root dir: $(pwd)"
echo " | Root dir: $(pwd)"
rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \
| sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort > "$STATE_DIR/tree-current"

Expand All @@ -131,7 +139,7 @@ function sync {
# we always create it, but remove it if it is empty afterwards.
mkdir --parents $DOT_DIR/backups/$TIMESTAMP

# Determine what will be fetched from server and make backup
# Determine what will be fetched from server and make backup
# copies of any local files to be deleted or overwritten.
# Order of includes/excludes/filters is EXTREMELY important
rsync --dry-run \
Expand All @@ -140,19 +148,29 @@ function sync {
[ "$(ls -A $DOT_DIR/backups/$TIMESTAMP)" ] && echo " | Some files were backed up to $DOT_DIR/backups/$TIMESTAMP"
[ "$(ls -A $DOT_DIR/backups/$TIMESTAMP)" ] || rmdir $DOT_DIR/backups/$TIMESTAMP

# Actual fetch
# Pulling changes from server
# Order of includes/excludes/filters is EXTREMELY important
echo
echo "# Pulling changes from server"
rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . | sed "s/^/ | /" || die "PULL"
if [ "$1" != "onlypush" ]
then

# Actual push
# Send new and updated, remotely remove files deleted locally
# Order of includes/excludes/filters is EXTREMELY important
echo
echo "# Pushing changes to server"
rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" $USER_RULES . $REMOTE/ | sed "s/^/ | /" || die "PUSH"
# Actual fetch
# Pulling changes from server
# Order of includes/excludes/filters is EXTREMELY important
echo
echo "# Pulling changes from server"
rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . | sed "s/^/ | /" || die "PULL"

fi

if [ "$1" != "onlypull" ]
then

# Actual push
# Send new and updated, remotely remove files deleted locally
# Order of includes/excludes/filters is EXTREMELY important
echo
echo "# Pushing changes to server"
rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" $USER_RULES . $REMOTE/ | sed "s/^/ | /" || die "PUSH"

fi

# Save after-sync state
# Must be done with rsync itself (rather than find) to respect includes/excludes
Expand All @@ -162,6 +180,7 @@ function sync {
rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \
| sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort > "$TMP_DIR/tree-after"


# Save all newly created files for next run (to prevent deletion of them)
# This includes files created by user in parallel to sync and files fetched from remote
comm -23 "$TMP_DIR/tree-after" "$STATE_DIR/tree-current" >"$STATE_DIR/added-prev"
Expand All @@ -185,7 +204,7 @@ function pack {
assert_dotdir

# Git is required for backup packing
if [ ! `builtin type -p git` ]; then
if [ ! `builtin type -p git` ]; then
echo "fatal: For backup packing, git must be installed"
exit 128
fi
Expand All @@ -196,7 +215,7 @@ function pack {
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 add .)
(cd $DOT_DIR/pack && git commit -a -q -m "INIT")
fi

Expand All @@ -206,11 +225,11 @@ function pack {
for DIR in $DOT_DIR/backups/*
do
TSTAMP=$(echo $DIR | sed "s|.*/||")
if [ "$(ls -A $DIR)" ]
if [ "$(ls -A $DIR)" ]
then
echo -n "Processing: $TSTAMP ... "
echo -n "Moving ... "
(cp -rfl $DIR/* $DOT_DIR/pack && rm -rf $DIR) || die MV
(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 ... "
Expand Down Expand Up @@ -274,8 +293,8 @@ function acquire_lock {
exit 1
else
echo -e "\x1b\x5b1;31mbitpocket error:\x1b\x5b0m Bitpocket found a stale lock directory:"
echo " | Root dir: $(pwd)"
echo " | Lock dir: $LOCK_DIR"
echo " | Root dir: $(pwd)"
echo " | Lock dir: $LOCK_DIR"
echo " | Command: LOCK_PATH=$(pwd)/$LOCK_DIR && rm \$LOCK_PATH/pid && rmdir \$LOCK_PATH"
echo "Please remove the lock directory and try again."
exit 2
Expand Down Expand Up @@ -336,7 +355,7 @@ function die {
function list {
echo -e "\x1b\x5b1;32mbitpocket\x1b\x5b0m will sync the following files:"
rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \
| sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort
| sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" | sort
}

function usage {
Expand All @@ -360,6 +379,10 @@ function usage {
if [ "$1" = "init" ]; then
# Initialize bitpocket directory
init $2 $3 $4
elif [ "$1" = "pull" ]; then
sync onlypull
elif [ "$1" = "push" ]; then
sync onlypush
elif [ "$1" = "pack" ]; then
# Pack backups using git
pack
Expand All @@ -371,7 +394,7 @@ elif [ "$1" = "cron" ]; then
cron
elif [ "$1" = "list" ]; then
# List all file in sync set (honoring .bitpocket/include & .bitpocket/exclude)
list
list
elif [ "$1" != "" ] && [ "$1" != "sync" ]; then
# Show help
usage
Expand Down
14 changes: 14 additions & 0 deletions demos/push_if_clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if [ -n "$(git status --porcelain)" ]; then
echo "repo is not clean. please cleanup first.";
else
echo "repo is clean, pulling"
bitpocket pull

if [ -n "$(git status --porcelain)" ]; then
echo "there are new files from the server. please cleanup first.";
else
bitpocket push
echo "successfully pushed";
fi
fi