From 1bab757298506277a8d382c4c74644389c5e7c65 Mon Sep 17 00:00:00 2001 From: Jan Schulz Date: Wed, 5 Dec 2018 00:02:41 +0100 Subject: [PATCH 1/5] Add systemd unit to enable both timer and changes based syncs Now it's enough to enable and start `grive@.service`. --- README.md | 13 ++++++++----- systemd/CMakeLists.txt | 1 + systemd/grive-timer@.service.in | 2 +- systemd/grive@.service | 13 +++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 systemd/grive@.service diff --git a/README.md b/README.md index acb07ca..35eee2a 100644 --- a/README.md +++ b/README.md @@ -90,15 +90,18 @@ Prepare a Google Drive folder in your $HOME directory with `grive -a`. ```bash # 'google-drive' is the name of your Google Drive folder in your $HOME directory -systemctl --user enable grive-timer@$(systemd-escape google-drive).timer -systemctl --user start grive-timer@$(systemd-escape google-drive).timer -systemctl --user enable grive-changes@$(systemd-escape google-drive).service -systemctl --user start grive-changes@$(systemd-escape google-drive).service +systemctl --user enable grive@$(systemd-escape google-drive).service +systemctl --user start grive@$(systemd-escape google-drive).service ``` -You can enable and start these two units for multiple folders in your `$HOME` +You can enable and start this unit for multiple folders in your `$HOME` directory if you need to sync with multiple google accounts. +You can also only enable the time based syncing or the changes based syncing +by only directly enabling and starting the corresponding unit: +`grive-changes@$(systemd-escape google-drive).service` or +`grive-timer@$(systemd-escape google-drive).timer`. + ### Shared files Files and folders which are shared with you don't automatically show up in diff --git a/systemd/CMakeLists.txt b/systemd/CMakeLists.txt index e3e57ba..d10322b 100644 --- a/systemd/CMakeLists.txt +++ b/systemd/CMakeLists.txt @@ -5,6 +5,7 @@ CONFIGURE_FILE(grive-timer@.service.in grive-timer@.service @ONLY) install( FILES + grive@.service ${CMAKE_BINARY_DIR}/systemd/grive-changes@.service ${CMAKE_BINARY_DIR}/systemd/grive-timer@.service DESTINATION diff --git a/systemd/grive-timer@.service.in b/systemd/grive-timer@.service.in index a6aae6a..d37070f 100644 --- a/systemd/grive-timer@.service.in +++ b/systemd/grive-timer@.service.in @@ -1,5 +1,5 @@ [Unit] -Description=Google drive sync +Description=Google drive sync (executed by timer unit) After=network-online.target [Service] diff --git a/systemd/grive@.service b/systemd/grive@.service new file mode 100644 index 0000000..e3372db --- /dev/null +++ b/systemd/grive@.service @@ -0,0 +1,13 @@ +[Unit] +Description=Google drive sync (main) +Requires=grive-timer@%i.timer grive-changes@%i.service + +# dummy service +[Service] +Type=oneshot +ExecStart=/bin/true +# This service shall be considered active after start +RemainAfterExit=yes + +[Install] +WantedBy=default.target From 378df2867b291b03687749e326ad20aed682e9ab Mon Sep 17 00:00:00 2001 From: Jan Katins Date: Mon, 15 Mar 2021 13:30:46 +0100 Subject: [PATCH 2/5] Do explicit unlocks in grive-sync.sh script --- systemd/grive-sync.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/systemd/grive-sync.sh b/systemd/grive-sync.sh index bb837ba..5d32296 100755 --- a/systemd/grive-sync.sh +++ b/systemd/grive-sync.sh @@ -62,18 +62,20 @@ unlock() { _lock u; } # drop a lock sync_directory() { _directory="${1}" + + reset_timer_and_exit() { echo "Retriggered google drive sync" && touch -m $LOCKFILE && exit; } + + exlock_now || reset_timer_and_exit + if ping -c1 -W1 -q accounts.google.com >/dev/null 2>&1; then true # pass else - echo "Google drive server not reachable..." + echo "Google drive server not reachable, NOT syncing..." + unlock exit 0 fi - reset_timer_and_exit() { echo "Retriggered google drive sync" && touch -m "$LOCKFILE" && exit; } - - exlock_now || reset_timer_and_exit - TIME_AT_START=0 TIME_AT_END=1 while [[ "${TIME_AT_START}" -lt "${TIME_AT_END}" ]]; do @@ -91,10 +93,11 @@ sync_directory() { done # always exit ok, so that we never go into a wrong systemd state + unlock exit 0 } -### LISTEN TO DIRECTORY CHANGES ### +### LISTEN TO CHANGES IN DIRECTORY ### listen_directory() { From 4dfc9a546182f5acffcdfd652bd4ffb2e29cccec Mon Sep 17 00:00:00 2001 From: Jan Katins Date: Mon, 15 Mar 2021 13:40:52 +0100 Subject: [PATCH 3/5] Install grive-sync.sh into the right libdir --- systemd/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/systemd/CMakeLists.txt b/systemd/CMakeLists.txt index d10322b..bd6ac92 100644 --- a/systemd/CMakeLists.txt +++ b/systemd/CMakeLists.txt @@ -1,4 +1,4 @@ -SET(GRIVE_SYNC_SH_BINARY "${CMAKE_INSTALL_PREFIX}/lib/grive/grive-sync.sh") +SET(GRIVE_SYNC_SH_BINARY "${CMAKE_INSTALL_FULL_LIBDIR}/grive/grive-sync.sh") CONFIGURE_FILE(grive-changes@.service.in grive-changes@.service @ONLY) CONFIGURE_FILE(grive-timer@.service.in grive-timer@.service @ONLY) @@ -23,5 +23,5 @@ install( PROGRAMS grive-sync.sh DESTINATION - lib/grive + ${CMAKE_INSTALL_FULL_LIBDIR}/grive ) \ No newline at end of file From 5510ca1b0635dd96e6d688603eac07a1318acbef Mon Sep 17 00:00:00 2001 From: Jan Katins Date: Mon, 15 Mar 2021 14:24:36 +0100 Subject: [PATCH 4/5] More consistent directory name output in logs --- systemd/grive-sync.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/systemd/grive-sync.sh b/systemd/grive-sync.sh index 5d32296..23ee92b 100755 --- a/systemd/grive-sync.sh +++ b/systemd/grive-sync.sh @@ -63,7 +63,7 @@ unlock() { _lock u; } # drop a lock sync_directory() { _directory="${1}" - reset_timer_and_exit() { echo "Retriggered google drive sync" && touch -m $LOCKFILE && exit; } + reset_timer_and_exit() { echo "Retriggered google drive sync ('${_directory}')" && touch -m $LOCKFILE && exit; } exlock_now || reset_timer_and_exit @@ -79,7 +79,7 @@ sync_directory() { TIME_AT_START=0 TIME_AT_END=1 while [[ "${TIME_AT_START}" -lt "${TIME_AT_END}" ]]; do - echo "Syncing "${_directory}"..." + echo "Syncing '${_directory}'..." TIME_AT_START="$(stat -c %Y "$LOCKFILE")" # exclude symlinks from sync cat "${_directory}"/.griveignore 2>/dev/null | sed '/#LINKS-EDIT_BEFORE_THIS$/,$d' > /tmp/.griveignore.base @@ -89,7 +89,7 @@ sync_directory() { ( cd "${_directory}" && find . -type l | sed 's/^.\///g'; ) >> "${_directory}"/.griveignore grive -p "${_directory}" 2>&1 | grep -v -E "^Reading local directories$|^Reading remote server file list$|^Synchronizing files$|^Finished!$" TIME_AT_END="$(stat -c %Y "$LOCKFILE")" - echo "Sync of "${_directory}" done." + echo "Sync of '${_directory}' done." done # always exit ok, so that we never go into a wrong systemd state @@ -105,7 +105,7 @@ listen_directory() { type inotifywait >/dev/null 2>&1 || { echo >&2 "I require inotifywait but it's not installed. Aborting."; exit 1; } - echo "Listening for changes in ~/"${_directory}"" + echo "Listening for changes in '${_directory}'" while true #run indefinitely do From 050fcf53a07d99822a7de186d7f4e4a0be209903 Mon Sep 17 00:00:00 2001 From: Jan Katins Date: Tue, 30 Mar 2021 15:19:53 +0200 Subject: [PATCH 5/5] Move helper script into libexec dir This seems to be a proper dir for such "not-for-normal-user" things... --- systemd/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/systemd/CMakeLists.txt b/systemd/CMakeLists.txt index bd6ac92..6de3e45 100644 --- a/systemd/CMakeLists.txt +++ b/systemd/CMakeLists.txt @@ -1,4 +1,4 @@ -SET(GRIVE_SYNC_SH_BINARY "${CMAKE_INSTALL_FULL_LIBDIR}/grive/grive-sync.sh") +SET(GRIVE_SYNC_SH_BINARY "${CMAKE_INSTALL_FULL_LIBEXECDIR}/grive/grive-sync.sh") CONFIGURE_FILE(grive-changes@.service.in grive-changes@.service @ONLY) CONFIGURE_FILE(grive-timer@.service.in grive-timer@.service @ONLY) @@ -24,4 +24,4 @@ install( grive-sync.sh DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/grive -) \ No newline at end of file +)