Skip to content

Commit

Permalink
Feature "sync shared" (MiczFlor#1978)
Browse files Browse the repository at this point in the history
* removed "is set" check (#)

unquoted "is set" check fails if var is not set and if-statement has a following check. Further the preceding "is empty" check is sufficient in this case.

* Sync fuer shortcuts und audiofile von lokalem Ordner (wenn Datei/Ordner nicht existent)

* chmod und chown korrigiert. update mpc angepasst

* sync in eigenes Script ausgelagert

* Settings in conf ausgelagert. Weitere Anpassungen im Abluaf

Enabled Flag hinzugefuegt, mit fruehem ausstieg.
rsync command ignoriert nun hinzufuegen der "folder.conf" als Aenderung.
- hierdurch sync nicht nur bei erstem Scan sondern immer moeglich
Ordner auf Remote werden initialisiert wenn nicht vorhanden.

* Synchronisierung korrigiert

Uunveraenderte Dateien werden uebersprungen (--update). Permissions ignoriert (--np-perms). folder.conf als Aenderung ignoriert (--exclude).
Nachtraegliches Setzen der Persmission korrigiert.

* gitignore erweitert

* Sync Shared als Componente ausgelagert

* Sync Shared Flag angepasst

* Skripte Pfade und Aufrufe korrigiert

* Skript Name angepasst

* Install Skript hinzugefuegt

* ReadMe hinzugefuegt

* New feature 'full sync'. rsync options corrected

Added full synchronisation on rfid scan.
Changed rsync option to correctly sync files and ignore 'folder.conf' and 'placholder' file

* Added DEBUG_sync_shared_sh to WebUi

* Added new Feature

Command for changing acticvation of 'cync on rfid scan'

* corrected description of new Command

* Timeout for server check added to settings

* Added script for configuration. Improved installation

* Adjusted sync rules for folder.conf

Sync folder.conf from server, but dont delete it if only present on phoniebox (except folder will be deleted)

* Added Readme

* Readme adjusted

* Readme adjusted

* refactored sync script.

* changed definition of owner and rights in setup conf

* refactored sync script

added function for changing access of files/folder
changed definition of SYNC*PATH variables

* refactoring and path handling

Make sure the path variables are alike and dont have a trailing slash
moved command handling to funtions
moved rsync to single function
moved server check to funtion

* removed leftover var

* new rfid commands harmonised

* minor corrections

* Added synchronisation mode SSH

config updated for new settings

* Settings sample updated

* Exclude special folders from syncing

* renamed folder sync-shared-from-server to sync-shared

Updated README.md

* refactored commands to playout_control.sh

fullsync and changeOnRfidScan refactored to playout_controls.sh

* fixed exec_for_mode for paths with whitespaces

* use consinstent quotation

* use consistent var access

* removed unused logic

* added documentation comments to conf file

* reordered conf and updated README

* updated README

* refactored function definition

* fixed sync_from_server for paths with whitespaces

* applied editorconfig

* applied filename convention

* refactored filepath to var

* updated README

added special thanks section

* fixed db update after sync for spotify version

* install packages for openssh-client added

* updated comments

* Update README.md

* fixed logic for subfolder shortcuts

* Update README.md
  • Loading branch information
AlvinSchiller committed Dec 21, 2023
1 parent 114e90e commit ffadce8
Show file tree
Hide file tree
Showing 11 changed files with 768 additions and 158 deletions.
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

0 comments on commit ffadce8

Please sign in to comment.