codekingpro/portable-devtools
114k
1#! /bin/sh2# Make backups.3 4# Copyright 2004-2006, 2013, 2019 Free Software Foundation5 6# This file is part of GNU tar.7 8# GNU tar is free software; you can redistribute it and/or modify9# it under the terms of the GNU General Public License as published by10# the Free Software Foundation; either version 3 of the License, or11# (at your option) any later version.12 13# GNU tar is distributed in the hope that it will be useful,14# but WITHOUT ANY WARRANTY; without even the implied warranty of15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16# GNU General Public License for more details.17 18# You should have received a copy of the GNU General Public License19# along with this program. If not, see <http://www.gnu.org/licenses/>.20 21# Load library routines22SYSCONFDIR=${SYSCONFDIR-/usr/etc}23. ${LIBDIR-/usr/lib/tar}/backup.sh24 25DUMP_LEVEL=026TIME=27NOW=`now`28 29usage() {30 cat - <<EOF31usage: $PROGNAME [OPTIONS] [WHEN]32Options are:33 34 -l, --level=LEVEL Do backup level LEVEL (default $DUMP_LEVEL).35 -f, --force Force backup even if today's log file already36 exists.37 -v, --verbose[=LEVEL] Set verbosity level. Default 100.38 -t, --time=TIME Wait till TIME, then do backup.39 40Informational options:41 -h, --help Display this help message.42 -V, --version Display program version.43 44 Optional argument WHEN is for backward compatibility only. It has been45 superseded by --time option.46 TIME argument can be one of:47 48 now -- do backup immediately.49 HH -- do backup at HH hours.50 HH:MM -- do backup at HH:MM.51 52Send bug reports to bug-tar@gnu.org.53EOF54}55 56# For compatibility with previous versions, deduce the backup level57# from the command name58case "$PROGNAME" in59level-[0-9]) DUMP_LEVEL=`expr $PROGNAME : 'level-\([0-9][0-9]*\)'`;;60esac61 62for opt63do64 if [ -z "$prev" ]; then65 option=$opt66 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`67 else68 option="${prev}=$opt"69 prev=""70 optarg=$opt71 fi72 case $option in73 --l=*|--le=*|--lev=*|--leve=*|--level=*)74 DUMP_LEVEL=$optarg75 ;;76 -l?*) DUMP_LEVEL=`expr $option : '-l\(.*\)'`;;77 -l|--l|--le|--lev|--leve|--level)78 prev=--level79 ;;80 --verb=*|--verbo=*|--verbos=*|--verbose=*)81 VERBOSE=$optarg82 ;;83 -v|--verb|--verbo|--verbos|--verbose)84 VERBOSE=10085 ;;86 -v*) VERBOSE=`expr $option : '-v\(.*\)'`;;87 --t=*|--ti=*|--tim=*|--time=*)88 TIME=$optarg89 ;;90 -t?*) TIME=`expr $option : '-t\(.*\)'`;;91 -t|--t|--ti|--tim|--time)92 prev=--time93 ;;94 -V|--v|--ve|--ver|--vers|--versi|--versio|--version)95 echo "backup (GNU tar) 1.35"96 license97 exit;;98 -h|--h|--he|--hel|--help)99 usage100 exit;;101 -f|--f|--fo|--for|--forc|--force)102 FORCE=yes103 ;;104 *) if [ "x$TIME" != "x" ]; then105 bailout "Extra argument. Try $PROGNAME --help for more info."106 else107 TIME=$option108 fi;;109 esac110done111 112if [ "x$TIME" = x ]; then113 bailout "No backup time specified. Try $PROGNAME --help for more info."114 exit 1115fi116 117init_backup118 119# Maybe sleep until around specified or default hour.120wait_time $TIME121 122if [ $DUMP_LEVEL -ne 0 ]; then123 PREV_LEVEL=`expr $DUMP_LEVEL - 1`124 PREV_DATE=`ls -t ${LOGPATH}/log-*-level-$PREV_LEVEL|125 head -n 1|126 sed "s,${LOGPATH}/log-\(.*\)-level.*,\1,"`127 if [ "x$PREV_DATE" = x ]; then128 bailout "Can't determine date of the previous backup"129 fi130 message 0 "Backup from $PREV_DATE to $NOW"131fi132 133# start doing things134 135# Make sure the log file did not already exist. Create it.136 137if [ "x$FORCE" = "xyes" ]; then138 rm ${LOGFILE}139fi140 141if [ -f "${LOGFILE}" ] ; then142 bailout "Log file ${LOGFILE} already exists."143else144 touch "${LOGFILE}"145fi146message 1 "Ready for backup."147message 10 "TAR invocation: $TAR_PART1"148message 20 "Variables:"149message 20 "BACKUP_DIRS=$BACKUP_DIRS"150message 20 "BACKUP_FILES=$BACKUP_FILES"151 152# The bunch of commands below is run in a subshell for which all output is153# piped through 'tee' to the logfile. Doing this, instead of having154# multiple pipelines all over the place, is cleaner and allows access to155# the exit value from various commands more easily.156(157 message 1 "preparing tapes"158 if ! $MT_BEGIN "${TAPE_FILE}"; then159 echo >&2 "$0: tape initialization failed"160 exit 1161 fi 162 rm -f "${VOLNO_FILE}"163 164 message 1 "processing backup directories"165 166 set - ${BACKUP_DIRS}167 while [ $# -ne 0 ] ; do168 date="`date`"169 fs="`echo \"${1}\" | sed -e 's/^.*://'`"170 fs=`root_fs $fs`171 fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"172 remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"173 if [ -z "$remotehost" ]; then174 remotehost=$localhost175 fi176 177 echo "Backing up ${1} at ${date}"178 message 10 "fs=$fs"179 message 10 "fsname=$fsname"180 message 10 "remotehost=$remotehost"181 if [ $DUMP_LEVEL -eq 0 ]; then182 make_level_log ${remotehost}183 else184 echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"185 remote_run "${remotehost}" cp "`level_log_name ${fsname} $PREV_LEVEL`" "`level_log_name temp`"186 fi187 188 ${DUMP_BEGIN-:} $DUMP_LEVEL $remotehost $fs $fsname189 backup_host ${remotehost} \190 "--listed=`level_log_name temp`" \191 "--label='`print_level` backup of ${fs} on ${remotehost} at ${NOW}'" \192 -C ${fs} .193 194 # 'rsh' doesn't exit with the exit status of the remote command. What195 # stupid lossage. TODO: think of a reliable workaround.196 if [ $? -ne 0 ] ; then197 echo "$0: backup of ${1} failed." 1>&2198 # I'm assuming that the tar will have written an empty199 # file to the tape, otherwise I should do a cat here.200 else201 flush_level_log ${remotehost} ${fsname}202 fi203 ${MT_STATUS} "$TAPE_FILE"204 ${DUMP_END-:} $DUMP_LEVEL $remotehost $fs $fsname205 echo "sleeping ${SLEEP_TIME} seconds"206 sleep ${SLEEP_TIME}207 shift208 done209 210 # Dump any individual files requested.211 212 if [ "x${BACKUP_FILES}" != "x" ] ; then213 message 1 "processing individual files"214 215 date="`date`"216 217 if [ $DUMP_LEVEL -eq 0 ]; then218 make_level_log $localhost219 else220 echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"221 remote_run "${localhost}" cp "`level_log_name MISC $PREV_LEVEL`" "`level_log_name temp`"222 fi223 224 echo "Backing up miscellaneous files at ${date}"225 226 ${DUMP_BEGIN-:} $DUMP_LEVEL $localhost MISC MISC227 backup_host $localhost \228 "--listed=`level_log_name temp`"\229 "--label='`print_level` backup of miscellaneous files at ${NOW}'" \230 ${BACKUP_FILES}231 232 if [ $? -ne 0 ] ; then233 echo "Backup of miscellaneous files failed."234 # I'm assuming that the tar will have written an empty235 # file to the tape, otherwise I should do a cat here.236 else237 flush_level_log $localhost MISC238 fi239 ${MT_STATUS} "$TAPE_FILE"240 ${DUMP_END-:} $DUMP_LEVEL $localhost MISC MISC241 else242 echo "No miscellaneous files specified"243 fi244 message 1 "final cleanup"245 246 $MT_REWIND "${TAPE_FILE}"247 $MT_OFFLINE "${TAPE_FILE}"248 echo "."249) 2>&1 | tee -a "${LOGFILE}"250RC=$?251 252if test "${ADMINISTRATOR}" != NONE; then253 echo "Sending the dump log to ${ADMINISTRATOR}"254 mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"255fi256exit $RC257# EOF258 