54.226.34.209 |    

Navigation

Google Advertisement

Simple backup script with GNU tar

backup
  1. #!/bin/bash
  2. # **************************************************************
  3. # file: backup
  4. # date: 2006-02-08 22:27
  5. # author: Marko Schulz - <info@shellXpress.de>
  6. # description: simple backup script with GNU tar
  7. # **************************************************************
  8. #
  9. # I put the following line in my crontab. This make every day
  10. # an incremental backup other than sunday a full backup.
  11. #
  12. # These lines must be on line in your crontab.
  13. # 0 23 * * * [ -n "`date +%a | grep -E '^(So|Sun)'`" ] \
  14. #	&& ( /usr/local/sbin/backup -f ) \
  15. #	|| ( /usr/local/sbin/backup -i ) >/dev/null 2>&1
  16. #
  17. # Directory(s) which will be backup.
  18. backup="/etc /home /root /usr/local/bin /usr/local/sbin /var/cache /var/lib /var/mail /var/spool"
  19. #
  20. # File in which stored the date of the last full backup.
  21. datefile="/var/log/backup/date.log"
  22. #
  23. # Directory in which the created backup stored.
  24. dumpdir="/data/backup"
  25. #
  26. # Files and directorys list in these file will not backup.
  27. prunefile="/var/log/backup/exclude.log"
  28. #
  29. # Mail configuration:
  30. #  Switch to line 77 and follow (mail section) to set your
  31. #  mail variables...
  32. #
  33. # **************************************************************
  34. # program action...
  35.  
  36. # Get current date...
  37. current=$( date +"%m/%d/%y %H:%M" )
  38.  
  39. # Get last backup date...
  40. date=$( cat $datefile 2>/dev/null )
  41.  
  42. # Temporary error variable...
  43. ERROR=0
  44.  
  45. # Lets go...
  46. case "$1" in
  47. 	-f|--full)
  48.  
  49. 		# Update full backup date...
  50. 		( echo $current > $datefile; chmod 0640 $datefile )
  51.  
  52. 		# Get Full backup...
  53. 		( tar --exclude-from $prunefile \
  54. 			-czf $dumpdir/backup-full-$( hostname )-$( date +"%Y-%m-%d_%H%M" ).tar.gz $backup >/dev/null 2>&1 )
  55.  
  56. 		# Set exit state...
  57. 		[ "$?" != 0 ] && ERROR=1
  58.  
  59. 	;;
  60. 	-i|--incremental)
  61.  
  62. 		# Get Incremental Backup...
  63. 		( tar -N "$date" \
  64. 			--exclude-from $prunefile \
  65. 			-czf $dumpdir/backup-incremental-$( hostname )-$( date +"%Y-%m-%d_%H%M" ).tar.gz $backup >/dev/null 2>&1 )
  66.  
  67. 		# Set exit state...
  68. 		[ "$?" != 0 ] && ERROR=2
  69.  
  70. 	;;
  71. 	*)
  72.  
  73. 		# Bad exec...
  74. 		echo -e "Usage: $( basename $0 ) -f --full | -i --incremental"
  75. 		echo -e "\t-i|--incremental get incremental backup"
  76. 		echo -e "\t-f|--full        get full backup\a"
  77.  
  78. 		# Set exit state...
  79. 		ERROR=3
  80.  
  81. 	;;
  82. esac
  83.  
  84. # Sent notify mail, if an error is given.
  85. if [ "$ERROR" != 0 ]; then
  86.  
  87. 	# define subject depending on error state...
  88. 	if [ "$ERROR" = 1 ]; then
  89. 		subject="[$( hostname )] Full backup failed"
  90. 		headline="Full backup failed on $( hostname )"
  91. 	elif [ "$ERROR" = 2 ]; then
  92. 		subject="[$( hostname )] Incremental backup failed"
  93. 		headline="Incremental backup failed on $( hostname )"
  94. 	elif [ "$ERROR" = 3 ]; then
  95. 		subject="[$( hostname )] Backup failed"
  96. 		headline="Backup failed on $( hostname )"
  97. 	fi
  98.  
  99. 	# Define mail header To/From/Subject etc...
  100. 	mail="To: \"Your Name\" <your@domain.de>\n"
  101. 	mail="${mail}Reply-to: \"Your Name\" <your@domain.de>\n"
  102. 	mail="${mail}From: \"System Operator\" <root@$( hostname ).your.domain>\n"
  103. 	mail="${mail}X-Mailer: Custom-CGI: $( basename $0 )\n"
  104. 	mail="${mail}MIME-Version: 1.0\n"
  105. 	mail="${mail}Subject: $subject\n"
  106. 	mail="${mail}Importance: High\n"
  107. 	mail="${mail}Content-Type: text/plain; charset=iso-8859-1\n"
  108. 	mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
  109.  
  110. 	# Define mail body...
  111. 	body="${mail}================================================\n"
  112. 	body="${body}program: $0\n"
  113. 	body="${body}hostname: $( hostname )\n"
  114. 	body="${body}contact: Your Name <your@domain.de>\n"
  115. 	body="${body}date/time: $( date +"%Y-%m-%d %H:%M:%S" )\n"
  116. 	body="${body}================================================\n\n"
  117. 	body="${body}*${headline}*\n\n"
  118.  
  119. 	# Here can you read an error file and put the output to message.
  120. 	#    eg.  message="$( cat /path/to/errorfile )\n"
  121. 	# or type in what you thing :-)
  122. 	message=""
  123.  
  124. 	# Concatenate message to body...
  125. 	body="${body}${message}\n"
  126.  
  127. 	# sent mail with sendmail...
  128. 	echo -e "${body}" | /usr/sbin/sendmail -t
  129.  
  130. fi
  131.  
  132. # exit program...
  133. exit $ERROR
  134.  
  135. # **************************************************************
  136. # end of this script...
Parsed in 0.093 seconds at 43.17 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 8 users online.

Tags Cloud

Latest snippets
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)
STDERR
(Bash::Function)
radius
(CSS)
shadow
(CSS)
rotate
(CSS)
create_function
(JavaScript::Snippets)