Advertisement
Guest User

NAS Cron

a guest
Oct 22nd, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.55 KB | None | 0 0
  1. ################################
  2. #         Cron on boot         #
  3. ################################
  4.  
  5. echo "-------------" >> /volume1/Plex/plexbootlog.log
  6. echo $(date): Sleep 30 seconds >> /volume1/Plex/plexbootlog.log
  7. sleep 30
  8. echo $(date): Waking up >> /volume1/Plex/plexbootlog.log
  9.  
  10. # To not log use >/dev/null 2>&1
  11.  
  12. echo $(date): attempting encfs local-sorted >> /volume1/Plex/plexbootlog.log
  13. #Mounting ENCFS, ENCFS6_CONFIG points to local config, --extpass="cat /passwordfilepath" so we do not have to enter a password to mount on boot
  14. #Mount encrypted local folder
  15. ENCFS6_CONFIG='/volume1/Plex/encfs/encfs.xml' /opt/bin/encfs --extpass="cat /volume1/Plex/encfs/encfspass" /volume1/Plex/.local-sorted /volume1/Plex/local-sorted >> /volume1/Plex/plexbootlog.log
  16.  
  17. echo $(date): attempting acdcli mount >> /volume1/Plex/plexbootlog.log
  18. export LIBFUSE_PATH="/lib/libfuse.so.2"
  19. echo "$(date): LIBFUSE_PATH set ($LIBFUSE_PATH)" >> /volume1/Plex/plexbootlog.log
  20. #--allow-other lets other linux users see encrypted acd dir, --modules="subdir,subdir=/dir" mounts specific acd directory
  21. /usr/local/bin/acd_cli mount -nl --allow-other --modules="subdir,subdir=/plex" /volume1/Plex/.acd-sorted/ >> /volume1/Plex/plexbootlog.log
  22.  
  23. echo $(date): attempting encfs acd-sorted >> /volume1/Plex/plexbootlog.log
  24. #Mounting ENCFS, ENCFS6_CONFIG points to local config, --extpass="cat /passwordfilepath" so we do not have to enter a password to mount on boot
  25. #Mount unencrypted acd folder
  26. ENCFS6_CONFIG='/volume1/Plex/encfs/encfs.xml' /opt/bin/encfs --extpass="cat /volume1/Plex/encfs/encfspass" /volume1/Plex/.acd-sorted /volume1/Plex/acd-sorted >> /volume1/Plex/plexbootlog.log
  27.  
  28. echo $(date): attempting unionfs mount >> /volume1/Plex/plexbootlog.log
  29. # -o allow_other lets my other linux users follow permissions so plex can see sorted
  30. # uid = user id (1024=admin), gid = group id (100=users)
  31. /opt/bin/unionfs -o cow -o allow_other,uid=1024,gid=100 /volume1/Plex/local-sorted=RW:/volume1/Plex/acd-sorted=RO /volume1/Plex/sorted/ >> /volume1/Plex/plexbootlog.log
  32.  
  33. # Make file so the cron jobs can run
  34. touch /tmp/allowcron
  35.  
  36. ################################
  37. #        Cron on timers        #
  38. ################################
  39.  
  40. #----- Plex - 20m - Sync ACD to sorted
  41. #Sync ACD to sorted every 20 minutes
  42. if [ -e /tmp/allowcron ]; then
  43.    flock -n /volume1/Plex/encfs/acdsync.lock -c '/usr/local/bin/acd_cli sync' >/dev/null 2>&1
  44. else
  45.    echo "$(date): allowcron not present: could not sync ACD to sorted" >> /volume1/Plex/plexbootlog.log
  46. fi
  47.  
  48. #----- Plex - 30m - Check if DB is ok
  49. #30 minutes check to ensure ACD is mounted/node db not corrupt. Source: http://disq.us/p/1ckpl2e
  50. if [ -e /tmp/allowcron ]; then
  51.    flock -n /volume1/Plex/encfs/acdcheck.lock -c 'bash /root/acdcheck' >> /volume1/Plex/plexbootlog.log
  52. else
  53.    echo "$(date): allowcron not present: could not check if DB is ok" >> /volume1/Plex/plexbootlog.log
  54. fi
  55.  
  56. #----- Plex - 1h - Upload
  57. #Hourly upload, flock ensures we do not run again if already running
  58. if [ -e /tmp/allowcron ]; then
  59.    flock -n /volume1/Plex/encfs/acdupload.lock -c '/usr/local/bin/acd_cli upload /volume1/Plex/.local-sorted/* /plex/' >/dev/null 2>&1
  60. else
  61.    echo "$(date): allowcron not present: could not upload" >> /volume1/Plex/plexbootlog.log
  62. fi
  63.  
  64. #----- Plex - 6h - Delete local folders
  65. #Delete local folders that are empty last modified 14 days ago, runs every 6 hours.
  66. if [ -e /tmp/allowcron ]; then
  67.    find /volume1/Plex/.local-sorted/ -type d -empty -mtime +14 -exec rm -rf {} \; >/dev/null 2>&1
  68. else
  69.    echo "$(date): allowcron not present: could not delete local folders" >> /volume1/Plex/plexbootlog.log
  70. fi
  71.  
  72. #----- Plex - 6h - Delete local files
  73. #Delete local files last modified 14 days ago, runs every 6 hours.
  74. if [ -e /tmp/allowcron ]; then
  75.    find /volume1/Plex/.local-sorted/ -type f -mtime +14 -exec rm -rf {} \; >/dev/null 2>&1
  76. else
  77.    echo "$(date): allowcron not present: could not delete local files" >> /volume1/Plex/plexbootlog.log
  78. fi
  79.  
  80. #----- Plex - 7d - Remove trashed and deleted items
  81. #Sync ACD to sorted to remove trashed/deleted items from acd currently not used still uploading files, runs every 7 days
  82. if [ -e /tmp/allowcron ]; then
  83.    flock -n /volume1/Plex/encfs/acdrsync.lock -c '/usr/bin/rsync -r --delete --existing --ignore-existing /volume1/Plex/sorted/ /volume1/Plex/acd-sorted/' >/dev/null 2>&1
  84. else
  85.    echo "$(date): allowcron not present: could not remove trashed and deleted items" >> /volume1/Plex/plexbootlog.log
  86. fi
  87.  
  88. ################################
  89. #     acdcheck from above      #
  90. ################################
  91.  
  92. #!/bin/sh
  93. echo "$(date): Remount Initiated"
  94. appname=$(basename $0)
  95.  
  96. # Change this to the location of your Amazon Cloud Mountpoint
  97. acdmount="/volume1/Plex/.acd-sorted"
  98.  
  99. if [ $(ls -l $acdmount | grep -v '^total' | wc -l) -gt 0 ]; then
  100.     echo "$(date): Everything looks ok"
  101.     exit
  102. fi
  103.  
  104. if [ $(ps -Al | grep $appname | wc -l) -gt 2 ]; then
  105.     echo "$(date): Already Running! Count $(ps -Al | grep $appname | wc -l)"
  106.     exit
  107. fi
  108.  
  109. echo "$(date): Remounting"
  110.  
  111. /usr/local/bin/acd_cli umount "$acdmount"
  112. syncresult=$(/usr/local/bin/acd_cli sync 2>&1)
  113.  
  114. if [ $(echo $syncresult | grep -i error | wc -l) -gt 0 ]; then
  115.  
  116.     echo "$(date): Error with the DB"
  117.  
  118.     echo "$(date): Removing ~/.cache/acd_cli/nodes.db"
  119.     rm ~/.cache/acd_cli/nodes.db
  120.  
  121.     echo "$(date): acd_cli sync"
  122.     /usr/local/bin/acd_cli sync
  123.  
  124.     sleep 10
  125. fi
  126.  
  127. echo "syncresult: $syncresult"
  128.  
  129. /usr/local/bin/acd_cli -nl mount --allow-other --modules="subdir,subdir=/plex" "$acdmount"
  130.  
  131. echo "$(date): Remount Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement