Advertisement
Guest User

Untitled

a guest
Feb 8th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ####################################
  4.  
  5. #
  6.  
  7. # Backup script with
  8.  
  9. # grandfather-father-son rotation.
  10.  
  11. #
  12.  
  13. ####################################
  14.  
  15.  
  16.  
  17. # What to backup.
  18.  
  19. backup_files="/home /etc /root /boot /opt"
  20.  
  21.  
  22.  
  23. # Where to backup to.
  24.  
  25. dest="/mnt/backup"
  26.  
  27.  
  28.  
  29. # Setup variables for the archive filename.
  30.  
  31. day=$(date +%A)
  32.  
  33. hostname=$(hostname -f)
  34.  
  35.  
  36.  
  37. # Find which week of the month 1-4 it is.
  38.  
  39. day_num=$(date +%-d)
  40.  
  41. if (( $day_num <= 7 )); then
  42.  
  43.         week_file="$hostname-week1.tgz"
  44.  
  45. elif (( $day_num > 7 && $day_num <= 14 )); then
  46.  
  47.         week_file="$hostname-week2.tgz"
  48.  
  49. elif (( $day_num > 14 && $day_num <= 21 )); then
  50.  
  51.         week_file="$hostname-week3.tgz"
  52.  
  53. elif (( $day_num > 21 && $day_num < 32 )); then
  54.  
  55.         week_file="$hostname-week4.tgz"
  56.  
  57. fi
  58.  
  59.  
  60.  
  61. # Find if the Month is odd or even.
  62.  
  63. month_num=$(date +%m)
  64.  
  65. month=$(expr $month_num % 2)
  66.  
  67. if [ $month -eq 0 ]; then
  68.  
  69.         month_file="$hostname-month2.tgz"
  70.  
  71. else
  72.  
  73.         month_file="$hostname-month1.tgz"
  74.  
  75. fi
  76.  
  77.  
  78.  
  79. # Create archive filename.
  80.  
  81. if [ $day_num == 1 ]; then
  82.  
  83.     archive_file=$month_file
  84.  
  85. elif [ $day != "Saturday" ]; then
  86.  
  87.         archive_file="$hostname-$day.tgz"
  88.  
  89. else
  90.  
  91.     archive_file=$week_file
  92.  
  93. fi
  94.  
  95.  
  96.  
  97. # Print start status message.
  98.  
  99. echo "Backing up $backup_files to $dest/$archive_file"
  100.  
  101. date
  102.  
  103. echo
  104.  
  105.  
  106.  
  107. # Backup the files using tar.
  108.  
  109. tar czf $dest/$archive_file $backup_files
  110.  
  111.  
  112.  
  113. # Print end status message.
  114.  
  115. echo
  116.  
  117. echo "Backup finished"
  118.  
  119. date
  120.  
  121.  
  122.  
  123. # Long listing of files in $dest to check file sizes.
  124.  
  125. ls -lh $dest/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement