Advertisement
Guest User

VPS Cron

a guest
Oct 22nd, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.62 KB | None | 0 0
  1. ################################
  2. #            CRON              #
  3. ################################
  4.  
  5. # This script is executed at boot. It will mount all necessary ACD_CLI folders.
  6. @reboot /home/cron/plex-boot.sh >> /home/plex/plexbootlog.log
  7.  
  8. # Minute   Hour      Day of Month     Month      Day of Week      Command    
  9. # (0-59)   (0-23)    (1-31)           (1-12)     (0-6)                
  10.  
  11.   #30 minutes check to ensure ACD is mounted/node db not corrupt. Source: http://disq.us/p/1ckpl2e
  12.   */30     *         *                *          *                /home/cron/plex-check.sh >> /home/plex/plexbootlog.log
  13.  
  14.   #Sync ACD to sorted every 20 minutes
  15.   */20     *         *                *          *                /home/cron/plex-sync.sh >> /home/plex/plexbootlog.log
  16.  
  17.   #1 day check: clean-up locally stored files that are allready on ACD
  18.   0        5         *                *          *                /home/cron/plex-cleanup.sh >> /home/plex/plexbootlog.log
  19.  
  20. ################################
  21. #         plex-boot.sh         #
  22. ################################
  23.  
  24. #!/bin/sh -e
  25. #
  26. # plex-boot.sh
  27. #
  28. # This script is executed at boot. It will mount all necessary folders.
  29. #
  30. # It is executed from crontab -e
  31.  
  32. # >> /home/plex/plexbootlog.log
  33.  
  34. echo "-------------"
  35. echo "$(date): Sleep 30 seconds"
  36. sleep 30
  37. echo "$(date): Waking up"
  38.  
  39. # To not log use >/dev/null 2>&1
  40.  
  41. echo "$(date): mounting /local-sorted (encfs)"
  42. #Mounting ENCFS, ENCFS6_CONFIG points to local config, --extpass="cat /passwordfilepath" so we do not have to enter a password to mount on boot
  43. #Mount encrypted local folders
  44. ENCFS6_CONFIG='/home/plex/encfs/encfs.xml' /usr/bin/encfs --extpass="cat /home/plex/encfs/encfspass" /home/plex/.local-sorted /home/plex/local-sorted
  45.  
  46. echo "$(date): mounting /.acd-sorted (ACDFuse)"
  47. #--allow-other lets other linux users see encrypted acd dir, --modules="subdir,subdir=/dir" mounts specific acd directory
  48. /usr/local/bin/acd_cli mount --allow-other --modules="subdir,subdir=/plex" /home/plex/.acd-sorted/
  49.  
  50. echo "$(date): mounting /acd-sorted (encfs)"
  51. #Mounting ENCFS, ENCFS6_CONFIG points to local config, --extpass="cat /passwordfilepath" so we do not have to enter a password to mount on boot
  52. #Mount unencrypted acd folder
  53. ENCFS6_CONFIG='/home/plex/encfs/encfs.xml' /usr/bin/encfs --extpass="cat /home/plex/encfs/encfspass" /home/plex/.acd-sorted /home/plex/acd-sorted
  54.  
  55. echo "$(date): mounting /sorted (unionfs)"
  56. # -o allow_other lets my other linux users follow permissions so plex can see sorted
  57. # uid = user id (112=plex), gid = group id (116=plex)
  58. /usr/bin/unionfs-fuse -o cow -o allow_other,uid=112,gid=116 /home/plex/local-sorted=RW:/home/plex/acd-sorted=RO /home/plex/sorted/
  59.  
  60. # Make file so the cron jobs can run
  61. touch /tmp/allowcron
  62. echo "$(date): /tmp/allowcron made"
  63. echo "$(date): boot process finished"
  64.  
  65. exit 0
  66.  
  67. ################################
  68. #         plex-check.sh        #
  69. ################################
  70.  
  71. #!/bin/sh -e
  72. #
  73. # plex-check.sh
  74. #
  75. # 30 minutes check to ensure ACD is mounted/node
  76. # db not corrupt. Source: http://disq.us/p/1ckpl2e
  77. #
  78. # flock ensures it's not executed twice.
  79. #
  80. # It is executed from crontab -e
  81.  
  82. if [ -e /tmp/allowcron ]; then
  83.    flock -n /home/plex/encfs/acdcheck.lock -c 'bash /usr/local/bin/acdcheck'
  84. else
  85.    echo "$(date): allowcron not present: could not acdcheck"
  86. fi
  87.  
  88. exit 0
  89.  
  90. #################################
  91. # acdcheck (from plex-check.sh) #
  92. #################################
  93.  
  94. #!/bin/sh
  95. echo "$(date): Remount Initiated $(date)"
  96. appname=$(basename $0)
  97.  
  98. # Change this to the location of your Amazon Cloud Mountpoint
  99. acdmount="/home/plex/.acd-sorted"
  100.  
  101. if [ $(ls -l $acdmount | grep -v '^total' | wc -l) -gt 0 ]; then
  102.     echo "$(date): Everything looks ok"
  103.     exit
  104. fi
  105.  
  106. if [ $(ps -Al | grep $appname | wc -l) -gt 2 ]; then
  107.     echo "$(date): Already Running! Count $(ps -Al | grep $appname | wc -l)"
  108.     exit
  109. fi
  110.  
  111. echo "$(date): Remounting"
  112.  
  113. /usr/local/bin/acd_cli umount "$acdmount"
  114. syncresult=$(/usr/local/bin/acd_cli sync 2>&1)
  115.  
  116. if [ $(echo $syncresult | grep -i error | wc -l) -gt 0 ]; then
  117.    
  118.     echo "$(date): Error with the DB"
  119.  
  120.     echo "$(date): Removing ~/.cache/acd_cli/nodes.db"
  121.     rm ~/.cache/acd_cli/nodes.db
  122.  
  123.     echo "$(date): acd_cli sync"
  124.     /usr/local/bin/acd_cli sync
  125.  
  126.     sleep 10
  127. fi
  128.  
  129. echo "syncresult: $syncresult"
  130.  
  131. /usr/local/bin/acd_cli -nl mount --allow-other --modules="subdir,subdir=/plex" "$acdmount"
  132.  
  133. echo "Remount Done $(date)"
  134.  
  135. ################################
  136. #         plex-sync.sh         #
  137. ################################
  138.  
  139. #!/bin/sh -e
  140. #
  141. # plex-sync.sh
  142. #
  143. # This script is executed every 20 minutes.
  144. # It will sync ACD data to sorted.
  145. #
  146. # flock ensures it's not executed twice.
  147. #
  148. # It is executed from crontab -e
  149.  
  150. if [ -e /tmp/allowcron ]; then
  151.    flock -n /home/plex/encfs/acdsync.lock -c '/usr/local/bin/acd_cli sync'
  152. else
  153.    echo "$(date): allowcron not present: could not acd_cli sync"
  154. fi
  155.  
  156. exit 0
  157.  
  158. ################################
  159. #        plex-cleanup.sh       #
  160. ################################
  161.  
  162. #!/bin/sh -e
  163. #
  164. # plex-cleanup.sh
  165. #
  166. # This script is executed every day.
  167. # Cleans up files stored locally that are already on ACD
  168. #
  169. # flock ensures it's not executed twice.
  170. #
  171. # It is executed from crontab -e
  172.  
  173. if [ -e /tmp/allowcron ]; then
  174.    # 22-10: might need a  >/dev/null 2>&1 at the end to surpress errors
  175.    flock -n /home/plex/encfs/acdcleanup.lock -c 'find /home/plex/.local-sorted/ -type f -exec rm -rf {} \; && find /home/plex/.local-sorted/ -type d -empty -exec rm -rf {} \;'
  176. else
  177.    echo "$(date): allowcron not present: could not cleanup local files"
  178. fi
  179.  
  180. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement