backup.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #! /bin/sh
  2. # This program is part of GNU tar
  3. # Copyright 2004, Free Software Foundation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 1, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  18. # 02111-1307, USA.
  19. PROGNAME=`basename $0`
  20. CONFIGPATH="$SYSCONFDIR/backup"
  21. REMOTEBACKUPDIR="$SYSCONFDIR/tar-backup"
  22. CONFIGFILE=${CONFIGPATH}/backup-specs
  23. DIRLIST=${CONFIGPATH}/dirs
  24. FILELIST=${CONFIGPATH}/files
  25. LOGPATH=${CONFIGPATH}/log
  26. LIBPATH=/usr/libexec/backup
  27. # Default functions for running various magnetic tape commands
  28. mt_begin() {
  29. mt -f "$1" retension
  30. }
  31. mt_rewind() {
  32. mt -f "$1" rewind
  33. }
  34. mt_offline() {
  35. mt -f "$1" offl
  36. }
  37. mt_status() {
  38. mt -f "$1" status
  39. }
  40. # The main configuration file may override any of these variables
  41. MT_BEGIN=mt_begin
  42. MT_REWIND=mt_rewind
  43. MT_OFFLINE=mt_offl
  44. MT_STATUS=mt_status
  45. # Insure `mail' is in PATH.
  46. PATH="/usr/ucb:${PATH}"
  47. export PATH
  48. # Put startdate in the subject line of mailed report, since if it happens
  49. # to run longer than 24 hours (as may be the case if someone forgets to put
  50. # in the next volume of the tape in adequate time), the backup date won't
  51. # appear too misleading.
  52. startdate="`date`"
  53. here="`pwd`"
  54. # Save local hostname
  55. localhost="`hostname | sed -e 's/\..*//' | tr A-Z a-z`"
  56. # Produce a diagnostic output
  57. message() {
  58. if [ "$VERBOSE" != "" ]; then
  59. if [ $VERBOSE -ge $1 ]; then
  60. shift
  61. echo "$@"
  62. fi
  63. fi
  64. }
  65. # Bail out and exit.
  66. bailout() {
  67. echo "$PROGNAME: $*" >&2
  68. exit 1
  69. }
  70. # Bail out if we don't have root privileges.
  71. test_root() {
  72. if [ ! -w / ]; then
  73. bailout "The backup must be run as root or else some files will fail to be dumped."
  74. fi
  75. }
  76. advice() {
  77. echo "Directory $1 is not found." >&2
  78. cat >&2 <<EOF
  79. The following directories and files are needed for the backup to function:
  80. 1. Directory with configuration files and file lists:
  81. $CONFIGPATH
  82. 2. Directory for backup log files
  83. $LOGPATH
  84. 3. Main configuration file
  85. $CONFIGFILE
  86. Please, create these and invoke the script again.
  87. EOF
  88. }
  89. init_backup() {
  90. # Check if the necessary directories exist
  91. if [ ! -d $CONFIGPATH ]; then
  92. advice $CONFIGPATH
  93. exit 1
  94. fi
  95. if [ ! -d $LOGPATH ]; then
  96. if mkdir $LOGPATH; then
  97. :
  98. else
  99. advice $LOGPATH
  100. exit 1
  101. fi
  102. fi
  103. # Get the values of BACKUP_DIRS, BACKUP_FILES, and other variables.
  104. if [ ! -r $CONFIGFILE ]; then
  105. echo "$PROGNAME: cannot read $CONFIGFILE. Stop." >&2
  106. exit 1
  107. fi
  108. . $CONFIGFILE
  109. # Environment sanity check
  110. test_root
  111. if [ x"${ADMINISTRATOR}" = x ]; then
  112. bailout "ADMINISTRATOR not defined"
  113. fi
  114. [ x"$TAR" = x ] && TAR=tar
  115. [ x"$SLEEP_TIME" = x ] && SLEEP_TIME=60
  116. if [ x$VOLNO_FILE = x ]; then
  117. bailout "VOLNO_FILE not specified"
  118. fi
  119. if [ -r $DIRLIST ]; then
  120. BACKUP_DIRS="$BACKUP_DIRS `cat $DIRLIST`"
  121. fi
  122. if [ -r $FILELIST ]; then
  123. BACKUP_FILES="$BACKUP_FILES `cat $FILELIST`"
  124. fi
  125. if [ \( x"$BACKUP_DIRS" = x \) -a \( x"$BACKUP_FILES" = x \) ]; then
  126. bailout "Neither BACKUP_DIRS nor BACKUP_FILES specified"
  127. fi
  128. TAR_PART1="${TAR} -c --format=gnu --multi-volume --one-file-system --sparse --volno-file=${VOLNO_FILE}"
  129. if [ "x$XLIST" != x ]; then
  130. TAR_PART1="${TAR_PART1} \`test -r $REMOTEBACKUPDIR/$XLIST && echo \"--exclude-from $REMOTEBACKUPDIR/$XLIST\"\`"
  131. fi
  132. if [ "$RSH_COMMAND" != "" ]; then
  133. TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
  134. fi
  135. if [ "$RSH" = "" ]; then
  136. RSH=rsh
  137. fi
  138. if [ x$BLOCKING != x ]; then
  139. TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
  140. fi
  141. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  142. if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
  143. TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
  144. fi
  145. # Set logfile name
  146. # Logfile name should be in the form ``log-1993-03-18-level-0''
  147. # They go in the directory `@sysconfdir@/log'.
  148. # i.e. year-month-date. This format is useful for sorting by name, since
  149. # logfiles are intentionally kept online for future reference.
  150. LOGFILE="${LOGPATH}/log-`now`-level-${DUMP_LEVEL}"
  151. POSIXLY_CORRECT=1
  152. export POSIXLY_CORRECT
  153. }
  154. wait_time() {
  155. if [ "${1}" != "now" ]; then
  156. if [ "${1}x" != "x" ]; then
  157. spec="${1}"
  158. else
  159. spec="${BACKUP_HOUR}"
  160. fi
  161. pausetime="`date | awk -v spec=\"${spec}\" '
  162. BEGIN {
  163. split(spec, time, ":")
  164. }
  165. {
  166. split($4, now, ":")
  167. diff = 3600 * (time[1] - now[1]) + 60 * (time[2] - now[2]);
  168. if (diff < 0)
  169. diff += 3600 * 24
  170. print diff
  171. }'`"
  172. clear
  173. echo "${SLEEP_MESSAGE}"
  174. sleep "${pausetime}"
  175. fi
  176. }
  177. level_log_name() {
  178. echo "$REMOTEBACKUPDIR/${1}.level-${2-$DUMP_LEVEL}"
  179. }
  180. # Prepare a temporary level logfile
  181. # usage: make_level_log HOSTNAME
  182. make_level_log() {
  183. if [ "z${localhost}" != "z$1" ] ; then
  184. $RSH "$1" mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
  185. $RSH "$1" rm -f `level_log_name temp`
  186. else
  187. mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
  188. rm -f `level_log_name temp`
  189. fi
  190. }
  191. # Rename temporary log
  192. # usage: flush_level_log HOSTNAME FSNAME
  193. flush_level_log() {
  194. message 10 "RENAME: `level_log_name temp` --> `level_log_name $2`"
  195. if [ "z${localhost}" != "z$1" ] ; then
  196. $RSH "$1" mv -f `level_log_name temp` "`level_log_name $2`"
  197. else
  198. mv -f `level_log_name temp` "`level_log_name $2`"
  199. fi
  200. }
  201. # Do actual backup on a host
  202. # usage: backup_host HOSTNAME [TAR_ARGUMENTS]
  203. backup_host() {
  204. message 10 "ARGS: $@"
  205. rhost=$1
  206. shift
  207. if [ "z${localhost}" != "z$rhost" ] ; then
  208. $RSH "$rhost" ${TAR_PART1} -f "${localhost}:${TAPE_FILE}" $@
  209. else
  210. # Using `sh -c exec' causes nested quoting and shell substitution
  211. # to be handled here in the same way rsh handles it.
  212. CMD="exec ${TAR_PART1} -f \"${TAPE_FILE}\" $@"
  213. message 10 "CMD: $CMD"
  214. sh -c "$CMD"
  215. message 10 "RC: $?"
  216. fi
  217. }
  218. print_level() {
  219. if [ ${1-$DUMP_LEVEL} -eq 0 ]; then
  220. echo "Full"
  221. else
  222. echo "Level ${1-$DUMP_LEVEL}"
  223. fi
  224. }
  225. prev_level() {
  226. print_level `expr $DUMP_LEVEL - 1` | tr A-Z a-z
  227. }
  228. remote_run() {
  229. rhost=$1
  230. shift
  231. message 10 "REMOTE $1: $@"
  232. if [ "x$rhost" != "x${localhost}" ] ; then
  233. $RSH "${rhost}" "$@"
  234. else
  235. $*
  236. fi
  237. }
  238. license() {
  239. cat - <<EOF
  240. This program is part of GNU tar
  241. Copyright 2004, Free Software Foundation
  242. This program is free software; you can redistribute it and/or modify
  243. it under the terms of the GNU General Public License as published by
  244. the Free Software Foundation; either version 1, or (at your option)
  245. any later version.
  246. This program is distributed in the hope that it will be useful,
  247. but WITHOUT ANY WARRANTY; without even the implied warranty of
  248. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  249. GNU General Public License for more details.
  250. You should have received a copy of the GNU General Public License
  251. along with this program; if not, write to the Free Software
  252. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  253. 02111-1307, USA.
  254. EOF
  255. }