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

Feature "sync shared" #1978

Merged
merged 57 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
5f2b7df
removed "is set" check (#)
AlvinSchiller Feb 1, 2023
9c9e7c4
Sync fuer shortcuts und audiofile von lokalem Ordner (wenn Datei/Ordn…
AlvinSchiller Feb 3, 2023
80631a9
chmod und chown korrigiert. update mpc angepasst
AlvinSchiller Feb 3, 2023
d8e1f2f
sync in eigenes Script ausgelagert
AlvinSchiller Feb 4, 2023
06604d3
Settings in conf ausgelagert. Weitere Anpassungen im Abluaf
AlvinSchiller Feb 5, 2023
085b542
Synchronisierung korrigiert
AlvinSchiller Feb 6, 2023
aba35ee
gitignore erweitert
AlvinSchiller Feb 6, 2023
9c18f6c
Sync Shared als Componente ausgelagert
AlvinSchiller Feb 8, 2023
a7203d3
Sync Shared Flag angepasst
AlvinSchiller Feb 9, 2023
6a5e3bb
Skripte Pfade und Aufrufe korrigiert
AlvinSchiller Feb 9, 2023
a2dbfd6
Skript Name angepasst
AlvinSchiller Feb 9, 2023
4abeb47
Install Skript hinzugefuegt
AlvinSchiller Feb 9, 2023
8aeafb9
ReadMe hinzugefuegt
AlvinSchiller Feb 9, 2023
66ea56c
New feature 'full sync'. rsync options corrected
AlvinSchiller Feb 10, 2023
a59b8f0
Added DEBUG_sync_shared_sh to WebUi
AlvinSchiller Feb 10, 2023
54c8975
Added new Feature
AlvinSchiller Feb 11, 2023
2984e70
corrected description of new Command
AlvinSchiller Feb 11, 2023
d11f81b
Timeout for server check added to settings
AlvinSchiller Feb 11, 2023
40b9882
Added script for configuration. Improved installation
AlvinSchiller Feb 16, 2023
a8dd815
Adjusted sync rules for folder.conf
AlvinSchiller Feb 16, 2023
546ba57
Added Readme
AlvinSchiller Feb 16, 2023
81aed4c
Readme adjusted
AlvinSchiller Feb 16, 2023
0c7b790
Readme adjusted
AlvinSchiller Feb 16, 2023
fc29a71
refactored sync script.
AlvinSchiller Feb 17, 2023
d32e65d
changed definition of owner and rights in setup conf
AlvinSchiller Feb 17, 2023
c641994
refactored sync script
AlvinSchiller Feb 17, 2023
f9aacba
refactoring and path handling
AlvinSchiller Feb 19, 2023
7d81a0d
removed leftover var
AlvinSchiller Feb 19, 2023
b1d5f5d
new rfid commands harmonised
AlvinSchiller Feb 19, 2023
77b2983
minor corrections
AlvinSchiller Feb 19, 2023
f1126a0
Added synchronisation mode SSH
AlvinSchiller Feb 20, 2023
0435e6f
Settings sample updated
AlvinSchiller Feb 20, 2023
f941843
Exclude special folders from syncing
AlvinSchiller Feb 22, 2023
40cdb53
renamed folder sync-shared-from-server to sync-shared
AlvinSchiller Feb 22, 2023
524f34f
refactored commands to playout_control.sh
AlvinSchiller Feb 24, 2023
f3f38be
fixed exec_for_mode for paths with whitespaces
AlvinSchiller Feb 24, 2023
d6ecd0e
use consinstent quotation
AlvinSchiller Feb 25, 2023
c3534ed
use consistent var access
AlvinSchiller Feb 25, 2023
e939f3b
removed unused logic
AlvinSchiller Feb 25, 2023
7c38da1
added documentation comments to conf file
AlvinSchiller Feb 25, 2023
765c21a
reordered conf and updated README
AlvinSchiller Feb 25, 2023
ebadc11
updated README
AlvinSchiller Feb 25, 2023
7182caa
refactored function definition
AlvinSchiller Feb 25, 2023
11e8cc2
fixed sync_from_server for paths with whitespaces
AlvinSchiller Feb 26, 2023
26d9462
applied editorconfig
AlvinSchiller Feb 26, 2023
e574191
applied filename convention
AlvinSchiller Feb 26, 2023
90587b7
refactored filepath to var
AlvinSchiller Feb 26, 2023
135b5f0
updated README
AlvinSchiller Feb 26, 2023
e96212b
Merge branch 'MiczFlor:develop' into test/sync
AlvinSchiller Feb 26, 2023
6384a09
fixed db update after sync for spotify version
AlvinSchiller Mar 1, 2023
40ff9ce
install packages for openssh-client added
AlvinSchiller Mar 1, 2023
cd57af1
updated comments
AlvinSchiller Mar 1, 2023
ee43b27
Update README.md
AlvinSchiller Mar 1, 2023
cd0e339
fixed logic for subfolder shortcuts
AlvinSchiller Mar 2, 2023
78a5b85
Update README.md
AlvinSchiller Mar 2, 2023
ab2ba64
Merge branch 'MiczFlor:develop' into feature/sync-shared
AlvinSchiller Mar 3, 2023
49193ae
Merge branch 'develop' into feature/sync-shared
AlvinSchiller Mar 11, 2023
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ settings/Playlists_Folders_Path
settings/rfid_trigger_play.conf
settings/Second_Swipe
settings/ShowCover
settings/sync-shared.conf
settings/sync-shared-enabled
settings/version
htdocs/config.php
scripts/deviceName.txt
Expand Down
61 changes: 61 additions & 0 deletions components/synchronisation/sync-shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Synchronize shared files from a server

This component activates a synchronisation from a server to the Phoniebox for the shared folders 'shortcuts' and 'audiofolders'.
It allows to manage the shortcuts and audiofiles of one to many Phonieboxes in a central place (e.g. NAS, one main Phoniebox, ...) in the network, but keeps the possibility to play audio offline once the data where synced.

The synchronisation can be initiated as a "full-sync" command and optionally on every RFID scan for the particular CardID (and corresponding audiofolder).
For the "full-sync" bind a CardId to the command "SYNCSHAREDFULL".
For the "RFID scan sync" feature, activate the option in the configuration. There is also a command to toggle the activation of the "RFID scan sync" feature via CardId (bind to command "SYNCSHAREDONRFIDSCANTOGGLE").

## Synchronisation

The synchronisation will be FROM a server TO the Phoniebox, overriding existing files. So configuration of audiofiles / -folders made locally will be lost after sync.
If you want to make the initial setup e.g. via WebUi copy the files and use it as a base for the server.
The "folder.conf" files will be synced if present on the server, but not delete if not (they are automatically generated on playback).

To access the files on the server two modes are supported: SSH or MOUNT.
Please make sure you have the correct access rights for the source and use key-based authentication for SSH.

### RFID scan sync
If the feature "RFID scan sync" is activated, there will be a check on every RFID scan against the server if a matching shortcut and audiofolder is found and the changes will be transfered.
The playback will be delayed for the time the data are transfered (see "SYNCSHAREDFULL" to use a full-sync if a lot of new files have been added).
If the server is not reachable the check will be aborted after the timeout. So an unreachable server will cause a delay (see command "SYNCSHAREDONRFIDSCANTOGGLE" to toggle activation state).
Deleted shortcuts / audiofolders (not the contained items) will not be purged locally if deleted on remote. This is also true for changed shortcuts (the old audiofolder / -files will remain). To also delete not existing items us a "full-sync".

## Installation

Run the 'install-sync-shared.sh' script. This will install all required packages and rights.
Please configure all settings according to your setup.


## Configuration

If your configuration has changed, run the script 'change_configuration.sh' to update the settings. This lets you also deactivate this feature.
You may also change the settings in the according files directly.

### Settings:

**{INSTALLATION_ROOT}/settings/sync-shared-enabled**

Holds the activation state of this feature. Values are "TRUE" or "FALSE"


**{INSTALLATION_ROOT}/settings/sync-shared.conf**

SYNCSHAREDMODE: The mode to access the server files. SSH or MOUNT

SYNCSHAREDREMOTESSHUSER: The username if SSH mode is used.

SYNCSHAREDREMOTESERVER: The IP or hostname of the server (used to check connectivity and SSH mode). e.g. "192.168.0.2" or "myhomeserver.local"

SYNCSHAREDREMOTEPORT: The port of the server (used to check connectivity and SSH mode). e.g. "80" or "22"

SYNCSHAREDREMOTETIMOUT: The timeout to reach the server (in seconds) (used to check connectivity). e.g. 1

SYNCSHAREDREMOTEPATH: The path to the shared files to sync (without trailing slash) (remote path for SSH mode or local path for MOUNT mode). e.g. "/mnt/Phoniebox"

SYNCSHAREDONRFIDSCAN: Holds the activation state of the optional feature "RFID scan sync". Values are "TRUE" or "FALSE"


## Special Thanks
inspired by [splitti - phoniebox_rsync](https://github.com/splitti/phoniebox_rsync)
139 changes: 139 additions & 0 deletions components/synchronisation/sync-shared/change-configuration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/bash

PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJROOTPATH="${PATHDATA}/../../.."
STATEFILE="${PROJROOTPATH}/settings/sync-shared-enabled"
CONFFILE="${PROJROOTPATH}/settings/sync-shared.conf"
SKIP_INITIAL_CHECK="$1"


#############################################################
# Functions

set_activation() {
local SETTINGVALUE="$1"

local SETTINGSTATE="activated"
if [ "$SETTINGVALUE" != "TRUE" ]; then
SETTINGSTATE="deactivated"
fi

# Let global controls know this feature is enabled
echo -e "\nLet global controls know this feature is ${SETTINGSTATE}. (sync-shared-enabled -> ${SETTINGVALUE})"

echo "$SETTINGVALUE" > "$STATEFILE"
sudo chgrp www-data "$STATEFILE"
sudo chmod 775 "$STATEFILE"
}

init_settings() {
# Init config from sample if not present
if [ ! -f "$CONFFILE" ]; then
cp "${PATHDATA}/settings/sync-shared.conf.sample" "$CONFFILE"
# change the read/write so that later this might also be editable through the web app
sudo chgrp www-data "$CONFFILE"
sudo chmod 775 "$CONFFILE"
fi
. "$CONFFILE"
}

set_setting() {
local SETTINGNAME="$1"
local SETTINGVALUE="$2"

# check if value is set and not equal to the current settings value
if [ ! -z "$SETTINGVALUE" -a "${!SETTINGNAME}" != "$SETTINGVALUE" ]; then
sed -i "s|^${SETTINGNAME}=.*|${SETTINGNAME}=\"${SETTINGVALUE}\"|g" "$CONFFILE"
echo "New value: \"${SETTINGVALUE}\""
fi
}

read_setting() {
local SETTINGNAME="$1"
local TEXT="$2"

local READ_PROMPT=$'\n'"${TEXT} Leave blank for no change."
READ_PROMPT="${READ_PROMPT}"$'\n'"Current value = \"${SETTINGNAME}\""$'\n'

read -rp "$READ_PROMPT" response
}

read_all_settings() {

read_setting "$SYNCSHAREDMODE" "Choose synchronisation mode to access the server (m[ount]/s[sh])."
case "$response" in
[mM][oO][uU][nN][tT]|[mM][nN][tT]|[mM])
response="MOUNT"
;;
[sS][sS][hH]|[sS])
response="SSH"
;;
*)
# no change
;;
esac
set_setting "SYNCSHAREDMODE" "$response"

if [ "$response" == "SSH" ]; then
read_setting "$SYNCSHAREDREMOTESSHUSER" "Please enter SSH user."
set_setting "SYNCSHAREDREMOTESSHUSER" "$response"
fi

read_setting "$SYNCSHAREDREMOTESERVER" "Please enter your servers adresse (IP/Hostname)."
set_setting "SYNCSHAREDREMOTESERVER" "$response"

read_setting "$SYNCSHAREDREMOTEPORT" "Please enter your servers port."
set_setting "SYNCSHAREDREMOTEPORT" "$response"

read_setting "$SYNCSHAREDREMOTETIMOUT" "Please enter the timeout to try to reach the server (in seconds)."
set_setting "SYNCSHAREDREMOTETIMOUT" "$response"

read_setting "$SYNCSHAREDREMOTEPATH" "Please enter the path to the shared files to sync (without trailing slash)."
# Make sure paths dont have a trailing slash ({VAR%/})
set_setting "SYNCSHAREDREMOTEPATH" "${response%/}"

read_setting "$SYNCSHAREDONRFIDSCAN" "Do you want to activate the syncronisation on RFID scan (y[es]/n[o])."
case "$response" in
[yY][eE][sS]|[yY])
response="TRUE"
;;
[nN][oO]|[nN])
response="FALSE"
;;
*)
;;
esac
set_setting "SYNCSHAREDONRFIDSCAN" "$response"
}

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

