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.
Simple passwords file synchronizer using Rclone
#!/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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment