Ticket #41734: patch-bin-bitpocket.diff
File patch-bin-bitpocket.diff, 9.2 KB (added by cedric.simillion@…, 11 years ago) |
---|
-
bin/bitpocket
old new 18 18 [ -f "$CFG_FILE" ] && . "$CFG_FILE" 19 19 20 20 # Test for GNU versions of core utils. Bail if non-GNU. 21 sed --version >/dev/null 2>/dev/null21 gsed --version >/dev/null 2>/dev/null 22 22 if [ $? -ne 0 ]; then 23 23 echo "fatal: It seems like you are running non-GNU versions of coreutils." 24 24 echo " It is currently unsafe to use bitpocket with this setup," … … 70 70 exit 128 71 71 fi 72 72 73 mkdir "$DOT_DIR"73 gmkdir "$DOT_DIR" 74 74 75 cat <<EOF > "$CFG_FILE"75 gcat <<EOF > "$CFG_FILE" 76 76 ## Host and path of central storage 77 77 REMOTE_HOST=$1 78 78 REMOTE_PATH="$2" … … 89 89 # SLOW_SYNC_STOP_CMD="notify-send 'BitPocket sync finished'" 90 90 EOF 91 91 92 echo "Initialized bitpocket directory at ` pwd`"92 echo "Initialized bitpocket directory at `gpwd`" 93 93 echo "Please have a look at the config file ($DOT_DIR/config)" 94 94 } 95 95 … … 112 112 on_slow_sync_start 113 113 114 114 # Check what has changed 115 touch "$STATE_DIR/tree-prev"116 touch "$STATE_DIR/added-prev"115 gtouch "$STATE_DIR/tree-prev" 116 gtouch "$STATE_DIR/added-prev" 117 117 118 118 # Save before-sync state 119 119 # Must be done with rsync itself (rather than find) to respect includes/excludes 120 120 # Order of includes/excludes/filters is EXTREMELY important 121 121 echo "# Saving current state and backing up files (if needed)" 122 echo " | Root dir: $( pwd)"122 echo " | Root dir: $(gpwd)" 123 123 rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \ 124 | sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" |sort > "$STATE_DIR/tree-current"124 | gsed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | gsed "s:^/\.::" | gsort > "$STATE_DIR/tree-current" 125 125 126 126 # Prevent bringing back locally deleted files or removing new local files 127 cp -f "$STATE_DIR/added-prev" "$TMP_DIR/fetch-exclude"128 sort "$STATE_DIR/tree-prev" "$STATE_DIR/tree-current" |uniq -u >> "$TMP_DIR/fetch-exclude"127 gcp -f "$STATE_DIR/added-prev" "$TMP_DIR/fetch-exclude" 128 gsort "$STATE_DIR/tree-prev" "$STATE_DIR/tree-current" | guniq -u >> "$TMP_DIR/fetch-exclude" 129 129 130 130 # It is difficult to only create the backup directory if needed; instead 131 131 # we always create it, but remove it if it is empty afterwards. 132 mkdir --parents $DOT_DIR/backups/$TIMESTAMP132 gmkdir --parents $DOT_DIR/backups/$TIMESTAMP 133 133 134 134 # Determine what will be fetched from server and make backup 135 135 # copies of any local files to be deleted or overwritten. 136 136 # Order of includes/excludes/filters is EXTREMELY important 137 137 rsync --dry-run \ 138 138 -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . \ 139 | grep "^[ch<>\.\*][f]\|\*deleting" | sed "s:^\S*\s*::" | sed "s:\(.*\):if [ -f \"\1\" ]; thencp --parents \"\1\" $DOT_DIR/backups/$TIMESTAMP; fi:" | sh || die "BACKUP"140 [ "$( ls -A $DOT_DIR/backups/$TIMESTAMP)" ] && echo " | Some files were backed up to $DOT_DIR/backups/$TIMESTAMP"141 [ "$( ls -A $DOT_DIR/backups/$TIMESTAMP)" ] ||rmdir $DOT_DIR/backups/$TIMESTAMP139 | grep "^[ch<>\.\*][f]\|\*deleting" | gsed "s:^\S*\s*::" | gsed "s:\(.*\):if [ -f \"\1\" ]; then gcp --parents \"\1\" $DOT_DIR/backups/$TIMESTAMP; fi:" | sh || die "BACKUP" 140 [ "$(gls -A $DOT_DIR/backups/$TIMESTAMP)" ] && echo " | Some files were backed up to $DOT_DIR/backups/$TIMESTAMP" 141 [ "$(gls -A $DOT_DIR/backups/$TIMESTAMP)" ] || grmdir $DOT_DIR/backups/$TIMESTAMP 142 142 143 143 # Actual fetch 144 144 # Pulling changes from server 145 145 # Order of includes/excludes/filters is EXTREMELY important 146 146 echo 147 147 echo "# Pulling changes from server" 148 rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . | sed "s/^/ | /" || die "PULL"148 rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" --exclude-from "$TMP_DIR/fetch-exclude" $USER_RULES $REMOTE/ . | gsed "s/^/ | /" || die "PULL" 149 149 150 150 # Actual push 151 151 # Send new and updated, remotely remove files deleted locally 152 152 # Order of includes/excludes/filters is EXTREMELY important 153 153 echo 154 154 echo "# Pushing changes to server" 155 rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" $USER_RULES . $REMOTE/ | sed "s/^/ | /" || die "PUSH"155 rsync -auvzxi --delete $RSYNC_OPTS --exclude "/$DOT_DIR" $USER_RULES . $REMOTE/ | gsed "s/^/ | /" || die "PUSH" 156 156 157 157 # Save after-sync state 158 158 # Must be done with rsync itself (rather than find) to respect includes/excludes … … 160 160 echo 161 161 echo "# Saving after-sync state and cleaning up" 162 162 rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \ 163 | sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" |sort > "$TMP_DIR/tree-after"163 | gsed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | gsed "s:^/\.::" | gsort > "$TMP_DIR/tree-after" 164 164 165 165 # Save all newly created files for next run (to prevent deletion of them) 166 166 # This includes files created by user in parallel to sync and files fetched from remote 167 167 comm -23 "$TMP_DIR/tree-after" "$STATE_DIR/tree-current" >"$STATE_DIR/added-prev" 168 168 169 169 # Save new tree state for next run 170 cat "$STATE_DIR/tree-current" "$STATE_DIR/added-prev" >"$STATE_DIR/tree-prev"170 gcat "$STATE_DIR/tree-current" "$STATE_DIR/added-prev" >"$STATE_DIR/tree-prev" 171 171 172 172 # Fire off slow sync stop notifier in background 173 173 on_slow_sync_stop … … 193 193 # If pack directory is missing, create it and prepare git repo 194 194 if [ ! -d "$DOT_DIR/pack" ] 195 195 then 196 mkdir $DOT_DIR/pack196 gmkdir $DOT_DIR/pack 197 197 git init $DOT_DIR/pack 198 touch $DOT_DIR/pack/.git-init-marker198 gtouch $DOT_DIR/pack/.git-init-marker 199 199 (cd $DOT_DIR/pack && git add .) 200 200 (cd $DOT_DIR/pack && git commit -a -q -m "INIT") 201 201 fi 202 202 203 203 # If any backups exist, pack them into the repo 204 if [ -d "$DOT_DIR/backups" ] && [ "$( ls -A $DOT_DIR/backups)" ]204 if [ -d "$DOT_DIR/backups" ] && [ "$(gls -A $DOT_DIR/backups)" ] 205 205 then 206 206 for DIR in $DOT_DIR/backups/* 207 207 do 208 TSTAMP=$(echo $DIR | sed "s|.*/||")209 if [ "$( ls -A $DIR)" ]208 TSTAMP=$(echo $DIR | gsed "s|.*/||") 209 if [ "$(gls -A $DIR)" ] 210 210 then 211 211 echo -n "Processing: $TSTAMP ... " 212 212 echo -n "Moving ... " 213 ( cp -rfl $DIR/* $DOT_DIR/pack &&rm -rf $DIR) || die MV213 (gcp -rfl $DIR/* $DOT_DIR/pack && grm -rf $DIR) || die MV 214 214 echo -n "Adding ... " 215 215 (cd $DOT_DIR/pack && git add .) || die ADD 216 216 echo -n "Committing ... " … … 221 221 echo "Done." 222 222 else 223 223 echo "Removing empty dir $DIR ..." 224 rmdir $DIR224 grmdir $DIR 225 225 fi 226 226 done 227 227 echo "Running 'git gc' on pack dir" … … 237 237 238 238 function on_slow_sync_start { 239 239 if [ -n "$SLOW_SYNC_START_CMD" ]; then 240 rm -rf "$SLOW_SYNC_FILE"241 (sleep $SLOW_SYNC_TIME && touch "$SLOW_SYNC_FILE" && eval "$SLOW_SYNC_START_CMD" ; wait) &240 grm -rf "$SLOW_SYNC_FILE" 241 (sleep $SLOW_SYNC_TIME && gtouch "$SLOW_SYNC_FILE" && eval "$SLOW_SYNC_START_CMD" ; wait) & 242 242 disown 243 243 shell_pid=$! 244 244 fi … … 246 246 247 247 function on_slow_sync_stop { 248 248 if [ -n "$shell_pid" ]; then 249 kill $shell_pid &>/dev/null249 gkill $shell_pid &>/dev/null 250 250 251 251 if [[ -n "$SLOW_SYNC_STOP_CMD" && -f "$SLOW_SYNC_FILE" ]]; then 252 252 (eval "$SLOW_SYNC_STOP_CMD") & … … 267 267 268 268 function acquire_lock { 269 269 if ! mkdir "$LOCK_DIR" 2>/dev/null ; then 270 kill -0 $(cat "$LOCK_DIR/pid") &>/dev/null270 gkill -0 $(gcat "$LOCK_DIR/pid") &>/dev/null 271 271 272 272 if [[ $? == 0 ]]; then 273 273 echo "There's already an instance of BitPocket syncing this directory. Exiting." 274 274 exit 1 275 275 else 276 276 echo -e "\x1b\x5b1;31mbitpocket error:\x1b\x5b0m Bitpocket found a stale lock directory:" 277 echo " | Root dir: $( pwd)"277 echo " | Root dir: $(gpwd)" 278 278 echo " | Lock dir: $LOCK_DIR" 279 echo " | Command: LOCK_PATH=$( pwd)/$LOCK_DIR && rm \$LOCK_PATH/pid &&rmdir \$LOCK_PATH"279 echo " | Command: LOCK_PATH=$(gpwd)/$LOCK_DIR && grm \$LOCK_PATH/pid && grmdir \$LOCK_PATH" 280 280 echo "Please remove the lock directory and try again." 281 281 exit 2 282 282 fi … … 286 286 } 287 287 288 288 function release_lock { 289 rm "$LOCK_DIR/pid" &>/dev/null && rmdir "$LOCK_DIR" &>/dev/null289 rm "$LOCK_DIR/pid" &>/dev/null && grmdir "$LOCK_DIR" &>/dev/null 290 290 } 291 291 292 292 function acquire_remote_lock { … … 308 308 echo "fatal: Not a bitpocket directory. Try 'bitpocket help' for usage." 309 309 exit 128 310 310 fi 311 mkdir -p "$TMP_DIR"312 mkdir -p "$STATE_DIR"311 gmkdir -p "$TMP_DIR" 312 gmkdir -p "$STATE_DIR" 313 313 } 314 314 315 315 function cleanup { … … 320 320 function bring_the_children_let_me_kill_them { 321 321 if [ -n "$shell_pid" ]; then 322 322 pkill -P $shell_pid &>/dev/null 323 kill $shell_pid &>/dev/null323 gkill $shell_pid &>/dev/null 324 324 fi 325 325 } 326 326 … … 336 336 function list { 337 337 echo -e "\x1b\x5b1;32mbitpocket\x1b\x5b0m will sync the following files:" 338 338 rsync -av --list-only --exclude "/$DOT_DIR" $USER_RULES . | grep "^-\|^d" \ 339 | sed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | sed "s:^/\.::" |sort339 | gsed "s:^\S*\s*\S*\s*\S*\s*\S*\s*:/:" | gsed "s:^/\.::" | gsort 340 340 } 341 341 342 342 function usage {