PicturesBackup3.save 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. #!/bin/bash
  2. ############
  3. ##
  4. ##THIS NEW SCRIPT WILL RESIDE ON TOMCAT AND DO THE FOLLOWING
  5. ##
  6. ##
  7. ##NEEDED UPGRADES
  8. ## Check if required servers are online
  9. ## If not, activate them via WOL, wait, anc check again
  10. ##
  11. ## 8/6/2019 - changed builtar to not use chown. Instead, use chgrp to change to familyshare
  12. ## MUST ensure sync function works with this change on ENTERPRISE!!
  13. ## This eliminates need for sudo to run script
  14. ##
  15. ## ADD command -v wol2 to check if script is installed
  16. ##
  17. ## use wol2 to start and stop enterprise
  18. ##
  19. ## Build debian to place this script in either:
  20. ## - /.local for user
  21. ## - /usr/local/bin for global
  22. ## Symlink to /usr/local/bin for ease of use
  23. ##
  24. ## Interactive Menu Mode:
  25. ##
  26. ## If no flags passed, start interactive mode with promopts for:
  27. ## - What needs to be done (build tar, sync, or clean)
  28. ## - Based on result, what source and destintation (if syncing or building tar)
  29. ## -
  30. ##
  31. #############
  32. ## DEFINE VARIABLES ##
  33. DATE=$(date +%b-%d-%T)
  34. VERSION=0.04
  35. LOCALARCHIVEPATH=/storage/backup
  36. #PIHOMEPATHTOFILES=home/pi/ ## Folder containing data that will be backed up
  37. PIHOMEPATHTOFILES=home/pi/scripts ## USE THIS PROFILE ONLY FOR TESTING- ARCHIVES ONLY A SMALL FOLDER
  38. PIHOMEFOLDER=PiHomeBackup ## Name of Folder that will store the archives
  39. PIHOMEFILENAME=PiHomeBackup-$DATE.tar.gz ## Filename structure for archives
  40. #OWNCLOUDPATHTOFILES=storage/owncloud/ ## Folder containing data that will be backed up
  41. OWNCLOUDPATHTOFILES=storage/owncloud/data/mike/files/Network ## Folder containing data that will be backed up
  42. OWNCLOUDFOLDER=PiOwncloudBackup ## Name of Folder that will store the archives
  43. OWNCLOUDFILENAME=PiOwncloudArchive-$DATE.tar.gz ## Filename structure for archives
  44. ENTERPRISESTORAGE=/storage/backup/
  45. ENTERPRISESHARE=/storage/ENTERPRISESHARE
  46. PCSHARE=/storage/PCSHARE
  47. PATHTOLOG=/var/log/TOMCAT.log
  48. VERBOSITY=2 ## Default Verbosity Normal (Debug Level 1 -C flag, Critical Level 3 -S flag)
  49. LOGLEVEL=1
  50. PIHOMEKEEPFILES=4 ## Number of local backups that will be kept after cleaning
  51. OWNCLOUDKEEPFILES=10 ## Number of local backups that will be kept after cleaning
  52. ## Arrays for servers to sync ##
  53. ## arrNAME=(Common Name, IP, remote folder to mount, local location for mount, username, password) ##
  54. arrENTERPRISE=(ENTERPRISE 192.168.100.27 /backup $ENTERPRISESHARE mike cherokee2)
  55. arrPC=(PC 192.168.100.51 /Mavrick_Pictures $PCSHARE NetworkUser cherokee2)
  56. ## FLAGS ##
  57. FLAGBUILD=0
  58. FLAGSYNC=0
  59. FLAGHOME=0
  60. FLAGOWNCLOUD=0
  61. FLAGPICTURES=0
  62. FLAGPHONE=0
  63. FLAGCLEAN=0 ## If 0, -C must be called to clean after archiving. If 1, cleaning will occur automatically
  64. FLAGTESTING=0 ## TEST MODE ACTIVE IF 1 OR HIGHER ##
  65. ## END VARIABLES ##
  66. ## BEGIN FUNCTIONS ##
  67. addtolog () {
  68. ####LOG LEVELS####
  69. # Log levels:
  70. # 0 - Write to log file only
  71. # 1 - Write to Log file and screen at debug verbosity level (useful for debugging)
  72. # 2 - Write to Log file and screen at Normal Verbosity Level
  73. # 3 - Write to Log File and screen at CRITICAL Verbosity Level
  74. # default - level 1 (debgug verbosity)
  75. ##################
  76. if [ -z "$2" ]; then
  77. intLOGLEVEL="2" #if no variable passed, assume level 2
  78. else
  79. intLOGLEVEL="$2" #otherwise, use variable passed
  80. fi
  81. # echo "$intLOGLEVEL $VERBOSITY message: $1"
  82. DATESTAMP=$(date +%b-%d-%T)
  83. echo "$DATESTAMP | $USER | $1" >> $PATHTOLOG
  84. if [ $intLOGLEVEL -gt "0" ]; then #If verbosity level is greater than zero, pass message to write()
  85. write "$1" $intLOGLEVEL
  86. # echo "wrote $1"
  87. fi
  88. }
  89. write () {
  90. ##This function will write to the terminal screen based on the set VERBOSITY level##
  91. ##This function takes arguments [string] and intVERBOSITY"
  92. ## NEW PROTOCOL
  93. ##
  94. ## default verbosity (set by VERBOSITY flag = 2 (Normal Verbosity)
  95. ##
  96. ## -C argument sets verbosity to 0 (debug Verbosity) (show all higher than 0)
  97. ## -S argument sets verbosity to 3 (Silent / Only Critical Verbosity)
  98. ##
  99. ## Level 0 - Not used
  100. ## Level 1 - Debug Messages
  101. ## Level 2 - Normal (default)
  102. ## Level 3 - Critical
  103. ##
  104. ########
  105. if [ -z "$2" ]; then ##if no verbosity level is given, assume level 2 (default)
  106. intVERBOSE="2"
  107. else
  108. intVERBOSE="$2" ##Otherwise, use the provided verbosity level"
  109. fi
  110. # echo "write got $1 at $intVERBOSE with $VERBOSITY"
  111. if [ $intVERBOSE -ge $VERBOSITY ]; then
  112. echo "$1"
  113. fi
  114. }
  115. buildtar () {
  116. addtolog "buildtar: buildtar started for $1."
  117. case $1 in
  118. PiHome)
  119. FILENAME=$PIHOMEFOLDER/$PIHOMEFILENAME
  120. PATHTOFILES=$PIHOMEPATHTOFILES
  121. KEEPFILES=$PIHOMEKEEPFILES #Number of archives to keep on TOMCAT
  122. ;;
  123. Owncloud)
  124. FILENAME=$OWNCLOUDFOLDER/$OWNCLOUDFILENAME
  125. PATHTOFILES=$OWNCLOUDPATHTOFILES
  126. KEEPFILES=$OWNCLOUDKEEPFILES #Number of archives to keep on TOMCAT
  127. ;;
  128. esac
  129. if [ ! $FLAGTESTING -gt "0" ]; then
  130. tar -czf $LOCALARCHIVEPATH/$FILENAME -C / $PATHTOFILES 2>> $PATHTOLOG
  131. if [ "$?" = "0" ]; then
  132. addtolog "buildtar: successfully archived $PATHTOFILES to $LOCALARCHIVEPATH/$FILENAME"
  133. else
  134. addtolog "buildtar: FAILED to build .tar for $1."
  135. fi
  136. else
  137. addtolog "buildtar: TEST MODE: .tar not built"
  138. addtolog "buildtar: SIMULATED backup of $PATHTOFILES"
  139. addtolog "buildtar: SIMULATED archive built $LOCALARCHIVEPATH/$FILENAME"
  140. fi
  141. if [ ! $FLAGTESTING -gt 0 ]; then
  142. if [ "$EUID" -ne 0 ]; then
  143. chgrp familyshare $LOCALARCHIVEPATH/$FILENAME && addtolog "buildtar: set permissions on $FILENAME" || addtolog "buildtar: FAILED to set permissions on $FILENAME"
  144. else
  145. chown pi:familyshare $LOCALARCHIVEPATH/$FILENAME && addtolog "buildtar: set permissions on $FILENAME from root" || addtolog "buildtar: FAILED to change permissions on $FILENAME"
  146. fi
  147. else
  148. addtolog "Test Mode - File Permission change not attempted"
  149. fi
  150. if [ $FLAGCLEAN -gt "0" ]; then
  151. cleanfiles $1 $KEEPFILES
  152. fi
  153. }
  154. mountfiles () {
  155. addtolog "mountfiles: mountfiles started for $1."
  156. if mount | grep $4 > /dev/null; then
  157. addtolog "mountfiles: $1 files already mounted."
  158. else
  159. # sudo mount -t cifs //$2$3 $4 -o username=$5,password=$6 >> $PATHTOLOG 2>&1 &&
  160. # addtolog "mountfiles: successfully mounted $1 files." ||
  161. # addtolog "mountfiles: FAILED to mount $1." & failscript
  162. sudo mount -t cifs //$2$3 $4 -o username=$5,password=$6 >> $PATHTOLOG 2>&1
  163. if sudo mountpoint -q $4; then
  164. addtolog "mountfiles: successfully mounted $1 files."
  165. else
  166. addtolog "mountfiles: FAILED to mount $1." & failscript
  167. fi
  168. fi
  169. }
  170. unmountfiles () {
  171. addtolog "unmountfiles: unmountfiles started for $1."
  172. if mount | grep $4 > /dev/null; then
  173. sudo umount $4 &&
  174. addtolog "unmountfiles: successfully unmounted $1 files." ||
  175. addtolog "unmountfiles: FAILED to unmount $1"
  176. else
  177. addtolog "unmountfiles: $1 Files already unmounted."
  178. fi
  179. }
  180. checkpackage () {
  181. ## This script is used to check for the installation of other packages / scripts required
  182. if [ -z "$1" ]; then
  183. addtolog "checkpackages: Package Variable to received. Exiting" 3
  184. failscript
  185. else
  186. command -v $1 1>/dev/null 2>&1 && PACKAGEEXISTS=1 return 0 || PACKAGEEXISTS=0 return 1
  187. addtolog "checkpackages: Package $1 - flag set to $PACKAGEEXISTS" 1
  188. fi
  189. }
  190. cleanfiles () {
  191. #####
  192. ## This Function will be run manually, or automatically after Tomcat builds a new Owncloud backup .tar file
  193. ## This Funtion take the following arguments:
  194. ## 1 - Name of server to clean (currently Enterprise or Owncloud)
  195. ## 2 - (optional) number of files to keep (default 4)
  196. ####
  197. if [ -z "$2" ]; then # If argument 2 is not set
  198. filestokeep=4 # Default to 4
  199. else
  200. filestokeep="$2" # Otherwise use argument
  201. fi
  202. addtolog "cleanfiles: cleanfiles started for $1. Keeping $filestokeep files."
  203. case $1 in
  204. Enterprise)
  205. ##This is a future upgrade
  206. ##Endure share is mounted
  207. ##Sort and delete files
  208. ##Log files deleted
  209. addtolog "Enterprise cleanfiles not available at this time." 3
  210. ;;
  211. PiHome)
  212. cleanpath="$LOCALARCHIVEPATH/$PIHOMEFOLDER"
  213. ;;
  214. Owncloud)
  215. cleanpath="$LOCALARCHIVEPATH/$OWNCLOUDFOLDER"
  216. ;;
  217. *)
  218. addtolog "Cannot call cleanfiles with $1" 3
  219. failscript
  220. esac
  221. arrFiles=($(ls -t -- $cleanpath/*.tar.gz 2>/dev/null))
  222. totalfiles=${#arrFiles[@]}
  223. addtolog "cleanfiles: found $totalfiles files in $cleanpath"
  224. if [ "$totalfiles" -le "$filestokeep" ]
  225. then
  226. addtolog "cleanfiles will not delete files on $cleanpath. Total Files is less than or equal to $filestokeep"
  227. else
  228. filestoremove=$((totalfiles - filestokeep))
  229. addtolog "cleanfiles will attempt to remove the oldest $filestoremove files"
  230. for index in "${!arrFiles[@]}"
  231. do
  232. if [ "$index" -gt "$((filestokeep - 1))" ]; then
  233. #Array is 0 based, so -1
  234. if [ $FLAGTESTING -lt "1" ]; then #Do Nothing if Testing flag is set true
  235. rm -f ${arrFiles[$index]}
  236. addtolog "cleanfiles: DELETED: ${arrFiles[$index]}"
  237. else
  238. addtolog "cleanfiles: TEST MODE | SIMULATED DELETE: ${arrFiles[$index]}"
  239. fi
  240. fi
  241. done
  242. fi
  243. }
  244. syncfiles () {
  245. addtolog "syncfiles: syncfiles started for $1."
  246. # determine server shares needed and file paths
  247. SOURCEPATH=$LOCALARCHIVEPATH
  248. DESTPATH=$ENTERPRISESHARE
  249. case $1 in
  250. PiHome)
  251. SOURCEPATH=${SOURCEPATH}/$PIHOMEFOLDER/$PIHOMEFILENAME
  252. DESTPATH=${DESTPATH}/TOMCAT/PiHomeDir/
  253. # DESTPATH=/home/pi/
  254. mountfiles ${arrENTERPRISE[@]}
  255. ;;
  256. Owncloud)
  257. SOURCEPATH=${SOURCEPATH}/$OWNCLOUDFOLDER/
  258. DESTPATH=${DESTPATH}/TOMCAT/Owncloud/
  259. mountfiles ${arrENTERPRISE[@]}
  260. ;;
  261. Pictures)
  262. SOURCEPATH=$PCSHARE/
  263. DESTPATH=${DESTPATH}/Pictures/
  264. mountfiles ${arrENTERPRISE[@]}
  265. mountfiles ${arrPC[@]}
  266. ;;
  267. phone)
  268. SOURCEPATH=/storage/backup/phone/mike/
  269. DESTPATH=${DESTPATH}/TOMCAT/phone/mike/
  270. mountfiles ${arrENTERPRISE[@]}
  271. esac
  272. # Perform rsync
  273. # echo "$DATE | syncfiles: I will rsync $SOURCEPATH to $DESTPATH"
  274. addtolog "syncfiles: syncing $SOURCEPATH to $DESTPATH." 3
  275. rsync -avP --no-o --no-g --stats --progress --chown=mike:mike $SOURCEPATH $DESTPATH 2>> $PATHTOLOG &&
  276. ## rsync modified 2/9/2019 to remove -z flag. This will eliminate rsync compression
  277. ## and should lead to faster trasnfers
  278. addtolog "syncfiles: successfully rsynced $SOURCEPATH to $DESTPATH." 3 ||
  279. addtolog "syncfiles: FAILED to rsync $SOURCEPATH to $DESTPATH." 3
  280. unmountfiles ${arrENTERPRISE[@]}
  281. unmountfiles ${arrPC[@]}
  282. }
  283. checkusersudo () {
  284. ## For Some operations, user must have sudo privileges
  285. ## This will check for sudo
  286. ## and stop script if user need additional privileges
  287. if [ "$EUID" -ne 0 ]; then
  288. MESSAGE="Must run as sudo. This program requires sudo privileges to archive Owncloud files."
  289. addtolog "$MESSAGE" 3
  290. failscript
  291. fi
  292. }
  293. startscript() {
  294. #Prepare Log file for new execution#
  295. addtolog "---------------------------------------------------------" 0
  296. addtolog "$0 Script starting" 3
  297. }
  298. failscript() {
  299. addtolog "$0 script failed. Exiting." 3
  300. addtolog "--------------------------------------------------------" 0
  301. exit 1
  302. }
  303. endscript() {
  304. addtolog "$0 script complete. Exiting." 3
  305. addtolog "---------------------------------------------------------" 0
  306. exit 0
  307. }
  308. ###############
  309. # HELP FILE #
  310. ###############
  311. display_help() {
  312. echo "This script will build .tar archives of the Pi Home Directory and Owncloud files, and can sync archives and PC pictures folder to ENTERPRISE."
  313. echo " "
  314. echo " -h This help file"
  315. echo " -B, Build .tar files"
  316. echo " -S, Sync files between machines"
  317. echo " -C, Cleanup. Used to manually clean acrchives. Normally automatic with -B or -S"
  318. echo " -p, Perform actions on PiHome Directory"
  319. echo " -o, Perform actions on Owncloud"
  320. echo " -i, Perform actions on Photos"
  321. echo " -t, Perform actions on Phone Backup"
  322. echo " -T, Testing Mode. No Deletions"
  323. echo " -m, Mount filesystems only (used for testing)"
  324. echo " -u, Unmount filesystems only (used for testing)"
  325. echo " -s, Silent Mode. Supress all output except errors."
  326. echo " -V, Verbose Mode. Show all debugging on screen."
  327. echo " -v, Version. Display Version information."
  328. echo
  329. addtolog "$0 help menu displayed." 0
  330. }
  331. ##############
  332. # END HELP #
  333. ##############
  334. trap ctrl_c 2 3 6 15
  335. ctrl_c () {
  336. addtolog "SIGINT CTRL_C. Unmounting files and stopping." 3
  337. addtolog "Aborting Script. Waiting for processes to finish." 3
  338. sleep 2
  339. addtolog "Unmounting file shares" 3
  340. sleep 2
  341. unmountfiles ${arrENTERPRISE[@]} && addtolog "Enterpise unmounted" || addtolog "Failed to unmount ENTERPRISE" 3
  342. unmountfiles ${arrPC[@]} && addtolog "PC unmounted" || addtolog "Failed to unmount PC" 3
  343. addtolog "$0 Script has stopped. Exiting." 3
  344. exit 1
  345. }
  346. startscript
  347. startlog="$0 script starting with the commands:"
  348. options=':hBSCpoitmusTVv'
  349. while getopts $options option
  350. do
  351. case $option in
  352. h ) startlog="$startlog help menu"
  353. addtolog "$startlog"
  354. display_help
  355. endscript;;
  356. s ) startlog="$startlog silent mode "
  357. VERBOSITY=0;;
  358. V ) startlog="$startlog verbose mode "
  359. VERBOSITY=2;;
  360. B ) FLAGBUILD=1
  361. startlog="$startlog BUILD ";;
  362. S ) FLAGSYNC=1
  363. echo $index
  364. echo $index
  365. echo $index
  366. echo $index
  367. echo $index
  368. startlog="$startlog SYNC ";;
  369. C ) startlog="$startlog CLEANUP "
  370. FLAGCLEAN=1;;
  371. p ) FLAGHOME=1
  372. startlog="$startlog PiHome ";;
  373. o ) FLAGOWNCLOUD=1
  374. startlog="$startlog owncloud ";;
  375. i ) FLAGPICTURES=1
  376. startlog="$startlog PC Pictures ";;
  377. t ) FLAGPHONE=1
  378. startlog="$startlog, Phone Backup ";;
  379. T ) FLAGTESTING=1
  380. startlog="$startlog TESTING MODE";;
  381. m ) startlog="$startlog mount files only"
  382. echo "mounting"
  383. addtolog "$startlog"
  384. mountfiles ${arrENTERPRISE[@]}
  385. mountfiles ${arrPC[@]}
  386. endscript;;
  387. u ) startlog="$startlog unmount files only"
  388. addtolog "$startlog"
  389. unmountfiles ${arrENTERPRISE[@]}
  390. unmountfiles ${arrPC[@]}
  391. endscript;;
  392. v ) startlog="$startlog, Display Version - $VERSION"
  393. echo "$0 - Version $VERSION"
  394. # addtolog "$startlog"
  395. # endscript
  396. exit 0;;
  397. * ) display_help
  398. endscript ;;
  399. /? ) addtolog "unknown menu option $1. Exiting." 3; exit 1;;
  400. esac
  401. done
  402. addtolog "$startlog" 3
  403. ####################
  404. # DONE ARGUMENTS #
  405. ####################
  406. if [ $FLAGTESTING -gt "0" ]; then
  407. VERBOSITY=0
  408. addtolog "**TESTING MODE ACTIVE**" 3
  409. addtolog "Verbose mode implied by testing mode" 3
  410. echo "Set Verbosity to $VERBOSITY"
  411. # if [ $FLAGCLEAN -gt "0" ]; then
  412. # cleanfiles Owncloud $OWNCLOUDKEEPFILES
  413. # fi
  414. fi
  415. addtolog "Test Versoty message" 1
  416. checkpackage wol3 || addtolog "Wol2 Package not found. Needed for sync operations." 1
  417. ## Check SYNC or BUILD flags have appropriate action##
  418. if [ $FLAGSYNC -eq "1" -o $FLAGBUILD -eq "1" ]; then
  419. if ! [ $FLAGHOME -eq "1" -o $FLAGOWNCLOUD -eq "1" -o $FLAGPICTURES -eq "1" -o $FLAGPHONE -eq "1" ]; then
  420. addtolog "FAIL: FLAGSYNC or FLAGBUILD did not have -p,o,i, or t action declared. See -h for help." 3
  421. exit 1
  422. fi
  423. fi
  424. ## Check PiHome Flag ##
  425. if [ $FLAGHOME -eq "1" ]; then
  426. ##Perform actiong on PiHome Directory
  427. addtolog "Perform Actions on PiHomeDirectory" 1
  428. if [ $FLAGBUILD -eq "1" ]; then
  429. addtolog "Perform Tar Build on PiHomeDirectory" 1
  430. buildtar PiHome
  431. fi
  432. if [ $FLAGSYNC -eq "1" ]; then
  433. addtolog "Perform Sync on PiHomeDirectory" 1
  434. ##PERFORM ACTIONS TO SYNC FILES##
  435. syncfiles PiHome
  436. fi
  437. fi
  438. ## Check Owncloud Flag ##
  439. if [ $FLAGOWNCLOUD -eq "1" ]; then
  440. ##Perform action on Owncloud Directory
  441. if [ $FLAGBUILD -eq "1" ]; then
  442. checkusersudo && buildtar Owncloud
  443. fi
  444. if [ $FLAGSYNC -eq "1" ]; then
  445. #echo "Perform Sync on Owncloud Directory"
  446. ##PERFORM ACTIONS TO SYNC FILES##
  447. syncfiles Owncloud
  448. fi
  449. fi
  450. ## Check Pictures Flag ##
  451. if [ $FLAGPICTURES -eq "1" ]; then
  452. ##Perform action on Pictures Directory
  453. #echo "Perform Actions on Pictrues Directory"
  454. if [ $FLAGBUILD -eq "1" ]; then
  455. addtolog "Cannot Perform Compression of Pictures. Sync only. Exiting." 3
  456. endscript
  457. fi
  458. if [ $FLAGSYNC -eq "1" ]; then
  459. #echo "Perform Sync on Pictures"
  460. ##PERFORM ACTIONS TO SYNC FILES##
  461. syncfiles Pictures
  462. fi
  463. fi
  464. ## Check Phone Flag ##
  465. if [ $FLAGPHONE -eq "1" ]; then
  466. ##Perform actiong on Phone Directory
  467. #echo "Perform Actions on Phone Backup Directory"
  468. if [ $FLAGBUILD -eq "1" ]; then
  469. addtolog "Cannot build archive of Phone Backup. Sync only. Exiting." 3
  470. endscript
  471. fi
  472. if [ $FLAGSYNC -eq "1" ]; then
  473. # echo "Cannot Sync Phone Backup Files. FUTURE UPGRADE"
  474. syncfiles phone
  475. fi
  476. fi
  477. endscript