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..6de3e45 100644 --- a/systemd/CMakeLists.txt +++ b/systemd/CMakeLists.txt @@ -1,10 +1,11 @@ -SET(GRIVE_SYNC_SH_BINARY "${CMAKE_INSTALL_PREFIX}/lib/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) install( FILES + grive@.service ${CMAKE_BINARY_DIR}/systemd/grive-changes@.service ${CMAKE_BINARY_DIR}/systemd/grive-timer@.service DESTINATION @@ -22,5 +23,5 @@ install( PROGRAMS grive-sync.sh DESTINATION - lib/grive -) \ No newline at end of file + ${CMAKE_INSTALL_FULL_LIBDIR}/grive +) diff --git a/systemd/grive-sync.sh b/systemd/grive-sync.sh index 92b3f6e..c15ab08 100755 --- a/systemd/grive-sync.sh +++ b/systemd/grive-sync.sh @@ -62,22 +62,24 @@ unlock() { _lock u; } # drop a lock sync_directory() { _directory="${1}" + + reset_timer_and_exit() { echo "Retriggered google drive sync ('${_directory}')" && 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 - 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 @@ -87,14 +89,15 @@ 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 + unlock exit 0 } -### LISTEN TO DIRECTORY CHANGES ### +### LISTEN TO CHANGES IN DIRECTORY ### listen_directory() { @@ -102,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 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