filesync 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env bash
  2. lockdir="/tmp/filesync.lock"
  3. help() {
  4. cat<<DOC
  5. Usage: $(basename $0) (setup|sync|pull|mount|umount)
  6. DOC
  7. }
  8. has? () {
  9. command -v "$1" &> /dev/null
  10. }
  11. syncDone() {
  12. if [[ -d "$lockdir" ]]; then
  13. rm -rf -- "$lockdir"
  14. fi
  15. }
  16. syncing() {
  17. # http://mywiki.wooledge.org/BashFAQ/045
  18. if mkdir "$lockdir"; then
  19. echo "Aquired lock"
  20. else
  21. echo "Syncing already in progress"
  22. exit 1
  23. fi
  24. }
  25. install_software() {
  26. has? encfs && echo "encfs installed" || install encfs
  27. has? sshfs && echo "sshfs installed" || install sshfs
  28. has? s3cmd && echo "s3cmd installed" || install s3cmd
  29. has? inotifywatch && echo "inotifywatch installed" || install inotifywatch
  30. }
  31. install() {
  32. printf "Installing $1\n"
  33. if has? apt-get; then
  34. sudo apt-get install "$1"
  35. elif has? pacman; then
  36. sudo pacman -S "$1"
  37. else
  38. printf "Can't install $1\n"
  39. fi
  40. }
  41. mount() {
  42. if mountpoint -q "$HOME/.backup/crypt"; then
  43. echo "crypt is mounted"
  44. else
  45. echo "mounting crypt"
  46. sshfs -o uid=$(id -u) -o gid=$(id -g) -o idmap=user -o allow_other \
  47. tyler@172.16.0.10:/volume1/homes/tyler/backup "$HOME/.backup/crypt"
  48. # check if it's mounted yet
  49. if ! mountpoint -q "$HOME/.backup/crypt"; then
  50. echo "sshfs mount fail"
  51. exit 1
  52. fi
  53. fi
  54. if mountpoint -q "$HOME/.backup/clear"; then
  55. echo "clear is mounted"
  56. else
  57. echo "mounting clear"
  58. cat "$HOME/.encfs.conf" | encfs -S "$HOME/.backup/crypt/" "$HOME/.backup/clear/"
  59. # check if it's mounted yet
  60. if ! mountpoint -q "$HOME/.backup/clear"; then
  61. echo "encfs mount fail"
  62. exit 1
  63. fi
  64. fi
  65. echo "clear and crypt are mounted"
  66. }
  67. umount() {
  68. fusermount -u "$HOME/.backup/clear"
  69. fusermount -u "$HOME/.backup/crypt"
  70. }
  71. setup() {
  72. install_software
  73. mkdir -p "$HOME/.backup/crypt"
  74. mkdir -p "$HOME/.backup/clear"
  75. mkdir -p "$HOME/backup"
  76. touch "$HOME/.backup/status"
  77. mount
  78. }
  79. pull() {
  80. setup
  81. syncing
  82. rsync --delete --modify-window=1 -avz "$HOME/.backup/clear/." "$HOME/backup/."
  83. syncDone
  84. }
  85. sync() {
  86. mount
  87. syncing
  88. rsync --delete --modify-window=1 -avz "$HOME/backup/." "$HOME/.backup/clear/."
  89. syncDone
  90. }
  91. public() {
  92. mount
  93. has? s3cmd && echo "s3cmd installed" || install_s3cmd
  94. # exit if there is no s3cfg
  95. [[ ! -r "$HOME/.s3cfg" ]] && exit 1
  96. syncing
  97. s3cmd sync --delete-removed "$HOME/backup/Public/" s3://tyler.zone/public/
  98. s3cmd setacl --acl-public --recursive s3://tyler.zone/public/
  99. syncDone
  100. }
  101. ___doSync() {
  102. local fd=$(tail -1 "$HOME/.backup/status" | cut -d' ' -f1)
  103. local public=$(echo "$fd" | grep -c "$HOME/backup/Public")
  104. local push=$(echo "$fd" | grep -c "$HOME/backup")
  105. if (( public > 0 )); then
  106. public
  107. fi
  108. if (( push > 0 )); then
  109. sync
  110. else
  111. setup
  112. fi
  113. }
  114. daemon() {
  115. setup
  116. inotifywait -e create,delete,modify,move --format "%w" -r "$HOME/backup" -do "$HOME/.backup/status"
  117. inotifywait -e create,delete,modify,move --format "%w" -r "$HOME/.backup/clear" -do "$HOME/.backup/status"
  118. inotifywait -e create,delete,modify,move --format "%w" -r "$HOME/backup/Public" -do "$HOME/.backup/status"
  119. while true; do
  120. inotifywait -e modify "$HOME/.backup/status" && ___doSync
  121. done
  122. }
  123. show() {
  124. if [[ $1 == 'pids' ]]; then
  125. ps aux | grep "inotifywait -e create,delete,modify,move --format %w -r $HOME" | grep -v grep | awk '{print $2}'
  126. ps aux | grep "filesync daemon" | grep -v grep | awk '{print $2}'
  127. ps aux | grep "inotifywait -e modify $HOME/.backup/status" | grep -v grep | awk '{print $2}'
  128. else
  129. ps aux | grep "inotifywait -e create,delete,modify,move --format %w -r $HOME" | grep -v grep
  130. ps aux | grep "filesync daemon" | grep -v grep
  131. ps aux | grep "inotifywait -e modify $HOME/.backup/status" | grep -v grep
  132. fi
  133. }
  134. main() {
  135. if (( $# < 1 )); then
  136. help
  137. exit 1
  138. fi
  139. case $1 in
  140. sync)
  141. shift
  142. if (( $# > 0 )); then
  143. if [[ $1 == 'public' ]]; then
  144. public
  145. else
  146. help
  147. exit 1
  148. fi
  149. else
  150. sync
  151. fi
  152. ;;
  153. show)
  154. shift
  155. if (( $# > 0 )); then
  156. if [[ $1 == 'pids' ]]; then
  157. show pids
  158. else
  159. help
  160. exit 1
  161. fi
  162. else
  163. show
  164. fi
  165. ;;
  166. mount) mount ;;
  167. umount) umount ;;
  168. setup)
  169. setup
  170. ;;
  171. pull) pull ;;
  172. public) public ;;
  173. daemon) daemon ;;
  174. *)
  175. help
  176. exit 1
  177. ;;
  178. esac
  179. }
  180. main "$@"