Guest User

Untitled

a guest
Nov 9th, 2012
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.02 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ##
  4. ## Initial and continous ZFS filesystems replication
  5. ##
  6.  
  7. PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
  8.  
  9. if [ ! `whoami` = "root" ]; then
  10. echo "You need to be root."
  11. exit
  12. fi
  13.  
  14.  
  15. l_old="@local_replicate.base"
  16. r_old="@remote_replicate.base"
  17. l_new="@local_replicate.delta"
  18. r_new="@remote_replicate.delta"
  19. log="/var/log/replicate.log"
  20. cmd=$(mktemp /tmp/replicate.job.XXXXXX)
  21. pid="/var/run/replicate.pid"
  22. path="/usr/local/etc/replicatejobs/"
  23. jobs=`find $path -type f ! -iname sample_* ! -iname noauto_* | sort -d`
  24.  
  25. ##
  26. ## Mail settings
  27. ##
  28. subject="A replication error has occurred!"
  29. address=""
  30. mail=$(mktemp /tmp/replicate.mail.XXXXXX)
  31.  
  32. usage()
  33. {
  34. echo ""
  35. echo "Usage: `basename $0` [Options] [Flags] [Job]"
  36. echo ""
  37. echo "Options: -c|-h"
  38. echo "-c: Clean."
  39. echo " removes replicated filesystems and snapshot(s)."
  40. echo "-h: Usage."
  41. echo ""
  42. echo "Flags: auto"
  43. echo "auto: Clean auto."
  44. echo " removes all scheduled snapshot(s)."
  45. echo ""
  46. echo "Examples: `basename $0` | `basename $0` jobname"
  47. echo " `basename $0` -c | `basename $0` -c jobname | `basename $0` -c auto"
  48. echo ""
  49. }
  50.  
  51. l_base()
  52. {
  53. ##
  54. ## Makes local baseline replication
  55. ##
  56.  
  57. if zpool status `echo $lfs | cut -f1 -d /` | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  58. echo "`date`: A Scrub or Resilver is currently in progress on source pool, aborting." | tee -a /var/log/messages >> $log
  59. echo "" >> $log
  60. exit 1
  61. elif zpool status $lp | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  62. echo "`date`: A Scrub or Resilver is currently in progress on destination pool, aborting." | tee -a /var/log/messages >> $log
  63. echo "" >> $log
  64. exit 1
  65. fi
  66.  
  67. echo "echo \"\`date\`: Beginning local baseline replication sequence on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  68.  
  69. # Take initial snapshot(s):
  70. echo "sudo zfs snapshot $lfs$l_old 2>> ${cmd}" >> ${cmd}
  71. echo errorcheck >> ${cmd}
  72. echo "echo \"\`date\`: Initial snapshot(s) created\" >> $log" >> ${cmd}
  73.  
  74. # Replicate data:
  75. echo "sudo zfs send $lfs$l_old | sudo zfs recv -Fd $lp 2>> ${cmd}" >> ${cmd}
  76. echo errorcheck >> ${cmd}
  77. echo "echo \"\`date\`: Data replicated\" >> $log" >> ${cmd}
  78.  
  79. echo "echo \"\`date\`: Local baseline replication sequence finished on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  80. echo "" >> ${cmd}
  81. echo "" >> $log
  82. }
  83.  
  84. r_base()
  85. {
  86. ##
  87. ## Makes remote baseline replication
  88. ##
  89.  
  90. if [ `ping -c 1 -W 1000 $rh | grep -oe "[0-9] packets received" | awk '{print $1}'` -eq "0" ]; then
  91. echo "`date`: Remote baseline replication sequence aborted on \"$lfs\"! No response from \"$rh\"." | tee -a /var/log/messages >> $log
  92. echo "" >> $log
  93. exit 1
  94. fi
  95. if zpool status `echo $lfs | cut -f1 -d /` | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  96. echo "`date`: A Scrub or Resilver is currently in progress on source pool, aborting." | tee -a /var/log/messages >> $log
  97. echo "" >> $log
  98. exit 1
  99. elif su replicator -c "ssh $rh zpool status $rp | grep \"scan:\" | egrep -qo '(scrub in progress|resilver in progress)'"; then
  100. echo "`date`: A Scrub or Resilver is currently in progress on remote pool, aborting." | tee -a /var/log/messages >> $log
  101. echo "" >> $log
  102. exit 1
  103. fi
  104.  
  105. echo "echo \"\`date\`: Beginning remote baseline replication sequence on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  106.  
  107. # Take initial snapshot(s):
  108. echo "sudo zfs snapshot $lfs$r_old 2>> ${cmd}" >> ${cmd}
  109. echo errorcheck >> ${cmd}
  110. echo "echo \"\`date\`: Initial snapshot(s) created\" >> $log" >> ${cmd}
  111.  
  112. # Replicate data:
  113. echo "sudo zfs send $lfs$r_old | ssh $rh sudo zfs recv -dF $rp/$lh 2>> ${cmd}" >> ${cmd}
  114. echo errorcheck >> ${cmd}
  115. echo "echo \"\`date\`: Data replicated\" >> $log" >> ${cmd}
  116.  
  117. echo "echo \"\`date\`: Remote baseline replication sequence finished on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  118. echo "" >> ${cmd}
  119. echo "" >> $log
  120. }
  121.  
  122. l_inc()
  123. {
  124. ##
  125. ## Makes local incremental replication
  126. ##
  127.  
  128. if zpool status `echo $lfs | cut -f1 -d /` | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  129. echo "`date`: A Scrub or Resilver is currently in progress on source pool, aborting." | tee -a /var/log/messages >> $log
  130. echo "" >> $log
  131. exit 1
  132. elif zpool status $lp | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  133. echo "`date`: A Scrub or Resilver is currently in progress on destination pool, aborting." | tee -a /var/log/messages >> $log
  134. echo "" >> $log
  135. exit 1
  136. fi
  137.  
  138. tfs=`echo $lfs | cut -f 2-512 -d / | sed "s/^/$lp\//"`
  139.  
  140. echo "echo \"\`date\`: Beginning local incremental replication sequence on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  141.  
  142. # Take new source snapshot(s):
  143. echo "sudo zfs snapshot $lfs$l_new 2>> ${cmd}" >> ${cmd}
  144. echo errorcheck >> ${cmd}
  145. echo "echo \"\`date\`: New snapshot(s) created\" >> $log" >> ${cmd}
  146.  
  147. # Replicate data:
  148. echo "sudo zfs send -i $lfs$l_old $lfs$l_new | sudo zfs recv -Fd $lp 2>> ${cmd}" >> ${cmd}
  149. echo errorcheck >> ${cmd}
  150. echo "echo \"\`date\`: Data replicated\" >> $log" >> ${cmd}
  151.  
  152. # Destroy target .old snapshot(s):
  153. echo "sudo zfs destroy $tfs$l_old 2>> ${cmd}" >> ${cmd}
  154. echo errorcheck >> ${cmd}
  155. echo "echo \"\`date\`: Target .base snapshot(s) destroyed\" >> $log" >> ${cmd}
  156.  
  157. # Rename target .new snapshot(s) .old:
  158. echo "sudo zfs rename $tfs$l_new $tfs$l_old 2>> ${cmd}" >> ${cmd}
  159. echo errorcheck >> ${cmd}
  160. echo "echo \"\`date\`: Target .delta snapshot(s) renamed .base\" >> $log" >> ${cmd}
  161.  
  162. # Destroy source .old snapshot(s):
  163. echo "sudo zfs destroy $lfs$l_old 2>> ${cmd}" >> ${cmd}
  164. echo errorcheck >> ${cmd}
  165. echo "echo \"\`date\`: Source .base snapshot(s) destroyed\" >> $log" >> ${cmd}
  166.  
  167. # Rename source .new snapshot(s) .old:
  168. echo "sudo zfs rename $lfs$l_new $lfs$l_old 2>> ${cmd}" >> ${cmd}
  169. echo errorcheck >> ${cmd}
  170. echo "echo \"\`date\`: Source .delta snapshot(s) renamed .base\" >> $log" >> ${cmd}
  171.  
  172. echo "echo \"\`date\`: Local incremental replication sequence finished on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  173. echo "" >> ${cmd}
  174. echo "" >> $log
  175. }
  176.  
  177. r_inc()
  178. {
  179. ##
  180. ## Makes remote incremental replication
  181. ##
  182.  
  183. if [ `ping -c 1 -W 1000 $rh | grep -oe "[0-9] packets received" | awk '{print $1}'` -eq "0" ]; then
  184. echo "`date`: Remote incremental replication sequence aborted on \"$lfs\"! No response from \"$rh\"." | tee -a /var/log/messages >> $log
  185. echo "" >> $log
  186. exit 1
  187. fi
  188. if zpool status `echo $lfs | cut -f1 -d /` | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  189. echo "`date`: A Scrub or Resilver is currently in progress on source pool, aborting." | tee -a /var/log/messages >> $log
  190. echo "" >> $log
  191. exit 1
  192. elif su replicator -c "ssh $rh zpool status $rp | grep \"scan:\" | egrep -qo '(scrub in progress|resilver in progress)'"; then
  193. echo "`date`: A Scrub or Resilver is currently in progress on remote pool, aborting." | tee -a /var/log/messages >> $log
  194. echo "" >> $log
  195. exit 1
  196. fi
  197.  
  198. tfs=`echo $lfs | cut -f 2-512 -d / | sed "s/^/$rp\/$lh\//"`
  199.  
  200. echo "echo \"\`date\`: Beginning remote incremental replication sequence on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  201.  
  202. # Take new source snapshot(s):
  203. echo "sudo zfs snapshot $lfs$r_new 2>> ${cmd}" >> ${cmd}
  204. echo errorcheck >> ${cmd}
  205. echo "echo \"\`date\`: New snapshot(s) created\" >> $log" >> ${cmd}
  206.  
  207. # Replicate data:
  208. echo "sudo zfs send -i $lfs$r_old $lfs$r_new | ssh $rh sudo zfs recv -dF $rp/$lh 2>> ${cmd}" >> ${cmd}
  209. echo errorcheck >> ${cmd}
  210. echo "echo \"\`date\`: Data replicated\" >> $log" >> ${cmd}
  211.  
  212. # Destroy target .old snapshot(s):
  213. echo "ssh $rh sudo zfs destroy $tfs$r_old 2>> ${cmd}" >> ${cmd}
  214. echo errorcheck >> ${cmd}
  215. echo "echo \"\`date\`: Target .base snapshot(s) destroyed\" >> $log" >> ${cmd}
  216.  
  217. # Rename target .new snapshot(s) .old:
  218. echo "ssh $rh sudo zfs rename $tfs$r_new $tfs$r_old 2>> ${cmd}" >> ${cmd}
  219. echo errorcheck >> ${cmd}
  220. echo "echo \"\`date\`: Target .delta snapshot(s) renamed .base\" >> $log" >> ${cmd}
  221.  
  222. # Destroy source .old snapshot(s):
  223. echo "sudo zfs destroy $lfs$r_old 2>> ${cmd}" >> ${cmd}
  224. echo errorcheck >> ${cmd}
  225. echo "echo \"\`date\`: Source .base snapshot(s) destroyed\" >> $log" >> ${cmd}
  226.  
  227. # Rename source .new snapshot(s) .old:
  228. echo "sudo zfs rename $lfs$r_new $lfs$r_old 2>> ${cmd}" >> ${cmd}
  229. echo errorcheck >> ${cmd}
  230. echo "echo \"\`date\`: Source .delta snapshot(s) renamed .base\" >> $log" >> ${cmd}
  231.  
  232. echo "echo \"\`date\`: Remote incremental replication sequence finished on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  233. echo "" >> ${cmd}
  234. echo "" >> $log
  235. }
  236.  
  237. l_clean()
  238. {
  239. ##
  240. ## Cleans locally replicated file system
  241. ##
  242.  
  243. if zpool status `echo $lfs | cut -f1 -d /` | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  244. echo "`date`: A Scrub or Resilver is currently in progress on source pool, aborting." | tee -a /var/log/messages >> $log
  245. echo "" >> $log
  246. exit 1
  247. elif zpool status $lp | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  248. echo "`date`: A Scrub or Resilver is currently in progress on destination pool, aborting." | tee -a /var/log/messages >> $log
  249. echo "" >> $log
  250. exit 1
  251. fi
  252.  
  253. tfs=`echo $lfs | cut -f 2-512 -d / | sed "s/^/$lp\//"`
  254.  
  255. echo "echo \"\`date\`: Beginning local cleaning process on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  256.  
  257. if [ `zfs list -t snapshot | grep $lfs$l_old | wc -l` -eq "1" ]; then
  258. echo "sudo zfs destroy $lfs$l_old 2>> ${cmd}" >> ${cmd}
  259. echo errorcheck >> ${cmd}
  260. echo "echo \"\`date\`: Local base snapshot destroyed\" >> $log" >> ${cmd}
  261. fi
  262. if [ `zfs list -t snapshot | grep $lfs$l_new | wc -l` -eq "1" ]; then
  263. echo "sudo zfs destroy $lfs$l_new 2>> ${cmd}" >> ${cmd}
  264. echo errorcheck >> ${cmd}
  265. echo "echo \"\`date\`: Local delta snapshot destroyed\" >> $log" >> ${cmd}
  266. fi
  267. if [ `zfs list -t snapshot | grep $tfs$l_old | wc -l` -eq "1" ]; then
  268. echo "sudo zfs destroy $tfs$l_old 2>> ${cmd}" >> ${cmd}
  269. echo errorcheck >> ${cmd}
  270. echo "echo \"\`date\`: Target base snapshot destroyed\" >> $log" >> ${cmd}
  271. fi
  272. if [ `zfs list -t snapshot | grep $tfs$l_new | wc -l` -eq "1" ]; then
  273. echo "sudo zfs destroy $tfs$l_new 2>> ${cmd}" >> ${cmd}
  274. echo errorcheck >> ${cmd}
  275. echo "echo \"\`date\`: Target delta snapshot destroyed\" >> $log" >> ${cmd}
  276. fi
  277. if [ `zfs list | grep $tfs | wc -l` -gt "0" ]; then
  278. echo "sudo zfs destroy $tfs 2>> ${cmd}" >> ${cmd}
  279. echo errorcheck >> ${cmd}
  280. echo "echo \"\`date\`: Target filesystem destroyed\" >> $log" >> ${cmd}
  281. fi
  282.  
  283. echo "echo \"\`date\`: Local cleanup complete on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  284. echo "" >> ${cmd}
  285. echo "" >> $log
  286. }
  287.  
  288. r_clean()
  289. {
  290. ##
  291. ## Cleans remotely replicated file systems
  292. ##
  293.  
  294. if [ `ping -c 1 -W 1000 $rh | grep -oe "[0-9] packets received" | awk '{print $1}'` -eq "0" ]; then
  295. echo "`date`: Remote cleanup aborted on \"$lfs\"! No response from \"$rh\"." | tee -a /var/log/messages >> $log
  296. echo "" >> $log
  297. exit 1
  298. fi
  299. if zpool status `echo $lfs | cut -f1 -d /` | grep "scan:" | egrep -qo "(scrub in progress|resilver in progress)"; then
  300. echo "`date`: A Scrub or Resilver is currently in progress on source pool, aborting." | tee -a /var/log/messages >> $log
  301. echo "" >> $log
  302. exit 1
  303. elif su replicator -c "ssh $rh zpool status $rp | grep \"scan:\" | egrep -qo '(scrub in progress|resilver in progress)'"; then
  304. echo "`date`: A Scrub or Resilver is currently in progress on destination pool, aborting." | tee -a /var/log/messages >> $log
  305. echo "" >> $log
  306. exit 1
  307. fi
  308.  
  309. tfs=`echo $lfs | cut -f 2-512 -d / | sed "s/^/$rp\/$lh\//"`
  310.  
  311. echo "echo \"\`date\`: Beginning remote cleaning process on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  312.  
  313. if [ `zfs list -t snapshot | grep $lfs$r_old | wc -l` -eq "1" ]; then
  314. echo "sudo zfs destroy $lfs$r_old 2>> ${cmd}" >> ${cmd}
  315. echo errorcheck >> ${cmd}
  316. echo "echo \"\`date\`: Local base snapshot destroyed\" >> $log" >> ${cmd}
  317. fi
  318. if [ `zfs list -t snapshot | grep $lfs$r_new | wc -l` -eq "1" ]; then
  319. echo "sudo zfs destroy $lfs$r_new 2>> ${cmd}" >> ${cmd}
  320. echo errorcheck >> ${cmd}
  321. echo "echo \"\`date\`: Local delta snapshot destroyed\" >> $log" >> ${cmd}
  322. fi
  323. if [ `su replicator -c "ssh $rh zfs list -t snapshot | grep $tfs$r_old | wc -l"` -eq "1" ]; then
  324. echo "ssh $rh sudo zfs destroy $tfs$r_old 2>> ${cmd}" >> ${cmd}
  325. echo errorcheck >> ${cmd}
  326. echo "echo \"\`date\`: Target base snapshot destroyed\" >> $log" >> ${cmd}
  327. fi
  328. if [ `su replicator -c "ssh $rh zfs list -t snapshot | grep $tfs$r_new | wc -l"` -eq "1" ]; then
  329. echo "ssh $rh sudo zfs destroy $tfs$r_new 2>> ${cmd}" >> ${cmd}
  330. echo errorcheck >> ${cmd}
  331. echo "echo \"\`date\`: Target delta snapshot destroyed\" >> $log" >> ${cmd}
  332. fi
  333. if [ `su replicator -c "ssh $rh zfs list | grep $tfs | wc -l"` -gt "0" ]; then
  334. echo "ssh $rh sudo zfs destroy $tfs 2>> ${cmd}" >> ${cmd}
  335. echo errorcheck >> ${cmd}
  336. echo "echo \"\`date\`: Target filesystem destroyed\" >> $log" >> ${cmd}
  337. fi
  338.  
  339. echo "echo \"\`date\`: Remote cleanup complete on \"$lfs\"\" | tee -a /var/log/messages >> $log" >> ${cmd}
  340. echo "" >> ${cmd}
  341. echo "" >> $log
  342. }
  343.  
  344. recurse()
  345. {
  346. sed -i '' -e 's/zfs snapshot /zfs snapshot -r /' -e 's/zfs send /zfs send -R /' -e 's/zfs destroy /zfs destroy -r /' -e 's/zfs rename /zfs rename -r /' ${cmd}
  347. }
  348.  
  349. sanity()
  350. {
  351. if [ -e $pid ]; then
  352. echo "`date`: Started a new process, but the previous \"replicate\" is still running, aborting." | tee -a /var/log/messages >> $log
  353. echo "" >> $log
  354. rm ${cmd}; rm ${mail}; exit 1
  355. fi
  356. chown replicator ${mail}; chmod 600 ${mail}
  357. chown replicator ${cmd}; chmod 700 ${cmd}
  358. cat > ${cmd} << EOF
  359. #!/bin/sh
  360. errorcheck()
  361. {
  362. if [ "\$?" -ne "0" ]; then
  363. tail -2 ${cmd} >> $log
  364. tail -10 $log > ${mail}
  365. mail -s "$subject" $address < ${mail}
  366. cat /dev/null > ${mail}
  367. exit 1
  368. fi
  369. }
  370.  
  371. EOF
  372. }
  373.  
  374. autoclean()
  375. {
  376. ##
  377. ## Find and destroy all automatically taken snapshots
  378. ##
  379.  
  380. zfs list -t snapshot -o name | grep auto > ${cmd}
  381. cat ${cmd}
  382. sed -i '' 's/^/sudo zfs destroy /' ${cmd}
  383. }
  384.  
  385. allow()
  386. {
  387. sudoers="/usr/local/etc/sudoers"
  388.  
  389. if [ `grep "ssh" ${cmd} | wc -l` -gt "0" ]
  390. then
  391. allows=`grep zfs ${cmd} | cut -f 1 -d ">" | tr '|' '\n' | sed -e 's/^ //' -e 's/ 2$//' | sed -e 's/sudo //' -e 's/^ //' -e 's/^/replicator ALL=(ALL) NOPASSWD: \/sbin\//' | grep -v ssh`
  392. else
  393. allows=`grep zfs ${cmd} | cut -f 1 -d ">" | tr '|' '\n' | sed -e 's/^ //' -e 's/ 2$//' | sed -e 's/sudo //' -e 's/^ //' -e 's/^/replicator ALL=(ALL) NOPASSWD: \/sbin\//'`
  394. fi
  395. if [ `grep "replicator" $sudoers | wc -l` -gt "0" ]; then
  396. sed -i '' '/replicator/d' $sudoers
  397. fi
  398. echo "$allows" >> $sudoers
  399. }
  400.  
  401. disallow()
  402. {
  403. if [ `grep "replicator" $sudoers | wc -l` -gt "0" ]; then
  404. sed -i '' '/replicator/d' $sudoers
  405. fi
  406. }
  407.  
  408. ##
  409. ## Initial preparation
  410. ##
  411.  
  412. chmod 664 /var/log/messages
  413.  
  414. case "$1" in
  415.  
  416. "")
  417. ##
  418. ## If no jobname is specified, it will find all jobs in config directory and
  419. ## process them one by one
  420. ##
  421.  
  422. for job in $jobs
  423. do
  424. sanity
  425. . $job
  426. if [ -z "$rh" ]
  427. then
  428. if [ `zfs list -t snapshot | grep $lfs$l_old | wc -l` -eq "1" ]
  429. then
  430. if [ "$r" = "yes" ]
  431. then
  432. l_inc; recurse
  433. else
  434. l_inc
  435. fi
  436. else
  437. if [ "$r" = "yes" ]
  438. then
  439. l_base; recurse
  440. else
  441. l_base
  442. fi
  443. fi
  444. else
  445. if [ `zfs list -t snapshot | grep $lfs$r_old | wc -l` -eq "1" ]
  446. then
  447. if [ "$r" = "yes" ]
  448. then
  449. r_inc; recurse
  450. else
  451. r_inc
  452. fi
  453. else
  454. if [ "$r" = "yes" ]
  455. then
  456. r_base; recurse
  457. else
  458. r_base
  459. fi
  460. fi
  461. fi
  462. touch $pid
  463. allow
  464. su replicator -c ${cmd}
  465. disallow
  466. rm $pid
  467. done
  468. ;;
  469.  
  470. -c)
  471. ##
  472. ## When cleaning, if no jobname is specified, it will find all jobs in
  473. ## config directory and process them one by one
  474. ##
  475.  
  476. if [ "$2" = "auto" ]
  477. then
  478. else
  479. echo ""
  480. echo "Starting off by deleting replication snapshot(s) and cleaning the entire"
  481. echo "target pool zfs filesystem. This can take a while, depending on how much"
  482. echo "is stored."
  483. sleep 10
  484. fi
  485.  
  486. if [ "$2" = "" ]; then
  487. for job in $jobs
  488. do
  489. sanity
  490. . $job
  491. if [ -z "$rh" ]
  492. then
  493. if [ "$r" = "yes" ]
  494. then
  495. l_clean; recurse
  496. else
  497. l_clean
  498. fi
  499. else
  500. if [ "$r" = "yes" ]
  501. then
  502. r_clean; recurse
  503. else
  504. r_clean
  505. fi
  506. fi
  507. touch $pid
  508. allow
  509. su replicator -c ${cmd}
  510. disallow
  511. rm $pid
  512. done
  513. exit
  514. fi
  515.  
  516. ##
  517. ## Also when cleaning, if you type in shorthand jobname, it will only clean
  518. ## specified job
  519. ##
  520.  
  521. if [ `ls -1 $path | grep $2 | wc -l` -eq "1" ]; then
  522. sanity
  523. . $path$2
  524. if [ -z "$rh" ]
  525. then
  526. if [ "$r" = "yes" ]
  527. then
  528. l_clean; recurse
  529. else
  530. l_clean
  531. fi
  532. else
  533. if [ "$r" = "yes" ]
  534. then
  535. r_clean; recurse
  536. else
  537. r_clean
  538. fi
  539. fi
  540. touch $pid
  541. allow
  542. su replicator -c ${cmd}
  543. disallow
  544. rm $pid
  545. else
  546.  
  547. ##
  548. ## If you type "auto" when cleaning, it finds and destroys all snapshots with
  549. ## the word "auto" in them.
  550. ##
  551.  
  552. if [ "$2" = "auto" ]
  553. then
  554. sanity
  555. autoclean
  556. while true
  557. do
  558. read -r -p 'These snapshot(s) will destroyed. Confirm? "Y|n"' choice
  559. case "$choice" in
  560. y|Y) touch $pid
  561. allow
  562. su replicator -c ${cmd}
  563. disallow
  564. rm $pid
  565. check=`zfs list -t snapshot`
  566. result="no datasets available"
  567. if [ "$check" = "$result" ]; then
  568. echo "Seems clean enough"
  569. else
  570. echo "Seems there are snaps left:"
  571. zfs list -t snapshot
  572. fi; break ;;
  573. n|N) echo "Suit yourself..." ; break ;;
  574. *) echo "Suit yourself..." ; break ;;
  575. esac
  576. done
  577. else
  578. echo ""
  579. echo "### Error! ###"
  580. echo "Jobname \"$2\" does not exist"
  581. usage
  582. fi
  583. fi
  584. ;;
  585.  
  586. -h)
  587. usage
  588. ;;
  589.  
  590. *)
  591. ##
  592. ## If you type in shorthand jobname, it will only process specified job
  593. ##
  594.  
  595. if [ `ls -1 $path | grep $1 | wc -l` -eq "1" ]
  596. then
  597. sanity
  598. . $path$1
  599. if [ -z "$rh" ]
  600. then
  601. if [ `zfs list -t snapshot | grep $lfs$l_old | wc -l` -eq "1" ]
  602. then
  603. if [ "$r" = "yes" ]
  604. then
  605. l_inc; recurse
  606. else
  607. l_inc
  608. fi
  609. else
  610. if [ "$r" = "yes" ]
  611. then
  612. l_base; recurse
  613. else
  614. l_base
  615. fi
  616. fi
  617. else
  618. if [ `zfs list -t snapshot | grep $lfs$r_old | wc -l` -eq "1" ]
  619. then
  620. if [ "$r" = "yes" ]
  621. then
  622. r_inc; recurse
  623. else
  624. r_inc
  625. fi
  626. else
  627. if [ "$r" = "yes" ]
  628. then
  629. r_base; recurse
  630. else
  631. r_base
  632. fi
  633. fi
  634. fi
  635. touch $pid
  636. allow
  637. su replicator -c ${cmd}
  638. disallow
  639. rm $pid
  640. else
  641. echo ""
  642. echo "### Error! ###"
  643. echo "Jobname \"$1\" does not exist"
  644. usage
  645. fi
  646. ;;
  647.  
  648. esac
  649.  
  650. ##
  651. ## Final cleanup
  652. ##
  653.  
  654. if [ -e ${cmd} ]; then
  655. rm ${cmd}
  656. fi
  657. if [ -e ${mail} ]; then
  658. rm ${mail}
  659. fi
  660.  
  661. chmod 644 /var/log/messages
Advertisement
Add Comment
Please, Sign In to add comment