-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
197 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# bitpocket | ||
|
||
## About | ||
|
||
**bitpocket** is a small but smart script that does 2-way directory | ||
synchronization. It uses _rsync_ to do efficient data transfer and tracks local | ||
file creation/removal to avoid known rsync problem when doing 2-way syncing | ||
with deletion. | ||
|
||
## Installation | ||
|
||
Download script and place it in a directory in your $PATH: | ||
|
||
$ curl -sL https://raw.github.com/sickill/bitpocket/master/bin/bitpocket > ~/bin/bitpocket | ||
$ chmod +x ~/bin/bitpocket | ||
|
||
|
||
## Setting up master | ||
|
||
Create empty directory on some host that will be the master copy of your files: | ||
|
||
$ ssh example.org | ||
$ mkdir ~/BitPocketMaster | ||
|
||
|
||
## Setting up slaves | ||
|
||
On each machine you want to synchronize initialize empty directory as your bitpocket: | ||
|
||
$ mkdir ~/BitPocket | ||
$ cd ~/BitPocket | ||
$ bitpocket init example.org:~/BitPocketMaster | ||
|
||
|
||
### Manual sync | ||
|
||
Now whenever you want to sync with master just run _bitpocket_ inside your | ||
bitpocket directory: | ||
|
||
$ cd ~/BitPocket | ||
$ bitpocket | ||
|
||
|
||
### Automatic sync with cron | ||
|
||
Add following line to your crontab to run bitpocket as often as desired: | ||
|
||
*/5 * * * * (cd ~/BitPocket; nice ~/bin/bitpocket >>.bitpocket/log) | ||
|
||
Note that cron usually has very limited environment and your ssh keys with | ||
passhrases won't work in cron jobs as ssh-agents/keyrings don't work there. | ||
Thus it's preferable to generate passphrase-less ssh key for bitpocket | ||
authentication: | ||
|
||
$ cd ~/BitPocket | ||
$ ssh-keygen -t rsa -C bitpocket-`hostname` -N '' -f .bitpocket/id_rsa | ||
|
||
and uncomment line with `RSYNC_SSH` in _.bitpocket/config_ file. | ||
|
||
|
||
## Author | ||
|
||
Marcin Kulik / <https://github.com/sickill> / <http://ku1ik.com/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/bin/bash | ||
|
||
export LC_ALL=C # for stable "sort" output | ||
|
||
DOTDIR=.bitpocket | ||
CFG_PATH=$DOTDIR/config | ||
LOCK_PATH=$DOTDIR/lock | ||
|
||
function init { | ||
if [[ -d $DOTDIR || -f $CFG_PATH ]]; then | ||
echo "fatal: Current directory already initialized for bitpocket" | ||
exit 128 | ||
fi | ||
|
||
if [ -z $1 ]; then | ||
echo "usage: bitpocket init <REMOTE>" | ||
echo | ||
echo "<REMOTE> can be local path or host:path" | ||
exit 128 | ||
fi | ||
|
||
mkdir $DOTDIR | ||
|
||
echo "REMOTE=\"$1\"" > $CFG_PATH | ||
echo "# export RSYNC_RSH=\"ssh -i .bitpocket/id_rsa\"" >> $CFG_PATH | ||
|
||
echo "Initialized bitpocket directory at `pwd`" | ||
} | ||
|
||
function save_tree { | ||
find | sort | cut -d '/' -f 2- | grep -v "^\\$DOTDIR" | grep -v '^\.$' > $DOTDIR/$1 | ||
} | ||
|
||
function sync { | ||
if [ ! -d $DOTDIR ]; then | ||
echo "fatal: Not a bitpocket directory" | ||
exit 128 | ||
fi | ||
|
||
[ -f $LOCK_PATH ] && kill -0 $(cat $LOCK_PATH) &>/dev/null && exit 0 | ||
echo $$ > $LOCK_PATH | ||
. $CFG_PATH | ||
|
||
echo -e "\e[1;32mbitpocket\e[0m started at `date`." | ||
echo | ||
|
||
# check what has changed | ||
touch $DOTDIR/tree-prev | ||
save_tree "tree-current" | ||
|
||
# prevent bringing back locally deleted files | ||
comm -23 $DOTDIR/tree-prev $DOTDIR/tree-current >$DOTDIR/fetch-exclude | ||
|
||
# prevent removing new local files | ||
comm -13 $DOTDIR/tree-prev $DOTDIR/tree-current >>$DOTDIR/fetch-exclude | ||
|
||
# fetch everything new and updated, locally remove files deleted on remote | ||
rsync -auvzxi --delete --exclude $DOTDIR --exclude-from $DOTDIR/fetch-exclude $REMOTE/ . || die | ||
|
||
# send new and updated, remotely remove files deleted locally | ||
rsync -auvzxi --delete --exclude $DOTDIR . $REMOTE/ || die | ||
|
||
# prepare for next run | ||
rm $DOTDIR/tree-current | ||
save_tree "tree-prev" | ||
|
||
rm $LOCK_PATH | ||
} | ||
|
||
function die { | ||
rm $LOCK_PATH | ||
echo "fatal: command failed" | ||
exit 128 | ||
} | ||
|
||
if [ "$1" = "init" ]; then | ||
init $2 | ||
else | ||
sync | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters