利用 rclone 搭配 inotifywait 進行雲端目錄 sync 作業

本文阅读 1 分钟
首页 Debian 正文

首先安裝 inotifywait

  1. apt -y install inotify-tools

接著將已下程式碼寫入 /home/sync.sh

  1. #!/bin/bash
  2. ## One-way, immediate, continuous, recursive, directory synchronization
  3. ## to a remote Rclone URL. ( S3, SFTP, FTP, WebDAV, Dropbox, etc. )
  4. ## Optional desktop notifications on sync events or errors.
  5. ## Useful only for syncing a SMALL number of files (< 8192).
  6. ## (See note in `man inotifywait` under `--recursive` about raising this limit.)
  7. ## Think use-case: Synchronize Traefik TLS certificates file (acme.json)
  8. ## Think use-case: Synchronize Keepass (.kdbx) database file immediately on save.
  9. ## Think use-case: Live edit source code and push to remote server
  10. ## This is NOT a backup tool!
  11. ## It will not help you if you delete your files or if they become corrupted.
  12. ## If you need a backup tool, check out https://blog.rymcg.tech/blog/linux/restic_backup
  13. ## Setup: Install `rclone` and `inotify-tools` from package manager.
  14. ## Run: `rclone config` to setup the remote, including the full remote
  15. ## subdirectory path to sync to.
  16. ## MAKE SURE that the remote (sub)directory is EMPTY
  17. ## or else ALL CONTENTS WILL BE DELETED by rclone when it syncs.
  18. ## If unsure, add `--dry-run` to the RCLONE_CMD variable below,
  19. ## to simulate what would be copied/deleted.
  20. ## Enable your user for Systemd Linger: sudo loginctl enable-linger $USER
  21. ## (Reference https://wiki.archlinux.org/title/Systemd/User#Automatic_start-up_of_systemd_user_instances)
  22. ## Copy this script any place on your filesystem, and make it executable: `chown +x sync.sh`
  23. ## Edit all the variables below, before running the script.
  24. ## Run: `./sync.sh systemd_setup` to create and enable systemd service.
  25. ## Run: `journalctl --user --unit rclone_sync.${RCLONE_REMOTE}` to view the logs.
  26. ## For desktop notifications, make sure you have installed a notification daemon (eg. dunst)
  27. ## Edit the variables below, according to your own environment:
  28. # RCLONE_SYNC_PATH: The path to COPY FROM (files are not synced TO here):
  29. RCLONE_SYNC_PATH="/var/backup"
  30. # RCLONE_REMOTE: The rclone remote name to synchronize with.
  31. # Identical to one of the remote names listed via `rclone listremotes`.
  32. # Make sure to include the final `:` in the remote name, which
  33. # indicates to sync/delete from the same (sub)directory as defined in the URL.
  34. # (ALL CONTENTS of the remote are continuously DELETED
  35. # and replaced with the contents from RCLONE_SYNC_PATH)
  36. RCLONE_REMOTE="gdrive:backup"
  37. # RCLONE_CMD: The sync command and arguments:
  38. ## (This example is for one-way sync):
  39. ## (Consider using other modes like `bisync` or `move` [see `man rclone` for details]):
  40. RCLONE_CMD="rclone -v sync ${RCLONE_SYNC_PATH} ${RCLONE_REMOTE}"
  41. # WATCH_EVENTS: The file events that inotifywait should watch for:
  42. WATCH_EVENTS="modify,delete,create,move"
  43. # SYNC_DELAY: Wait this many seconds after an event, before synchronizing:
  44. SYNC_DELAY=5
  45. # SYNC_INTERVAL: Wait this many seconds between forced synchronizations:
  46. SYNC_INTERVAL=3600
  47. # NOTIFY_ENABLE: Enable Desktop notifications
  48. NOTIFY_ENABLE=true
  49. # SYNC_SCRIPT: dynamic reference to the current script path
  50. SYNC_SCRIPT=$(realpath $0)
  51. notify() {
  52. MESSAGE=$1
  53. if test ${NOTIFY_ENABLE} = "true"; then
  54. notify-send "rclone ${RCLONE_REMOTE}" "${MESSAGE}"
  55. fi
  56. }
  57. rclone_sync() {
  58. set -x
  59. # Do initial sync immediately:
  60. notify "Startup"
  61. ${RCLONE_CMD}
  62. # Watch for file events and do continuous immediate syncing
  63. # and regular interval syncing:
  64. while [[ true ]] ; do
  65. inotifywait --recursive --timeout ${SYNC_INTERVAL} -e ${WATCH_EVENTS} \
  66. ${RCLONE_SYNC_PATH} 2>/dev/null
  67. if [ $? -eq 0 ]; then
  68. # File change detected, sync the files after waiting a few seconds:
  69. sleep ${SYNC_DELAY} && ${RCLONE_CMD} && \
  70. notify "Synchronized new file changes"
  71. elif [ $? -eq 1 ]; then
  72. # inotify error occured
  73. notify "inotifywait error exit code 1"
  74. sleep 10
  75. elif [ $? -eq 2 ]; then
  76. # Do the sync now even though no changes were detected:
  77. ${RCLONE_CMD}
  78. fi
  79. done
  80. }
  81. systemd_setup() {
  82. set -x
  83. if loginctl show-user ${USER} | grep "Linger=no"; then
  84. echo "User account does not allow systemd Linger."
  85. echo "To enable lingering, run as root: loginctl enable-linger $USER"
  86. echo "Then try running this command again."
  87. exit 1
  88. fi
  89. mkdir -p ${HOME}/.config/systemd/user
  90. SERVICE_FILE=${HOME}/.config/systemd/user/rclone_sync.${RCLONE_REMOTE}.service
  91. if test -f ${SERVICE_FILE}; then
  92. echo "Unit file already exists: ${SERVICE_FILE} - Not overwriting."
  93. else
  94. cat <<EOF > ${SERVICE_FILE}
  95. [Unit]
  96. Description=rclone_sync ${RCLONE_REMOTE}
  97. [Service]
  98. ExecStart=${SYNC_SCRIPT}
  99. [Install]
  100. WantedBy=default.target
  101. EOF
  102. fi
  103. systemctl --user daemon-reload
  104. systemctl --user enable --now rclone_sync.${RCLONE_REMOTE}
  105. systemctl --user status rclone_sync.${RCLONE_REMOTE}
  106. echo "You can watch the logs with this command:"
  107. echo " journalctl --user --unit rclone_sync.${RCLONE_REMOTE}"
  108. }
  109. if test $# = 0; then
  110. rclone_sync
  111. else
  112. CMD=$1; shift;
  113. ${CMD} $@
  114. fi

接著執行

  1. loginctl enable-linger root
  2. ./sync.sh systemd_setup
本文经授权后发布,本文观点不代表立场,文章出自:https://blog.rymcg.tech/blog/linux/rclone_sync/
VyOS 設定 ip nht resolve-via-default
« 上一篇 12-18
Debian 12 安裝 Nginx Upload Module
下一篇 » 05-09