Merge pull request #246 from jankatins/add-main-systemd-unit

Add systemd unit to enable both timer and changes based syncs
pull/308/head
Vitaliy Filippov 2021-05-27 22:50:41 +03:00 committed by GitHub
commit dedb762b74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 18 deletions

View File

@ -90,15 +90,18 @@ Prepare a Google Drive folder in your $HOME directory with `grive -a`.
```bash ```bash
# 'google-drive' is the name of your Google Drive folder in your $HOME directory # '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 enable grive@$(systemd-escape google-drive).service
systemctl --user start grive-timer@$(systemd-escape google-drive).timer systemctl --user start grive@$(systemd-escape google-drive).service
systemctl --user enable grive-changes@$(systemd-escape google-drive).service
systemctl --user start grive-changes@$(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. 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 ### Shared files
Files and folders which are shared with you don't automatically show up in Files and folders which are shared with you don't automatically show up in

View File

@ -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-changes@.service.in grive-changes@.service @ONLY)
CONFIGURE_FILE(grive-timer@.service.in grive-timer@.service @ONLY) CONFIGURE_FILE(grive-timer@.service.in grive-timer@.service @ONLY)
install( install(
FILES FILES
grive@.service
${CMAKE_BINARY_DIR}/systemd/grive-changes@.service ${CMAKE_BINARY_DIR}/systemd/grive-changes@.service
${CMAKE_BINARY_DIR}/systemd/grive-timer@.service ${CMAKE_BINARY_DIR}/systemd/grive-timer@.service
DESTINATION DESTINATION
@ -22,5 +23,5 @@ install(
PROGRAMS PROGRAMS
grive-sync.sh grive-sync.sh
DESTINATION DESTINATION
lib/grive ${CMAKE_INSTALL_FULL_LIBDIR}/grive
) )

View File

@ -62,22 +62,24 @@ unlock() { _lock u; } # drop a lock
sync_directory() { sync_directory() {
_directory="${1}" _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 if ping -c1 -W1 -q accounts.google.com >/dev/null 2>&1; then
true true
# pass # pass
else else
echo "Google drive server not reachable..." echo "Google drive server not reachable, NOT syncing..."
unlock
exit 0 exit 0
fi 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_START=0
TIME_AT_END=1 TIME_AT_END=1
while [[ "${TIME_AT_START}" -lt "${TIME_AT_END}" ]]; do while [[ "${TIME_AT_START}" -lt "${TIME_AT_END}" ]]; do
echo "Syncing "${_directory}"..." echo "Syncing '${_directory}'..."
TIME_AT_START="$(stat -c %Y "$LOCKFILE")" TIME_AT_START="$(stat -c %Y "$LOCKFILE")"
# exclude symlinks from sync # exclude symlinks from sync
cat "${_directory}"/.griveignore 2>/dev/null | sed '/#LINKS-EDIT_BEFORE_THIS$/,$d' > /tmp/.griveignore.base 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 ( 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!$" 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")" TIME_AT_END="$(stat -c %Y "$LOCKFILE")"
echo "Sync of "${_directory}" done." echo "Sync of '${_directory}' done."
done done
# always exit ok, so that we never go into a wrong systemd state # always exit ok, so that we never go into a wrong systemd state
unlock
exit 0 exit 0
} }
### LISTEN TO DIRECTORY CHANGES ### ### LISTEN TO CHANGES IN DIRECTORY ###
listen_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; } 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 while true #run indefinitely
do do

View File

@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Google drive sync Description=Google drive sync (executed by timer unit)
After=network-online.target After=network-online.target
[Service] [Service]

13
systemd/grive@.service Normal file
View File

@ -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