#!/usr/bin/bash # # This is a simple bash script to convert # the list provided by bd-sync into instructions # for copying and deleting. # # NB: Scripts are generated in two stages - # The copying script itself generates # the deletion script. This is for two # reasons: # 1. Allows a review of what is about # to be deleted, which is generally # irreversible so needs confirmation. # 2. The flow of copying and deleting # is in opposite directions: # Copying: FIRST create main directories # THEN create sub-dirs and files # Delete : FIRST delete sub-dirs and files # THEN delete main directories # Which is why we have 'the # reversing step' below. # # Author: http://the-brown-dragon.com/ ####################################### LATESTLOG="/b_d/e.d/sync/latest" [ -z $1 ] || LATESTLOG="$1" OUTFILE="g.sh" DELFILE="d.sh" SEDFILE=delthis.sed cat > $SEDFILE < $DELFILE/;p;d} s/\\\$/\\\\\$/g /#S\[ource\] : /{ s/#S\[ource\] : // h;d } /#D\[estination\]: /{ s/#D\[estination\]: // H;d } /^#/d /^d/{ s/..// /\*$/{ G s/\(.*\)\n\(.*\)\n\(.*\)/\2\n\3\n\1/ s/\(.*\)\n\(.*\)\n\1\(.*\)/\2\3/ s/\\\$/\\\\\\\\\$/g s/!/\\\\!/g s/\(.*\)\*$/mkdir -p "\1" || echo "Failed to create directory \1"/ } /^\*/{ s/\\\$/\\\\\\\\\$/g s/!/\\\\!/g s/\*\(.*\)/echo rmdir "\\\\"\1\\\\"" >> $DELFILE/ } p;d } s/..// /\*$/{ s/\(.*\)\*$/\1\n\1/ G s/\(.*\)\n\(.*\)\n\(.*\)\n\(.*\)/\3\n\4\n\1\n\2/ s/\(.*\)\n\(.*\)\n\(.*\)\n\1\(.*\)/cp -P "\3" "\2\4" || echo "Failed to copy \3"\ntouch -h -r "\3" "\2\4"/ } /^\*/{ s/\\\$/\\\\\\\\\$/g s/!/\\\\!/g s/\*\(.*\)/echo "rm \\\\"\1\\\\"" >> $DELFILE/ } /..*\*..*/s/\(.*\)\*\(.*\)/cp -P "\1" "\2" || echo "Failed to copy \1"\ntouch -h -r "\1" "\2"/ # The reversing step! Refer main script comment for reason. \$s/.*/&\nsed -i '1!G;h;\$!d;s\/^\/#!\\\\\/usr\\\\\/bin\\\\\/bash\\\\n\/' $DELFILE/ SEDFILEENDS sed -f $SEDFILE $LATESTLOG > $OUTFILE rm $SEDFILE