Users browsing this thread: 1 Guest(s)
josuah
Long time nixers
(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:

[Image: 7SSexFz.png]

[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.

The script: lsync
Code:
#!/bin/sh
#    /\
#   / / ____ __  /\ ____   ____
#  / / / __// / / // __ \ / ___\
# / /__\ \  \ \/ // / / // /__
# \/ \___/ __\  / \/  \/ \____\ - Sync from two local dirs
#==========\__,'================================================================

[ $# -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

[ ! -d "$dir1" ] && mkdir -p "$dir1"
[ ! -d "$dir2" ] && mkdir -p "$dir2"

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))

                elif [ ! -e "$dir1/$path" ]
                then [ -z "$v" ] && cp -p  "$dir2/$path"  "$dir1/$path"
                        printf '\033[1;32m1 < 2\033[0m %s\n' "$path"
                        copied=$(($copied + 1))

                # Overwrite files keeping the latest version
                elif [ "$dir1/$path" -nt "$dir2/$path" ]
                then [ -z "$v" ] && cp -pf "$dir1/$path"  "$dir2/$path"
                        printf '\033[1;31m1>>>2\033[0m %s\n' "$path"
                        overwritten=$(($overwritten + 1))

                elif [ "$dir2/$path" -nt "$dir1/$path" ]
                then [ -z "$v" ] && cp -pf "$dir2/$path"  "$dir1/$path"
                        printf '\033[1;31m1<<<2\033[0m %s\n' "$path"
                        overwritten=$(($overwritten + 1))

                else printf '\033[1m1 = 2\033[0m %s\n' "$path"
                        identical=$(($identical + 1))
                fi
        done

        printf ' \   \    %-30s \033[1;32m%5s\033[0m copied
  \   \__ %-30s \033[1;31m%5s\033[0m overwritten
   \_____ %-30s \033[1;39m%5s\033[0m identical
          %-30s \033[1;30m%5s\033[0m directories\n' \
                ''      "$copied"      \
                "$dir2" "$overwritten" \
                "$dir1" "$identical"   \
                ''      "$directories"
}
PS:
(17-06-2016, 11:26 PM)pranomostro Wrote: https://github.com/pranomostro/script
Oh, so you keep a fork bomb in your scripts, interesting. :)


Messages In This Thread
Automating tasks - by sagittarius - 17-06-2016, 09:03 AM
RE: Automating tasks - by venam - 17-06-2016, 04:37 PM
RE: Automating tasks - by josuah - 17-06-2016, 08:00 PM
RE: Automating tasks - by pranomostro - 17-06-2016, 11:26 PM
RE: Automating tasks - by venam - 18-06-2016, 06:19 AM
RE: Automating tasks - by josuah - 18-06-2016, 08:12 AM
RE: Automating tasks - by pranomostro - 18-06-2016, 11:39 AM