Skip to content

Instantly share code, notes, and snippets.

@codeasashu
Forked from ZviBaratz/sync-passwords.sh
Last active May 26, 2021 21:09
Show Gist options
  • Select an option

  • Save codeasashu/02e90f11235f51938f8e74f3ca4e8e3c to your computer and use it in GitHub Desktop.

Select an option

Save codeasashu/02e90f11235f51938f8e74f3ca4e8e3c to your computer and use it in GitHub Desktop.

Revisions

  1. codeasashu revised this gist May 26, 2021. 2 changed files with 85 additions and 0 deletions.
    File renamed without changes.
    85 changes: 85 additions & 0 deletions sync-passwords_macos.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    #!/bin/bash


    # Important note:
    # If you don't synchronize but then edit the other file,
    # the newer modification time on the second file edited
    # will cause data to be overridden.
    # The solution would be to merge manually
    # (Database --> Merge from database).


    #####################
    ## Configuration ##
    #####################

    # Name of your remote storage as defined in Rclone
    DRIVE_NAME="google-drive"

    # Name and locations of the passwords file
    DB_FILE_NAME="Passwords.kdbx"
    LOCAL_LOCATION="$HOME/Documents"
    REMOTE_LOCATION="Documents"

    #####################


    # Compose full path to local and remote database files
    LOCAL_PATH="$LOCAL_LOCATION/$DB_FILE_NAME"
    REMOTE_PATH="$REMOTE_LOCATION/$DB_FILE_NAME"

    # Alias import and export commands and make them available within the functions
    alias passwords_export="rclone copy $LOCAL_PATH $DRIVE_NAME:$REMOTE_LOCATION"
    alias passwords_import="rclone copy $DRIVE_NAME:$REMOTE_PATH $LOCAL_LOCATION"
    shopt -s expand_aliases

    # Parse remote passwords file modification time using Rclone's lsl command
    # See: https://rclone.org/commands/rclone_lsl/
    function get_remote_passwords_mtime ()
    {
    output=`rclone lsjson $DRIVE_NAME:$REMOTE_PATH 2>/dev/null`
    if [ $? -eq 3 ]; then
    unset output
    return 1
    else
    local string=`echo "$output" | jq -c ".[] | select( .Path | contains(\"$DB_FILE_NAME\") ) | .ModTime |= (sub(\"\\\\\.[0-9]+Z\";\"Z\") | fromdate) | .ModTime"`
    echo $string
    unset output
    return 0
    fi

    }

    function sync_passwords ()
    {

    # The conversion is required for the comparison in the following if statement
    local_mtime_in_seconds_since_epoch=`stat -f "%Fm" $LOCAL_PATH | cut -d '.' -f 1`
    remote_mtime_in_seconds_since_epoch=`get_remote_passwords_mtime`

    echo "Local modtime: $local_mtime_in_seconds_since_epoch"
    echo "Remote modtime: $remote_mtime_in_seconds_since_epoch"

    # Handle local being newer than remote
    if [ "$local_mtime_in_seconds_since_epoch" -gt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be newer than remote!\n"
    printf "Exporting...\t"
    passwords_export
    printf "Done!\n"
    return 0

    # Handle remote being newer than local
    elif [ "$local_mtime_in_seconds_since_epoch" -lt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be older than remote!\n"
    printf "Importing...\t"
    passwords_import
    printf "Done!\n"
    return 0

    else
    printf "Password files are synchronized.\n"
    return 0
    fi
    }

    sync_passwords
  2. @ZviBaratz ZviBaratz revised this gist Sep 20, 2019. 1 changed file with 25 additions and 3 deletions.
    28 changes: 25 additions & 3 deletions sync-passwords.sh
    Original file line number Diff line number Diff line change
    @@ -49,16 +49,34 @@ function get_local_passwords_mtime ()
    # See: https://rclone.org/commands/rclone_lsl/
    function get_remote_passwords_mtime ()
    {
    local string=`rclone lsl $DRIVE_NAME:$REMOTE_PATH | tr -s ' ' | cut -d ' ' -f 3,4;`
    echo `format_datetime_from_string "$string"`
    output=`rclone lsl $DRIVE_NAME:$REMOTE_PATH 2>/dev/null`
    if [ $? -eq 3 ]; then
    unset output
    return 1
    else
    local string=`echo "$output" | tr -s ' ' | cut -d ' ' -f 3,4;`
    echo `format_datetime_from_string "$string"`
    unset output
    return 0
    fi

    }

    function sync_passwords ()
    {

    # Storing the values so they can be used for printing and then conversion
    local human_readable_local_mtime=`get_local_passwords_mtime`
    local human_readable_remote_mtime=`get_remote_passwords_mtime`
    human_readable_remote_mtime=`get_remote_passwords_mtime 2>/dev/null`

    # In case there is no remote yet
    if [ $? -ne 0 ]; then
    printf "No remote passwords database found!\n"
    printf "Exporting...\t"
    passwords_export
    printf "Done!\n"
    return 0
    fi

    # Printing modification times to the user
    printf "Local passwords file modification time:\t\t$human_readable_local_mtime\n"
    @@ -67,23 +85,27 @@ function sync_passwords ()
    # The conversion is required for the comparison in the following if statement
    local_mtime_in_seconds_since_epoch=$(date -d "$human_readable_local_mtime" +%s)
    remote_mtime_in_seconds_since_epoch=$(date -d "$human_readable_remote_mtime" +%s)
    unset human_readable_remote_mtime

    # Handle local being newer than remote
    if [ "$local_mtime_in_seconds_since_epoch" -gt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be newer than remote!\n"
    printf "Exporting...\t"
    passwords_export
    printf "Done!\n"
    return 0

    # Handle remote being newer than local
    elif [ "$local_mtime_in_seconds_since_epoch" -lt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be older than remote!\n"
    printf "Importing...\t"
    passwords_import
    printf "Done!\n"
    return 0

    else
    printf "Password files are synchronized.\n"
    return 0
    fi
    }

  3. @ZviBaratz ZviBaratz revised this gist Sep 20, 2019. 1 changed file with 29 additions and 18 deletions.
    47 changes: 29 additions & 18 deletions sync-passwords.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,18 @@
    #!/bin/bash

    #####################
    ## Configuration ##
    #####################

    # Important note:
    # If you don't synchronize and then edit the other file,
    # Important note:
    # If you don't synchronize but then edit the other file,
    # the newer modification time on the second file edited
    # will cause data to be overridden.
    # will cause data to be overridden.
    # The solution would be to merge manually
    # (Database --> Merge from database).


    #####################
    ## Configuration ##
    #####################

    # Name of your remote storage as defined in Rclone
    DRIVE_NAME="google-drive"

    @@ -19,58 +21,67 @@ DB_FILE_NAME="Passwords.kdbx"
    LOCAL_LOCATION="$HOME/Documents"
    REMOTE_LOCATION="Documents"

    ######################################
    #####################


    # Compose full path to local and remote database files
    LOCAL_PATH="$LOCAL_LOCATION/$DB_FILE_NAME"
    REMOTE_PATH="$REMOTE_LOCATION/$DB_FILE_NAME"

    # Alias import and export commands and make them available within the functions
    alias passwords_export="rclone copy $LOCAL_PATH $DRIVE_NAME:$REMOTE_LOCATION"
    alias passwords_import="rclone copy $DRIVE_NAME:$REMOTE_PATH $LOCAL_LOCATION"
    shopt -s expand_aliases

    function format_datetime_from_string ()
    {
    echo `date -d "$1" +"%F %T.%3N"`
    }

    # Parse local passwords file modification time using the stat command
    function get_local_passwords_mtime ()
    {
    local string=`stat -c %y $LOCAL_PATH | cut -d ' ' -f 1,2;`
    local mtime=`date -d "$string" +"%F %T.%3N"`
    echo "$mtime"
    echo `format_datetime_from_string "$string"`
    }

    # Parse remote passwords file modification time using Rclone's lsl command
    # See: https://rclone.org/commands/rclone_lsl/
    function get_remote_passwords_mtime ()
    {
    local string=`rclone lsl $DRIVE_NAME:$REMOTE_PATH | tr -s ' ' | cut -d ' ' -f 3,4;`
    local mtime=`date -d "$string" +"%F %T.%3N"`
    echo "$mtime"
    echo `format_datetime_from_string "$string"`
    }

    function sync_passwords ()
    {

    human_readable_local_mtime=`get_local_passwords_mtime`
    human_readable_remote_mtime=`get_remote_passwords_mtime`

    # Storing the values so they can be used for printing and then conversion
    local human_readable_local_mtime=`get_local_passwords_mtime`
    local human_readable_remote_mtime=`get_remote_passwords_mtime`

    # Printing modification times to the user
    printf "Local passwords file modification time:\t\t$human_readable_local_mtime\n"
    printf "Remote passwords file modification time:\t$human_readable_remote_mtime\n"


    # The conversion is required for the comparison in the following if statement
    local_mtime_in_seconds_since_epoch=$(date -d "$human_readable_local_mtime" +%s)
    remote_mtime_in_seconds_since_epoch=$(date -d "$human_readable_remote_mtime" +%s)

    # Handle local being newer than remote
    if [ "$local_mtime_in_seconds_since_epoch" -gt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be newer than remote!\n"
    printf "Exporting...\t"
    passwords_export
    printf "Done!\n"

    # Handle remote being newer than local
    elif [ "$local_mtime_in_seconds_since_epoch" -lt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be older than remote!\n"
    printf "Importing...\t"
    passwords_import
    printf "Done!\n"

    else
    printf "Password files are synchronized.\n"
    fi
  4. @ZviBaratz ZviBaratz revised this gist Sep 18, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions sync-passwords.sh
    Original file line number Diff line number Diff line change
    @@ -75,3 +75,5 @@ function sync_passwords ()
    printf "Password files are synchronized.\n"
    fi
    }

    sync_passwords
  5. @ZviBaratz ZviBaratz created this gist Sep 17, 2019.
    77 changes: 77 additions & 0 deletions sync-passwords.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    #!/bin/bash

    #####################
    ## Configuration ##
    #####################

    # Important note:
    # If you don't synchronize and then edit the other file,
    # the newer modification time on the second file edited
    # will cause data to be overridden.
    # The solution would be to merge manually
    # (Database --> Merge from database).

    # Name of your remote storage as defined in Rclone
    DRIVE_NAME="google-drive"

    # Name and locations of the passwords file
    DB_FILE_NAME="Passwords.kdbx"
    LOCAL_LOCATION="$HOME/Documents"
    REMOTE_LOCATION="Documents"

    ######################################

    LOCAL_PATH="$LOCAL_LOCATION/$DB_FILE_NAME"
    REMOTE_PATH="$REMOTE_LOCATION/$DB_FILE_NAME"

    alias passwords_export="rclone copy $LOCAL_PATH $DRIVE_NAME:$REMOTE_LOCATION"
    alias passwords_import="rclone copy $DRIVE_NAME:$REMOTE_PATH $LOCAL_LOCATION"
    shopt -s expand_aliases

    # Parse local passwords file modification time using the stat command
    function get_local_passwords_mtime ()
    {
    local string=`stat -c %y $LOCAL_PATH | cut -d ' ' -f 1,2;`
    local mtime=`date -d "$string" +"%F %T.%3N"`
    echo "$mtime"
    }

    # Parse remote passwords file modification time using Rclone's lsl command
    # See: https://rclone.org/commands/rclone_lsl/
    function get_remote_passwords_mtime ()
    {
    local string=`rclone lsl $DRIVE_NAME:$REMOTE_PATH | tr -s ' ' | cut -d ' ' -f 3,4;`
    local mtime=`date -d "$string" +"%F %T.%3N"`
    echo "$mtime"
    }

    function sync_passwords ()
    {

    human_readable_local_mtime=`get_local_passwords_mtime`
    human_readable_remote_mtime=`get_remote_passwords_mtime`

    printf "Local passwords file modification time:\t\t$human_readable_local_mtime\n"
    printf "Remote passwords file modification time:\t$human_readable_remote_mtime\n"

    local_mtime_in_seconds_since_epoch=$(date -d "$human_readable_local_mtime" +%s)
    remote_mtime_in_seconds_since_epoch=$(date -d "$human_readable_remote_mtime" +%s)

    # Handle local being newer than remote
    if [ "$local_mtime_in_seconds_since_epoch" -gt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be newer than remote!\n"
    printf "Exporting...\t"
    passwords_export
    printf "Done!\n"

    # Handle remote being newer than local
    elif [ "$local_mtime_in_seconds_since_epoch" -lt "$remote_mtime_in_seconds_since_epoch" ]; then
    printf "Local passwords file found to be older than remote!\n"
    printf "Importing...\t"
    passwords_import
    printf "Done!\n"

    else
    printf "Password files are synchronized.\n"
    fi
    }