backup.sh.in 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. }
  97. advice() {
  98. echo "Directory $1 is not found." >&2
  99. cat >&2 <<EOF
  100. The following directories and files are needed for the backup to function:
  101. 1. Directory with configuration files and file lists:
  102. $CONFIGPATH
  103. 2. Directory for backup log files
  104. $LOGPATH
  105. 3. Main configuration file
  106. $CONFIGFILE
  107. Please, create these and invoke the script again.
  108. EOF
  109. }
  110. init_common() {
  111. # Check if the necessary directories exist
  112. if [ ! -d $CONFIGPATH ]; then
  113. advice $CONFIGPATH
  114. exit 1
  115. fi
  116. if [ ! -d $LOGPATH ]; then
  117. if mkdir $LOGPATH; then
  118. :
  119. else
  120. advice $LOGPATH
  121. exit 1
  122. fi
  123. fi
  124. # Get the values of BACKUP_DIRS, BACKUP_FILES, and other variables.
  125. if [ ! -r $CONFIGFILE ]; then
  126. echo "$PROGNAME: cannot read $CONFIGFILE. Stop." >&2
  127. exit 1
  128. fi
  129. . $CONFIGFILE
  130. # Environment sanity check
  131. test_root
  132. if [ x"${ADMINISTRATOR}" = x ]; then
  133. bailout "ADMINISTRATOR not defined"
  134. fi
  135. [ x"$TAR" = x ] && TAR=tar
  136. [ x"$SLEEP_TIME" = x ] && SLEEP_TIME=60
  137. if [ x$VOLNO_FILE = x ]; then
  138. bailout "VOLNO_FILE not specified"
  139. fi
  140. if [ -r $DIRLIST ]; then
  141. BACKUP_DIRS="$BACKUP_DIRS `cat $DIRLIST`"
  142. fi
  143. if [ -r $FILELIST ]; then
  144. BACKUP_FILES="$BACKUP_FILES `cat $FILELIST`"
  145. fi
  146. if [ \( x"$BACKUP_DIRS" = x \) -a \( x"$BACKUP_FILES" = x \) ]; then
  147. bailout "Neither BACKUP_DIRS nor BACKUP_FILES specified"
  148. fi
  149. if [ "$RSH" = "" ]; then
  150. RSH=rsh
  151. fi
  152. POSIXLY_CORRECT=1
  153. export POSIXLY_CORRECT
  154. }
  155. init_backup() {
  156. init_common
  157. TAR_PART1="${TAR} -c --format=gnu --multi-volume --one-file-system --sparse --volno-file=${VOLNO_FILE}"
  158. if [ "x$XLIST" != x ]; then
  159. TAR_PART1="${TAR_PART1} \`test -r $REMOTEBACKUPDIR/$XLIST && echo \"--exclude-from $REMOTEBACKUPDIR/$XLIST\"\`"
  160. fi
  161. if [ "$RSH_COMMAND" != "" ]; then
  162. TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
  163. fi
  164. if [ x$BLOCKING != x ]; then
  165. TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
  166. fi
  167. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  168. if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
  169. TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
  170. fi
  171. # Set logfile name
  172. # Logfile name should be in the form ``log-1993-03-18-level-0''
  173. # They go in the directory `@sysconfdir@/log'.
  174. # i.e. year-month-date. This format is useful for sorting by name, since
  175. # logfiles are intentionally kept online for future reference.
  176. LOGFILE="${LOGPATH}/log-`now`-level-${DUMP_LEVEL}"
  177. }
  178. init_restore() {
  179. init_common
  180. # FIXME: Replace --list with --extract
  181. TAR_PART1="${TAR} --extract --multi-volume"
  182. if [ "$RSH_COMMAND" != "" ]; then
  183. TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
  184. fi
  185. if [ x$BLOCKING != x ]; then
  186. TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
  187. fi
  188. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  189. if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
  190. TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
  191. fi
  192. LOGFILE="${LOGPATH}/restore-`now`"
  193. }
  194. wait_time() {
  195. if [ "${1}" != "now" ]; then
  196. if [ "${1}x" != "x" ]; then
  197. spec="${1}"
  198. else
  199. spec="${BACKUP_HOUR}"
  200. fi
  201. pausetime="`date | awk -v spec=\"${spec}\" '
  202. BEGIN {
  203. split(spec, time, ":")
  204. }
  205. {
  206. split($4, now, ":")
  207. diff = 3600 * (time[1] - now[1]) + 60 * (time[2] - now[2]);
  208. if (diff < 0)
  209. diff += 3600 * 24
  210. print diff
  211. }'`"
  212. clear
  213. echo "${SLEEP_MESSAGE}"
  214. sleep "${pausetime}"
  215. fi
  216. }
  217. level_log_name() {
  218. echo "$REMOTEBACKUPDIR/${1}.level-${2-$DUMP_LEVEL}"
  219. }
  220. # Prepare a temporary level logfile
  221. # usage: make_level_log HOSTNAME
  222. make_level_log() {
  223. if [ "z${localhost}" != "z$1" ] ; then
  224. $RSH "$1" mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
  225. $RSH "$1" rm -f `level_log_name temp`
  226. else
  227. mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
  228. rm -f `level_log_name temp`
  229. fi
  230. }
  231. # Rename temporary log
  232. # usage: flush_level_log HOSTNAME FSNAME
  233. flush_level_log() {
  234. message 10 "RENAME: `level_log_name temp` --> `level_log_name $2`"
  235. if [ "z${localhost}" != "z$1" ] ; then
  236. $RSH "$1" mv -f `level_log_name temp` "`level_log_name $2`"
  237. else
  238. mv -f `level_log_name temp` "`level_log_name $2`"
  239. fi
  240. }
  241. # Return the timestamp of the last backup.
  242. # usage: get_dump_time LEVEL
  243. get_dump_time() {
  244. ls -r ${LOGPATH}/log-*-level-$1 \
  245. | head -1 \
  246. | sed "s,.*log-\(.*\)-level-$1,\1,"
  247. }
  248. # Do actual backup on a host
  249. # usage: backup_host HOSTNAME [TAR_ARGUMENTS]
  250. backup_host() {
  251. message 10 "ARGS: $@"
  252. rhost=$1
  253. shift
  254. if [ "z${localhost}" != "z$rhost" ] ; then
  255. $RSH "$rhost" ${TAR_PART1} -f "${localhost}:${TAPE_FILE}" $@
  256. else
  257. # Using `sh -c exec' causes nested quoting and shell substitution
  258. # to be handled here in the same way rsh handles it.
  259. CMD="exec ${TAR_PART1} -f \"${TAPE_FILE}\" $@"
  260. message 10 "CMD: $CMD"
  261. sh -c "$CMD"
  262. message 10 "RC: $?"
  263. fi
  264. }
  265. print_level() {
  266. if [ ${1-$DUMP_LEVEL} -eq 0 ]; then
  267. echo "Full"
  268. else
  269. echo "Level ${1-$DUMP_LEVEL}"
  270. fi
  271. }
  272. prev_level() {
  273. print_level `expr $DUMP_LEVEL - 1` | tr A-Z a-z
  274. }
  275. remote_run() {
  276. rhost=$1
  277. shift
  278. message 10 "REMOTE $rhost: $@"
  279. if [ "x$rhost" != "x${localhost}" ] ; then
  280. $RSH "${rhost}" "$@"
  281. else
  282. $*
  283. fi
  284. }
  285. license() {
  286. cat - <<EOF
  287. This program is part of GNU tar
  288. Copyright 2004, Free Software Foundation
  289. This program is free software; you can redistribute it and/or modify
  290. it under the terms of the GNU General Public License as published by
  291. the Free Software Foundation; either version 1, or (at your option)
  292. any later version.
  293. This program is distributed in the hope that it will be useful,
  294. but WITHOUT ANY WARRANTY; without even the implied warranty of
  295. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  296. GNU General Public License for more details.
  297. You should have received a copy of the GNU General Public License
  298. along with this program; if not, write to the Free Software
  299. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  300. 02111-1307, USA.
  301. EOF
  302. }