backup.sh.in 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #! /bin/sh
  2. # Make backups.
  3. # Copyright 2004-2006, 2013-2014, 2016-2017 Free Software Foundation,
  4. # Inc.
  5. # This file is part of GNU tar.
  6. # GNU tar is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. # GNU tar 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. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. PROGNAME=`basename $0`
  17. CONFIGPATH="$SYSCONFDIR/backup"
  18. REMOTEBACKUPDIR="$SYSCONFDIR/tar-backup"
  19. CONFIGFILE=${CONFIGPATH}/backup-specs
  20. DIRLIST=${CONFIGPATH}/dirs
  21. FILELIST=${CONFIGPATH}/files
  22. LOGPATH=${CONFIGPATH}/log
  23. # Default functions for running various magnetic tape commands
  24. mt_begin() {
  25. $MT -f "$1" retension
  26. }
  27. mt_rewind() {
  28. $MT -f "$1" rewind
  29. }
  30. mt_offline() {
  31. $MT -f "$1" offl
  32. }
  33. mt_status() {
  34. $MT -f "$1" status
  35. }
  36. # The main configuration file may override any of these variables
  37. MT_BEGIN=mt_begin
  38. MT_REWIND=mt_rewind
  39. MT_OFFLINE=mt_offline
  40. MT_STATUS=mt_status
  41. # Insure 'mail' is in PATH.
  42. PATH="/usr/ucb:${PATH}"
  43. export PATH
  44. # Put startdate in the subject line of mailed report, since if it happens
  45. # to run longer than 24 hours (as may be the case if someone forgets to put
  46. # in the next volume of the tape in adequate time), the backup date won't
  47. # appear too misleading.
  48. startdate="`date`"
  49. here="`pwd`"
  50. # Save local hostname
  51. localhost="`hostname | sed -e 's/\..*//' | tr A-Z a-z`"
  52. # Produce a diagnostic output
  53. message() {
  54. if [ "$VERBOSE" != "" ]; then
  55. if [ $VERBOSE -ge $1 ]; then
  56. shift
  57. echo "$@" >&2
  58. fi
  59. fi
  60. }
  61. # Bail out and exit.
  62. bailout() {
  63. echo "$PROGNAME: $*" >&2
  64. exit 1
  65. }
  66. # Return current date
  67. now() {
  68. #IF_DATE_FORMAT_OK
  69. date +%Y-%m-%d
  70. #ELSE_DATE_FORMAT_OK
  71. LC_ALL=C date | \
  72. sed 's/[^ ]* *\([^ ]*\) *\([^ ]*\).* \([^ ]*\)$/\3-\1-\2/
  73. /-[0-9]$/s/\([0-9]\)$/0\1/
  74. /Jan/{s/Jan/01/p;q;}
  75. /Feb/{s/Feb/02/p;q;}
  76. /Mar/{s/Mar/03/p;q;}
  77. /Apr/{s/Apr/04/p;q;}
  78. /May/{s/May/05/p;q;}
  79. /Jun/{s/Jun/06/p;q;}
  80. /Jul/{s/Jul/07/p;q;}
  81. /Aug/{s/Aug/08/p;q;}
  82. /Sep/{s/Sep/09/p;q;}
  83. /Oct/{s/Oct/10/p;q;}
  84. /Nov/{s/Nov/11/p;q;}
  85. /Dec/{s/Dec/12/p;q;}'
  86. #ENDIF_DATE_FORMAT_OK
  87. }
  88. # Bail out if we don't have root privileges.
  89. test_root() {
  90. if [ ! -w ${ROOT_FS-/} ]; then
  91. bailout "The backup must be run as root or else some files will fail to be dumped."
  92. fi
  93. }
  94. root_fs() {
  95. echo "${ROOT_FS}$1" | tr -s /
  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 [ -z "$RSH" ]; then
  150. RSH=rsh
  151. MT_RSH_OPTION=
  152. else
  153. MT_RSH_OPTION="--rsh-command=$RSH"
  154. fi
  155. if [ -z "$TAPE_FILE" ]; then
  156. TAPE_FILE=/dev/tape
  157. fi
  158. # If TAPE_FILE is a remote device, update mt invocation accordingly
  159. : ${MT:=mt}
  160. case $TAPE_FILE in
  161. *:*) MT="$MT $MT_RSH_OPTION";;
  162. *) ;;
  163. esac
  164. POSIXLY_CORRECT=1
  165. export POSIXLY_CORRECT
  166. }
  167. init_backup() {
  168. init_common
  169. TAR_PART1="${TAR} -c --format=gnu --multi-volume --one-file-system --sparse --volno-file=${VOLNO_FILE}"
  170. if [ "x$XLIST" != x ]; then
  171. TAR_PART1="${TAR_PART1} \`test -r $REMOTEBACKUPDIR/$XLIST && echo \"--exclude-from $REMOTEBACKUPDIR/$XLIST\"\`"
  172. fi
  173. if [ "$RSH_COMMAND" != "" ]; then
  174. TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
  175. fi
  176. if [ x$BLOCKING != x ]; then
  177. TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
  178. fi
  179. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  180. if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
  181. TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
  182. fi
  183. # Set logfile name
  184. # Logfile name should be in the form 'log-1993-03-18-level-0'
  185. # They go in the directory '@sysconfdir@/log'.
  186. # i.e. year-month-date. This format is useful for sorting by name, since
  187. # logfiles are intentionally kept online for future reference.
  188. LOGFILE="${LOGPATH}/log-`now`-level-${DUMP_LEVEL}"
  189. }
  190. init_restore() {
  191. init_common
  192. # FIXME: Replace --list with --extract
  193. TAR_PART1="${TAR} --extract --multi-volume"
  194. if [ "$RSH_COMMAND" != "" ]; then
  195. TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
  196. fi
  197. if [ x$BLOCKING != x ]; then
  198. TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
  199. fi
  200. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  201. if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
  202. TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
  203. fi
  204. LOGFILE="${LOGPATH}/restore-`now`"
  205. }
  206. wait_time() {
  207. if [ "${1}" != "now" ]; then
  208. if [ "${1}x" != "x" ]; then
  209. spec="${1}"
  210. else
  211. spec="${BACKUP_HOUR}"
  212. fi
  213. pausetime="`date | awk -v spec=\"${spec}\" '
  214. BEGIN {
  215. split(spec, time, ":")
  216. }
  217. {
  218. split($4, now, ":")
  219. diff = 3600 * (time[1] - now[1]) + 60 * (time[2] - now[2]);
  220. if (diff < 0)
  221. diff += 3600 * 24
  222. print diff
  223. }'`"
  224. clear
  225. echo "${SLEEP_MESSAGE}"
  226. sleep "${pausetime}"
  227. fi
  228. }
  229. level_log_name() {
  230. echo "$REMOTEBACKUPDIR/${1}.level-${2-$DUMP_LEVEL}"
  231. }
  232. # Prepare a temporary level logfile
  233. # usage: make_level_log HOSTNAME
  234. make_level_log() {
  235. if [ "z${localhost}" != "z$1" ] ; then
  236. $RSH "$1" mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
  237. $RSH "$1" rm -f `level_log_name temp`
  238. else
  239. mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
  240. rm -f `level_log_name temp`
  241. fi
  242. }
  243. # Rename temporary log
  244. # usage: flush_level_log HOSTNAME FSNAME
  245. flush_level_log() {
  246. message 10 "RENAME: `level_log_name temp` --> `level_log_name $2`"
  247. if [ "z${localhost}" != "z$1" ] ; then
  248. $RSH "$1" mv -f `level_log_name temp` "`level_log_name $2`"
  249. else
  250. mv -f `level_log_name temp` "`level_log_name $2`"
  251. fi
  252. }
  253. # Return the timestamp of the last backup.
  254. # usage: get_dump_time LEVEL
  255. get_dump_time() {
  256. ls -r ${LOGPATH}/log-*-level-$1 \
  257. | head -n 1 \
  258. | sed "s,.*log-\(.*\)-level-$1,\1,"
  259. }
  260. # Do actual backup on a host
  261. # usage: backup_host HOSTNAME [TAR_ARGUMENTS]
  262. backup_host() {
  263. message 10 "ARGS: $@"
  264. rhost=$1
  265. shift
  266. if [ "z${localhost}" != "z$rhost" ] ; then
  267. $RSH "$rhost" ${TAR_PART1} -f "${localhost}:${TAPE_FILE}" $@
  268. else
  269. # Using 'sh -c exec' causes nested quoting and shell substitution
  270. # to be handled here in the same way rsh handles it.
  271. CMD="exec ${TAR_PART1} -f \"${TAPE_FILE}\" $@"
  272. message 10 "CMD: $CMD"
  273. sh -c "$CMD"
  274. RC=$?
  275. message 10 "RC: $RC"
  276. return $RC
  277. fi
  278. }
  279. print_level() {
  280. if [ ${1-$DUMP_LEVEL} -eq 0 ]; then
  281. echo "Full"
  282. else
  283. echo "Level ${1-$DUMP_LEVEL}"
  284. fi
  285. }
  286. prev_level() {
  287. print_level `expr $DUMP_LEVEL - 1` | tr A-Z a-z
  288. }
  289. remote_run() {
  290. rhost=$1
  291. shift
  292. message 10 "REMOTE $rhost: $@"
  293. if [ "x$rhost" != "x${localhost}" ] ; then
  294. $RSH "${rhost}" "$@"
  295. else
  296. $*
  297. fi
  298. }
  299. license() {
  300. cat - <<EOF
  301. Copyright (C) 2013 Free Software Foundation, Inc.
  302. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  303. This is free software: you are free to change and redistribute it.
  304. There is NO WARRANTY, to the extent permitted by law.
  305. EOF
  306. }