# If intial check is skipped, asume the component shall be activated
if [ -z "$SKIP_INITIAL_CHECK" ]; then
read -rp "Do you want to activate the sync-shared component? [Y/n] " response
else
response="yes"
fi

case "$response" in
[nN][oO]|[nN])
set_activation "FALSE"
;;
*)
set_activation "TRUE"

# Ensure start was intended
read -rp "Do you want to change the configuration? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
exit
;;
*)
;;
esac
init_settings
read_all_settings
;;
esac

echo -e "\nConfiguration finished"
28 changes: 28 additions & 0 deletions components/synchronisation/sync-shared/install-sync-shared.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# Ensure start was intended
read -rp "Start installation? [Y/n] " response
case "$response" in
[nN][oO]|[nN])
echo "Installation aborted..."
exit
;;
*)
;;
esac

PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJROOTPATH="${PATHDATA}/../../.."

# Ensure script is executable for everyone
sudo chmod ugo+rx "${PATHDATA}/sync-shared.sh"

# Make sure required packages are installed
echo -e "\nChecking rsync package"
sudo apt install rsync -y
echo -e "\nChecking ssh package"
sudo apt install openssh-client -y

"${PATHDATA}"/change-configuration.sh "SkipInitialCheck"

echo -e "\n\nFINAL NOTE:\nPlease check README.md for further configuration"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# The mode to access the server files. SSH or MOUNT
SYNCSHAREDMODE="MOUNT"
# The username if SSH mode is used
SYNCSHAREDREMOTESSHUSER=""
# The IP or hostname of the server (used to check connectivity and SSH mode)
SYNCSHAREDREMOTESERVER=""
# The port of the server (used to check connectivity and SSH mode)
SYNCSHAREDREMOTEPORT=""
# The timeout to reach the server (in seconds) (used to check connectivity)
SYNCSHAREDREMOTETIMOUT="1"
# The path to the shared files to sync (without trailing slash) (remote path for SSH mode or local path for MOUNT mode)
SYNCSHAREDREMOTEPATH=""
# Holds the activation state of the optional feature "RFID scan sync"
SYNCSHAREDONRFIDSCAN="FALSE"
Loading