backup.sh.in 8.9 KB

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