(18-06-2016, 06:19 AM)venam Wrote: some rsync commands
For what I need, rsync is an overkill, and needs to be installed/compiled on the targeted platform.
With a shell script, I can keep it along with my data on my USB key, and run it on any UNIX.
Code:
lsync(1) Mirroring utility lsync(1)
NAME
LSync - Mirror two local dirs keeping the latest version
SYNTAX
lsync [-v] [-e PATTERN] DIR1 DIR2
DESCRIPTION
-e Exclude PATTERN from the search, matching the whole path.
-v View the output without copying anything.
DIR The two directories to mirror.
USAGE
LSync mirrors data across two directories so that both contains all the
files from each other keeping the timestapms.
When a file exists in both directories, if they do not have the same
timestamps, the most recent file overwrite the other.
There is no deletion performed.
Example of output it produces on screen:
[EDIT]: That way, I can grep the output for '>>>' or ' > ' to see what is overwritten or copied from left to right, '>' to see whatever comes from left to right...
It only contains these commands + shell builtins: <code>
mkdir find cut -c tee sed sort uniq tee grep -v cp -p cp -pf
</code>
But I would not recommand to use it in production, as I am a beginner, and there may be many flaws to it.
[ $# -lt 2 ] || [ $# -gt 5 ] && printf '%s\n' '
NAME
LSync - Mirror two local dirs keeping the latest version
SYNTAX
lsync [-v] [-e PATTERN] DIR1 DIR2
DESCRIPTION
-e Exclude PATTERN from the search, matching the whole path.
-v View the output without copying anything.
DIR The two directories to mirror.
USAGE
LSync mirrors data across two directories so that both contains all the
files from each other keeping the timestapms.
When a file exists in both directories, if they do not have the same
timestamps, the most recent file overwrite the other.
There is no deletion performed.
' && exit 0
while [ $# -gt 0 ]
do case "$1" in
'-e' ) p="$2"; shift ;;
'-v' ) v=1 ;;
* ) [ -z "$dir1" ] && dir1="$1" || dir2="$1" ;;
esac
shift
done
copied=0; overwritten=0; directories=0; identical=0
# Generate a path list for all the files from both directories
{
find "$dir1" | cut -c $((${#dir1} + 1))-
find "$dir2" | cut -c $((${#dir2} + 1))-
} | sed 's/^\///' | sort | uniq | {
[ -z "$p" ] && tee || grep -v "$p"
} | {
while read path
do
# Create directories
if [ -d "$dir1/$path" ] || [ -d "$dir2/$path" ]
then [ -z "$v" ] && mkdir -p "$dir1/$path" "$dir2/$path"
printf '\033[1;30m %s\033[0m\n' "$path"
directories=$(($directories + 1))
# Copy files that does not exist on one side
elif [ ! -e "$dir2/$path" ]
then [ -z "$v" ] && cp -p "$dir1/$path" "$dir2/$path"
printf '\033[1;32m1 > 2\033[0m %s\n' "$path"
copied=$(($copied + 1))