Skip to content

Commit

Permalink
Switch to GPG
Browse files Browse the repository at this point in the history
  • Loading branch information
vitobotta committed Nov 11, 2023
1 parent 89acdd2 commit f7cfccf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# zsh-history-sync

Syncs your zsh shell history between computers using git and in encrypted format (using openssl), easily. Only requirement is to have a git repository on Github or similar (recommended private although the history is encrypted).
Syncs your zsh shell history between computers using git and in encrypted format (using GPG), easily. Only requirement is to have a git repository on Github or similar (recommended private although the history is encrypted).

If you like this or any of my other projects and would like to help with their development, consider [becoming a sponsor](https://github.com/sponsors/vitobotta).

Expand All @@ -12,7 +12,7 @@ Notes:

## Installation

You need to clone this repo with the scripts somewhere and run the install script. The installer will ask you for the path to your git repository that you want to use to synchronise the history, as well as a password to encrypt it (the password will be stored in ~/.zsh-history-sync.encryption-key). The install script then updates your .zshrc to load what's required to trigger the synchronisation in background.
You need to clone this repo with the scripts somewhere and run the install script. The installer will ask you for the path to your git repository that you want to use to synchronise the history, as well the UID of the GPG key you want to use to encrypt the history. The install script then updates your .zshrc to load what's required to trigger the synchronisation in background.

```bash
git clone https://github.com/vitobotta/zsh-history-sync.git
Expand All @@ -22,20 +22,3 @@ cd zsh-history-sync

source ~/.zshrc
```

I recommend you also schedule a sync every minute (just to ensure every command is synced since the automatic sync depends on when the last command was executed). It's better to specify an offset on the second computer, so to minimise the risk of sync conflicts. Using crontab, on the first computer:

```
* * * * * /path/to/zsh-history-sync/sync-history.sh /path/to/your/repo
```

On the second computer:

```
* * * * * sleep 30; /path/to/zsh-history-sync/sync-history.sh /path/to/your/repo
```





6 changes: 3 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
echo "Please enter the full path to the git repository you want to use for the syncing:"
read GIT_REPO_PATH

echo "Please enter the password to use for encryption:"
read -s ENCRYPTION_PASSWORD
echo "Please enter your GPG key UID for encryption:"
read GPG_KEY_UID

SCRIPT_PATH="$(readlink -f "$0")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"

echo "export ZSH_HISTORY_SYNC_SCRIPT_PATH=${SCRIPT_DIR}/sync-history.sh" >> ~/.zshrc
echo "export ZSH_HISTORY_SYNC_GIT_REPO_PATH=${GIT_REPO_PATH}" >> ~/.zshrc
echo "${ENCRYPTION_PASSWORD}" > ${HOME}/.zsh-history-sync.encryption-key
echo "export ZSH_HISTORY_SYNC_GPG_KEY_UID=${GPG_KEY_UID}" >> ~/.zshrc
echo source "${SCRIPT_DIR}/zsh.include.sh" >> ~/.zshrc
19 changes: 16 additions & 3 deletions sync-history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ if [ -e ${lockfile} ] && kill -0 `cat ${lockfile}`; then
exit
fi

# make sure the lockfile is removed when we exit and when we receive a signal
trap "rm -f ${lockfile}; exit" INT TERM EXIT
echo $$ > ${lockfile}

Expand All @@ -22,6 +21,8 @@ last_command_timestamp_file="${HOME}/.zsh-history-sync.last-sync"
encryption_key_file="${HOME}/.zsh-history-sync.encryption-key"
identifier="$(hostname)"

ZSH_HISTORY_SYNC_GPG_KEY_UID="${ZSH_HISTORY_SYNC_GPG_KEY_UID:-}"

read_file() {
if [ ! -f $1 ]; then
echo "$1 doesn't exist, creating..."
Expand All @@ -36,6 +37,18 @@ read_file() {
done
}

GPG_CMD=$(which gpg)

if [[ -z "$GPG_CMD" ]]; then
echo "No GPG binary found."
exit 1
fi

if [[ -z "$ZSH_HISTORY_SYNC_GPG_KEY_UID" ]]; then
echo "No GPG key UID specified."
exit 1
fi

current_time=$(date +%s)
last_executed_time=$(cat $last_command_timestamp_file 2>/dev/null || echo 0)

Expand All @@ -47,7 +60,7 @@ if (( current_time - last_executed_time >= 30 )) || [ "$force_sync" = "-f" ]; th

if [[ -f $sync_file ]]; then
temp_sync_file=$(mktemp)
openssl enc -aes-256-cbc -md sha256 -d -in "$sync_file" -out "$temp_sync_file" -pass file:"$encryption_key_file" -pbkdf2
$GPG_CMD --decrypt "$sync_file" > "$temp_sync_file" 2>/dev/null
new_items=$(read_file "$temp_sync_file")
rm "$temp_sync_file"
else
Expand All @@ -58,7 +71,7 @@ if (( current_time - last_executed_time >= 30 )) || [ "$force_sync" = "-f" ]; th
items=$(echo -e "$source_items\n$new_items" | grep -v '^\:\s[<=>]\{3\}' | awk '!x[$0]++')

echo -e "$items" > $source_file
echo -e "$items" | openssl enc -aes-256-cbc -md sha256 -out "$sync_file" -pass file:"$encryption_key_file" -pbkdf2
echo -e "$items" | $GPG_CMD --encrypt --trust-model always --yes --recipient "$ZSH_HISTORY_SYNC_GPG_KEY_UID" --output "$sync_file" 2>/dev/null

fc -R $source_file

Expand Down

0 comments on commit f7cfccf

Please sign in to comment.