"Fossies" - the Fresh Open Source Software Archive

Member "rsync-backup-0.11/rsync-backup.sh" (20 Dec 2020, 4789 Bytes) of package /linux/privat/rsync-backup-0.11.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/bash
    2 exec 2>&1
    3 
    4 NUM_OWN_PROCESSES=$(pgrep -cf $0)
    5 if [ ${NUM_OWN_PROCESSES} -gt 1 ];
    6 then
    7     echo "Rsync-backup still running, not starting again..."
    8     exit 1
    9 fi
   10 
   11 if [ ! -f "/etc/rsync-backup.conf" ]
   12 then
   13     echo "No configuration file found in /etc/rsync-backup.conf. Exiting..."
   14     exit 1
   15 fi
   16 
   17 source "/etc/rsync-backup.conf"
   18 
   19 if [ ! -d `dirname ${STATUSFILE}` ]
   20 then
   21     mkdir `dirname ${STATUSFILE}`
   22 fi
   23 
   24 for EXCLUDE_DIR in $EXCLUDE
   25 do
   26     RSYNCOPTS="${RSYNCOPTS} --exclude=${EXCLUDE_DIR}"
   27 done
   28 
   29 if [ "${LIMIT}" != "" ]
   30 then
   31     RSYNCOPTS="${RSYNCOPTS} --bwlimit=${LIMIT}"
   32 fi
   33 
   34 if [ ! -f "${STATUSFILE}" ]
   35 then
   36     echo "No status file found, creating one"
   37     cat >${STATUSFILE} << EOF
   38 # This file was generated by rsync-backup.
   39 # DO NOT EDIT MANUALLY!
   40 
   41 NEXTDAILY=`date +%Y%m%d`
   42 NEXTWEEKLY=`date --date="+1 week" +%Y%m%d`
   43 NEXTMONTHLY=`date --date="+1 month" +%Y%m%d`
   44 EOF
   45 fi
   46 
   47 source "${STATUSFILE}"
   48 
   49 [ ${WEEKLY} -gt 0 ] && [ ${DAILY} -ne 7 ] && echo "WARNING: for proper functioning DAILY needs to be 7 when WEEKLY is set"
   50 [ ${MONTHLY} -gt 0 ] && [ ${WEEKLY} -ne 4 ] && echo "WARNING: for proper functioning WEEKLY needs to be 4 when MONTHLY is set"
   51 
   52 TODAY=`date +%Y%m%d`
   53 
   54 cmd="false"
   55 
   56 # Monthly rotate
   57 if [ ${NEXTMONTHLY} -le ${TODAY} ]
   58 then
   59     echo "Rotating monthly snapshots"
   60     cmd="${cmd}; [ -d ${DESTDIR}/monthly.${MONTHLY} ] && rm -Rf ${DESTDIR}/monthly.${MONTHLY} "
   61     for (( i=${MONTHLY}-1 ; $i > 0; i-- ))
   62     do
   63         cmd="${cmd}; [ -d ${DESTDIR}/monthly.${i} ] && mv ${DESTDIR}/monthly.${i} ${DESTDIR}/monthly.$[${i} + 1]"
   64     done
   65     cmd="${cmd}; [ -d ${DESTDIR}/weekly.4 ] && mv ${DESTDIR}/weekly.4 ${DESTDIR}/monthly.1"
   66     NEXTMONTHLY=`date --date="+1 month" +%Y%m%d`
   67 fi
   68 
   69 # Weekly rotate
   70 if [ ${NEXTWEEKLY} -le ${TODAY} ]
   71 then
   72     echo "Rotating weekly snapshots"
   73     cmd="${cmd}; [ -d ${DESTDIR}/weekly.${WEEKLY} ] && rm -Rf ${DESTDIR}/weekly.${WEEKLY} "
   74     for (( i=${WEEKLY}-1 ; $i > 0; i-- ))
   75     do
   76         cmd="${cmd}; [ -d ${DESTDIR}/weekly.${i} ] && mv ${DESTDIR}/weekly.${i} ${DESTDIR}/weekly.$[${i} + 1]"
   77     done
   78     cmd="${cmd}; [ -d ${DESTDIR}/daily.7 ] && mv ${DESTDIR}/daily.7 ${DESTDIR}/weekly.1"
   79     NEXTWEEKLY=`date --date="+1 week" +%Y%m%d`
   80 fi
   81 
   82 # Daily rotate
   83 if [ ${NEXTDAILY} -le ${TODAY} ]
   84 then
   85     echo "Rotating daily snapshots"
   86     cmd="${cmd}; [ -d ${DESTDIR}/daily.${DAILY} ] && rm -Rf ${DESTDIR}/daily.${DAILY} "
   87     for (( i=${DAILY}-1 ; $i > 0; i-- ))
   88     do
   89         cmd="${cmd}; [ -d ${DESTDIR}/daily.${i} ] && mv ${DESTDIR}/daily.${i} ${DESTDIR}/daily.$[${i} + 1]"
   90     done
   91     cmd="${cmd}; [ -d ${DESTDIR}/daily.2 ] && cp -al ${DESTDIR}/daily.2 ${DESTDIR}/daily.1 && [ -f ${DESTDIR}/daily.1/timestamp ] && rm ${DESTDIR}/daily.1/timestamp"
   92     cmd="${cmd}; [ ! -d ${DESTDIR}/daily.2 ] && mkdir -p ${DESTDIR}/daily.1"
   93     NEXTDAILY=`date --date="+1 day" +%Y%m%d`
   94 fi
   95 cmd="${cmd}; /bin/true"
   96 
   97 echo -e "\nRotating started at `date`"
   98 ssh -p ${SSHPORT} ${DESTHOST} ${cmd}
   99 if [ $? -ne 0 ]
  100 then
  101     echo -e "Failed rotating at `date`\nBackup will terminate now."
  102     exit 1
  103 fi
  104 echo -e "Rotating finished at `date`\n"
  105 
  106 cat >${STATUSFILE} << EOF
  107 # This file was generated by rsync-backup.
  108 # DO NOT EDIT MANUALLY!
  109 
  110 NEXTDAILY=${NEXTDAILY}
  111 NEXTWEEKLY=${NEXTWEEKLY}
  112 NEXTMONTHLY=${NEXTMONTHLY}
  113 EOF
  114 
  115 for DIR in ${DIRS}
  116 do
  117     echo -e "##### Starting backup of ${DIR} at `date` #####"
  118     if nice rsync ${RSYNCOPTS} -e "ssh -p ${SSHPORT}" ${DIR} ${DESTHOST}:${DESTDIR}/daily.1/
  119     then
  120         echo -e "##### Finished backup of ${DIR} at `date` #####\n"
  121     else
  122         echo -e "!!!!! Failed backing up ${DIR} at `date` !!!!!\n"
  123     fi
  124 done
  125 
  126 if [ "${MYSQLMOUNTPOINT}" != "" ]
  127 then
  128     echo -e "Starting to make snapshot of MySQL at `date`"
  129     # Lock DB's
  130     rm -f /root/mysqlpipe
  131     mknod /root/mysqlpipe p
  132     mysql </root/mysqlpipe &
  133     exec >/root/mysqlpipe
  134     for DB in `mysql -Bse "show databases"`
  135     do
  136         echo "USE $DB;"
  137         echo "FLUSH TABLES WITH READ LOCK;"
  138     done
  139     # Make snapshot
  140     lvcreate -L${MYSQLSNAPSHOTSIZE} -s -n dbsnapshot ${MYSQLLV} >/dev/null
  141     mount `dirname ${MYSQLLV}`/dbsnapshot ${MYSQLMOUNTPOINT}
  142     # Unlock DB's
  143     for DB in `mysql -Bse "show databases"`
  144     do
  145         echo "USE $DB;"
  146         echo "UNLOCK TABLES;"
  147     done
  148     echo "exit"
  149     exec >&2
  150     rm /root/mysqlpipe
  151     echo -e "Finished making snapshot of MySQL at `date`"
  152     # Do backup
  153     echo -e "##### Starting backup of ${MYSQLMOUNTPOINT} at `date` #####"
  154     if nice rsync ${RSYNCOPTS} -e "ssh -p ${SSHPORT}" ${MYSQLMOUNTPOINT} ${DESTHOST}:${DESTDIR}/daily.1/
  155     then
  156         echo -e "##### Finished backup of ${MYSQLMOUNTPOINT} at `date` #####"
  157     else
  158         echo -e "!!!!! Failed backing up ${MYSQLMOUNTPOINT} at `date` !!!!!"
  159     fi
  160     # Remove snapshot
  161     echo -e "Starting to remove snapshot of MySQL at `date`"
  162     umount ${MYSQLMOUNTPOINT}
  163     lvremove -f `dirname ${MYSQLLV}`/dbsnapshot >/dev/null
  164     echo -e "Finished removing snapshot of MySQL at `date`\n"
  165 fi
  166 
  167 ssh -p ${SSHPORT} ${DESTHOST} "date > ${DESTDIR}/daily.1/timestamp ; chmod -R u+w ${DESTDIR}/daily.1/"
  168