syncPhotos.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. # User config
  3. export BASE_LOCAL="/Volumes/iPhoto"
  4. export BASE_REMOTE="/Volumes/webdav.opendrive.com"
  5. SOURCE_LOCAL="/mnt/media/Pictures/iPhoto.sparsebundle"
  6. SOURCE_REMOTE="https://webdav.opendrive.com"
  7. TARGET="Shared.photolibrary"
  8. # App config
  9. RETRIES=10
  10. SYNC_BIN="${HOME}/bin/video/backup/sync.sh"
  11. # Mount the remote drive if needed
  12. MOUNTED_REMOTE=0
  13. if [ ! -d "${BASE_REMOTE}" ]; then
  14. # Mount may be timed out, which will fail the -d test
  15. if mount | grep -q "${BASE_REMOTE}"; then
  16. umount "${BASE_REMOTE}"
  17. fi
  18. mkdir "${BASE_REMOTE}"
  19. fi
  20. if [ ! -d "${BASE_REMOTE}/${TARGET}" ]; then
  21. MOUNTED_REMOTE=1
  22. FAIL_COUNT=0
  23. while ! mount_webdav -s "${SOURCE_REMOTE}" "${BASE_REMOTE}"; do
  24. if [ $FAIL_COUNT -gt $RETRIES ]; then
  25. echo "Unable to mount remote drive" 1>&2
  26. exit 1
  27. fi
  28. FAIL_COUNT=$(( $FAIL_COUNT + 1 ))
  29. sleep 5
  30. done
  31. fi
  32. # Mount th local drive if needed
  33. MOUNTED_LOCAL=0
  34. if [ ! -d "${BASE_LOCAL}" ]; then
  35. MOUNTED_LOCAL=1
  36. hdiutil attach -readonly "${SOURCE_LOCAL}" >/dev/null 2>&1
  37. fi
  38. # Backup if all went well
  39. if [ -d "${BASE_REMOTE}" ] && [ -d "${BASE_LOCAL}" ]; then
  40. "${SYNC_BIN}" "${TARGET}" 1000
  41. fi
  42. # Detach the local drive if we mounted it
  43. if [ -d "${BASE_LOCAL}" ] && [ $MOUNTED_LOCAL -gt 0 ]; then
  44. hdiutil detach "${BASE_LOCAL}" >/dev/null 2>&1
  45. fi
  46. # Detach the remote drive if we mounted it
  47. if [ -d "${BASE_REMOTE}" ] &&[ $MOUNTED_REMOTE -gt 0 ]; then
  48. umount "${BASE_REMOTE}"
  49. fi
  50. # Always exit cleanly
  51. exit 0