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

sync: Add transaction support #80

Open
wants to merge 1 commit into
base: develop
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ your _.bitpocket/config_ file:

# REMOTE_MOUNTPOINT=/

Changes received in the pull and sent in the push phase are grouped as part of
a transaction. That is, the changes are placed in a staging area until the
completion of the phase and then moved into place. This has the effect of
maintaining consistency of the files across intermittent network connections.
This is enabled by default, but requires some overhead of extra space on both
the souce and destination systems to allow for the staging of all the changes.

# TRANSACTIONAL=true

## Author

* Marcin Kulik | @sickill | https://github.com/sickill | http://ku1ik.com/
Expand Down
41 changes: 39 additions & 2 deletions bin/bitpocket
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CFG_FILE="$DOT_DIR/config"
TMP_DIR="$DOT_DIR/tmp"
STATE_DIR="$DOT_DIR/state"
LOCK_DIR="$TMP_DIR/lock" # Use a lock directory for atomic locks. See the Bash FAQ http://mywiki.wooledge.org/BashFAQ/045
PARTIAL_DIR="$DOT_DIR/staging"

# Default settings
SLOW_SYNC_TIME=10
Expand All @@ -22,6 +23,7 @@ REMOTE_BACKUPS=false
BACKUPS=true
LOCAL_MOUNTPOINT=false
REMOTE_MOUNTPOINT=false
TRANSACTIONAL=true

# Default command-line options and such
COMMANDS=()
Expand Down Expand Up @@ -127,6 +129,8 @@ function init {
exit 128
fi

REMOTE_PATH="$($REMOTE_RUNNER "cd '$REMOTE_PATH'; pwd -P")"

mkdir "$DOT_DIR"

cat <<EOF > "$CFG_FILE"
Expand Down Expand Up @@ -162,6 +166,11 @@ REMOTE_BACKUPS=false
## result in all local or remote data disappearing. Give the expected
## mountpoint of the local and/or remote target.
# REMOTE_MOUNTPOINT=/

## Perform the transfer as a transaction. If interrupted, no updates are made.
# Partial file transfers remain to speed up future syncs. This should be
# combined with the "clean" command to automatically clean up ab
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. Is there going to be a "clean" command?

# TRANSACTIONAL=true
EOF

echo "Initialized bitpocket directory at $(pwd)"
Expand Down Expand Up @@ -189,13 +198,20 @@ function pull() {

local BACKUP_TARGET="$DOT_DIR/backups/$TIMESTAMP"
local DO_BACKUP=""
local DO_TRANSACTION=""

if [[ $BACKUPS == true ]]
then
echo "# >> Saving current state and backing up files (if needed)"
local DO_BACKUP="--backup --backup-dir=$BACKUP_TARGET"
fi

if [[ $TRANSACTIONAL == true ]]
then
echo "# >> Syncing as a transaction"
DO_TRANSACTION="--partial --temp-dir=$PARTIAL_DIR --delay-updates --delete-delay"
fi

cp "$STATE_DIR/tree-current" "$TMP_DIR/tree-after"

# Determine what will be fetched from server and make backup copies of any
Expand All @@ -215,7 +231,7 @@ function pull() {
--exclude-from="$TMP_DIR/local-add-change" \
--filter=". -" \
--filter="P **" \
$DO_BACKUP \
$DO_BACKUP $DO_TRANSACTION \
--out-format=%i:%B:%n \
$RSYNC_OPTS $USER_RULES $REMOTE/ . \
| detect_changes \
Expand Down Expand Up @@ -271,19 +287,32 @@ function push() {

local BACKUP_TARGET="$DOT_DIR/backups/$TIMESTAMP"
local DO_BACKUP=""
local DO_TRANSACTION=""

if [[ $REMOTE_BACKUPS == true ]]
then
echo "# >> Saving current state and backing up files (if needed)"
DO_BACKUP="--backup --backup-dir=$BACKUP_TARGET"
fi

if [[ $TRANSACTIONAL == true ]]
then
echo "# >> Syncing as a transaction"
$REMOTE_RUNNER "
cd '$REMOTE_PATH'
[[ -d '$PARTIAL_DIR' ]] || mkdir -p '$PARTIAL_DIR'
chmod 0700 '$PARTIAL_DIR' \
|| die 'Cannot set proper permissions on PARTIAL_DIR'"
DO_TRANSACTION="--partial --temp-dir=$PARTIAL_DIR --delay-updates --delete-delay"
fi

# Do not push back remotely deleted files
prefix "R " < "$TMP_DIR/local-del" \
| rsync -auzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" \
--exclude-from="$TMP_DIR/remote-del" \
--filter=". -" \
--filter="P **" \
$DO_BACKUP \
$DO_BACKUP $DO_TRANSACTION \
$USER_RULES . $REMOTE/ \
| prefix " | " || die "PUSH"

Expand Down Expand Up @@ -655,6 +684,14 @@ function assert_dotdir {
fi
mkdir -p "$TMP_DIR"
mkdir -p "$STATE_DIR"

if [[ $TRANSACTIONAL ]]
then
[[ -d "$PARTIAL_DIR" ]] || mkdir -p "$PARTIAL_DIR"
chmod 0700 "$PARTIAL_DIR" \
|| die "Cannot set proper permissions on PARTIAL_DIR"
fi

}

function detect_fatfs {
Expand Down