Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # *** Accepts two optional arguments
  4. # -tDATE: today's date (ISO-8601, yyyy-mm-dd)
  5. # -yDATE: yesterday's date (ISO-8601, yyyy-mm-dd)
  6. #
  7. # Use these if a nightly backup is missed.
  8. # "Yesterday's date" should be the date of the last successful backup.
  9. # Otherwise --link-dest will fail and rsync will do a full copy
  10. # instead of a diff.
  11. #
  12. # Example: the backup from 2013-08-21 failed.
  13. # If the job is allowed to run automatically on 2013-08-22,
  14. # --link-dest will point to the 2013-08-21 folders which do not exist
  15. # (or are empty). That will cause rsync to create a full backup
  16. # instead of a differential one. Instead, run
  17. # 'rsync-backup -t2013-08-22 -y2013-08-20' the following morning. Doing
  18. # so wll preservs the diff going forward.
  19.  
  20. #set -o nounset -o xtrace
  21.  
  22. unset PATH
  23.  
  24. # --- system commands used by script ---
  25. ID=/usr/bin/id
  26. ECHO=/bin/echo
  27. DATE=/bin/date
  28. GETOPT=/usr/bin/getopt
  29.  
  30. MOUNT=/bin/mount
  31. RM=/bin/rm
  32. MKDIR=/bin/mkdir
  33.  
  34. RSYNC=/usr/bin/rsync
  35.  
  36. # --- local locations ---
  37. MOUNT_DEVICE=/dev/mapper/vg_nasit3-lv_backups
  38. SNAPSHOT_RW=/srv/backups
  39. EXCLUDES=/usr/local/etc/backup_exclude
  40. SSH_KEY=/root/.ssh/id_dsa_nasit3
  41.  
  42. # --- dates ---
  43. TODAY=$($DATE -I)
  44. YESTERDAY=$($DATE -d "1 day ago" -I)
  45. MONTH_AGO=$($DATE -d "3 months ago" -I)
  46. MONTH_AGO_OLD=$($DATE -d "3 months ago" +%d-%b-%Y)
  47.  
  48. #YESTERDAY=$($DATE -d "1 day ago" +%d-%b-%Y)
  49.  
  50. DURST_MONTH_AGO=$($DATE -d "1 month ago" -I)
  51.  
  52. # --- remote locations ---
  53. declare -A sources
  54. sources=( [caldera-cart]=caldera-cart:/srv/backups
  55. [filesrv]=filesrv:/srv/backups
  56. [filesrv_data]=filesrv:/srv/data
  57. [itsupport]=itsupport:/srv/backups
  58. [caldera-tg5]=caldera-tg5:/srv/backups
  59. [caldera-tg4]=caldera-tg4:/srv/backups
  60. [dc1]=dc1:/srv/backups
  61. [dc2]=dc2:/srv/backups
  62. [prep2]=prep2:/srv/backups
  63. [db1]=db1:/srv/backups
  64. [db2]=db2:/srv/backups
  65. [db3]=db3:/srv/backups
  66. [postgresql]=postgresql:/srv/backups
  67. [mserver]=mserver:/srv/backups
  68. [monitor]=monitor:/srv/backups
  69. [PuppetMaster01]=PuppetMaster01:/srv/backups
  70. [services_www]=services:/var/www
  71. [services_extranet]=services:/srv/web
  72. [ca]=ca:/srv/backups
  73. [stash]=stash:/home/stash
  74. )
  75.  
  76. declare -A durstdb
  77. durstdb=( [r11]=r11:/mnt/images/HotFolders/SYSTEM_BACKUP
  78. [r9]=r9:/mnt/images/HotFolders/SYSTEM_BACKUP
  79. [r8]=r8:/mnt/images/HotFolders/SYSTEM_BACKUP
  80. [r5]=r5:/mnt/images/HotFolders/SYSTEM_BACKUP
  81. [r4]=r4:/mnt/images/HotFolders/SYSTEM_BACKUP
  82. )
  83.  
  84. declare -A durstsw
  85. durstsw=( [r10]=r10:
  86. [r6]=r6:
  87. )
  88.  
  89. # --- the script ---
  90. # make sure running as root
  91. if (( `$ID -u` != 0 )); then
  92. {
  93. $ECHO "Sorry, must be root. Exiting..."
  94. exit
  95. } fi
  96.  
  97. TEMP=`$GETOPT -o t::y:: -n 'rsync-backup.sh' -- "$@"`
  98. eval set -- "$TEMP"
  99. while true; do
  100. case "$1" in
  101. -t ) TODAY="$2"; shift 2 ;;
  102. -y ) YESTERDAY="$2"; shift 2 ;;
  103. * ) break ;;
  104. esac
  105. done
  106.  
  107. for label in "${!sources[@]}"; do
  108. {
  109. $ECHO -e "\n\n*** $label ***"
  110. $MKDIR -p "$SNAPSHOT_RW/$label/$TODAY"
  111. $RSYNC --link-dest="$SNAPSHOT_RW/$label/$YESTERDAY" \
  112. -avz --numeric-ids --delete --delete-excluded \
  113. --exclude-from="$EXCLUDES" -e "ssh -i "$SSH_KEY"" \
  114. "${sources[$label]}/" "$SNAPSHOT_RW/$label/$TODAY"
  115. $RM -rf "$SNAPSHOT_RW/$label/$MONTH_AGO_OLD"
  116. $RM -rf "$SNAPSHOT_RW/$label/$MONTH_AGO"
  117. } done
  118.  
  119. for label in "${!durstdb[@]}"; do
  120. {
  121. $ECHO -e "\n\n*** $label ***"
  122. $MKDIR -p "$SNAPSHOT_RW/$label"
  123. $RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
  124. "${durstdb[$label]}/DB*-$TODAY.*" "$SNAPSHOT_RW/$label"
  125. $RM -rf "$SNAPSHOT_RW/$label/DB*-$DURST_MONTH_AGO.*"
  126. } done
  127.  
  128. for label in "${!durstsw[@]}"; do
  129. {
  130. $ECHO -e "\n\n*** $label ***"
  131. $MKDIR -p "$SNAPSHOT_RW/$label/$TODAY"
  132. $RSYNC --link-dest="$SNAPSHOT_RW/$label/$YESTERDAY" \
  133. -avzR --numeric-ids --delete --delete-excluded \
  134. --exclude-from="$EXCLUDES" -e "ssh -i "$SSH_KEY"" \
  135. "${durstsw[$label]}/usr/local/printsw/" "$SNAPSHOT_RW/$label/$TODAY"
  136. $RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
  137. "${durstsw[$label]}/mnt/images/Statistics/DPJobState-$TODAY.txt" \
  138. "$SNAPSHOT_RW/$label/$TODAY"
  139. $RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
  140. "${durstsw[$label]}/mnt/images/Statistics/DPPrinterState.txt" \
  141. "$SNAPSHOT_RW/$label/$TODAY"
  142. $RSYNC -avz --numeric-ids -e "ssh -i "$SSH_KEY"" \
  143. "${durstsw[$label]}/mnt/images/Statistics/DPQueueState.txt" \
  144. "$SNAPSHOT_RW/$label/$TODAY"
  145. $RM -rf "$SNAPSHOT_RW/$label/$MONTH_AGO"
  146. } done
  147.  
  148. $ECHO -e "\n\n*** FB1 ***"
  149. $MKDIR -p "$SNAPSHOT_RW/FB1/$TODAY"
  150. $RSYNC --link-dest="$SNAPSHOT_RW/FB1/$YESTERDAY" -avzR --delete --delete-excluded \
  151. --exclude-from="$EXCLUDES" FB10000-021::FB1configs "$SNAPSHOT_RW/FB1/$TODAY"
  152. $RM -rf "$SNAPSHOT_RW/FB1/$MONTH_AGO"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement