backup.sh.in 8.6 KB

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