4
0

backup.sh.in 8.6 KB

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