Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ bidirectional sync #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions rsync/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
private_key_file: /ssl/rsync/id_rsa
username: user
folders:
- source: /config
destination: /home/user/config-target
- source: /media/playlists
destination: /home/user/cool-playlists
- local: /config
remote: /home/user/config-target
direction: push
- local: /media/playlists
remote: /home/user/cool-playlists
options: '--archive --recursive --compress'
direction: pull
remote_host: ''
remote_folder: /home/user

```

Expand All @@ -37,18 +38,24 @@

The list of folders you want to sync with the remote machine.

### `folders` - `source`
### `folders` - `local`

The source folder for rsync.
The local folder for rsync.

### `folders` - `destination`
### `folders` - `remote`

The destination folder for rsync
The remote folder for rsync

### `folders` - `options` (optional)

Use your own options for rsync. This string is replacing the default one and get directly to rsync. The default is `--archive --recursive --compress --delete --prune-empty-dirs`.

### `folders` - `direction` (optional)

Specify the direction of synchronization for each folder:
- `push`: Copy from Home Assistant to the remote machine (default)

Check failure on line 56 in rsync/DOCS.md

View workflow job for this annotation

GitHub Actions / workflows / 🔎 MarkdownLint

Lists should be surrounded by blank lines [Context: "- `push`: Copy from Home Assis..."]
- `pull`: Copy from the remote machine to Home Assistant

### `remote_host`

The ip or host of the remote machine you want to connect to.
Expand Down
10 changes: 6 additions & 4 deletions rsync/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: rsync

Check warning on line 1 in rsync/config.yaml

View workflow job for this annotation

GitHub Actions / workflows / 🔎 Home Assistant Addon Linter

'map' contains the 'config' folder, which has been replaced by 'homeassistant_config'. See: https://developers.home-assistant.io/blog/2023/11/06/public-addon-config
version: dev
slug: rsync
description: Sync folders to a remote machine via ssh and rsync.
Expand Down Expand Up @@ -26,15 +26,17 @@
private_key_file: match(^/ssl/.+)
username: str
folders:
- source: str
destination: str
- local: str
remote: str
options: str?
direction: str? # 'push' (local to remote) ou 'pull' (remote to local)
MB901 marked this conversation as resolved.
Show resolved Hide resolved
remote_host: str
remote_port: port?
options:
private_key_file: /ssl/rsync/id_rsa
username: user
folders:
- source: /config
destination: /home/user
- local: /config
remote: /home/user
direction: push # default, push local to remote
remote_host: ""
42 changes: 33 additions & 9 deletions rsync/root/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ HOST=$(bashio::config 'remote_host')
USERNAME=$(bashio::config 'username')
FOLDERS=$(bashio::addon.config | jq -r ".folders")

# Create local directories if they don't exist
echo "$FOLDERS" | jq -c '.[]' | while read -r folder; do
LOCAL_DIR=$(echo "$folder" | jq -r '.local')
bashio::log.info "Checking local directory: $LOCAL_DIR"
if [ ! -d "$LOCAL_DIR" ]; then
bashio::log.info "Creating local directory: $LOCAL_DIR"
mkdir -p "$LOCAL_DIR"
# chmod 755 "$LOCAL_DIR"
fi
done

if bashio::config.has_value 'remote_port'; then
PORT=$(bashio::config 'remote_port')
bashio::log.info "Use port $PORT"
Expand All @@ -27,16 +38,29 @@ fi
folder_count=$(echo "$FOLDERS" | jq -r '. | length')
for (( i=0; i<folder_count; i=i+1 )); do

local=$(echo "$FOLDERS" | jq -r ".[$i].source")
remote=$(echo "$FOLDERS" | jq -r ".[$i].destination")
local=$(echo "$FOLDERS" | jq -r ".[$i].local")
remote=$(echo "$FOLDERS" | jq -r ".[$i].remote")
options=$(echo "$FOLDERS" | jq -r ".[$i].options // \"--archive --recursive --compress --delete --prune-empty-dirs\"")
bashio::log.info "Sync ${local} -> ${remote} with options \"${options}\""
set -x
# shellcheck disable=SC2086
rsync ${options} \
-e "ssh -p ${PORT} -i ${PRIVATE_KEY_FILE} -oStrictHostKeyChecking=no" \
"$local" "${USERNAME}@${HOST}:${remote}"
set +x
direction=$(echo "$FOLDERS" | jq -r ".[$i].direction // \"push\"")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It the direction should be optional, then a default value is needed here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other changes are done :)
but I don't understand this one...
"push" is used as the fallback value when no direction is defined in the configuration.

if [ "$direction" = "pull" ]; then
# Pull from remote to local.
bashio::log.info "Sync ${USERNAME}@${HOST}:${remote} -> ${local} with options \"${options}\""
set -x
# shellcheck disable=SC2086
rsync ${options} \
-e "ssh -p ${PORT} -i ${PRIVATE_KEY_FILE} -oStrictHostKeyChecking=no" \
"${USERNAME}@${HOST}:${remote}" "${local}"
set +x
else
# Default push from local to remote
bashio::log.info "Sync ${local} -> ${USERNAME}@${HOST}:${remote} with options \"${options}\""
set -x
# shellcheck disable=SC2086
rsync ${options} \
-e "ssh -p ${PORT} -i ${PRIVATE_KEY_FILE} -oStrictHostKeyChecking=no" \
"$local" "${USERNAME}@${HOST}:${remote}"
set +x
fi
done

bashio::log.info "Synced all folders"
Loading