LVM Snapshots komprimieren, löschen und archivieren

Aus Bluelupo's Wiki
Wechseln zu: Navigation, Suche

Bashscript run_compress_dd.sh

  1 #!/bin/bash
  2 ########## Variablendefinitionen ##########
  3 ARCHIVE_COUNTER="3"
  4 COMPRESS_LEVEL="5"
  5 BACKUP_PATH="/mnt/backup/diskdump"
  6 LOGFILE="/var/log/lvmsnap.log"
  7 LOCKPATH="/var/tmp"
  8 LOCKFILE="`basename $0.lck`"
  9 LOCKFILE_LVMSNAP="run_lvmsnap.sh.lck"
 10 MAILRECIPIENT="michael@localhost"
 11 USBDISK_DEV="/dev/backupdisk1"
 12 BACKUP_ROOTDIR="/mnt/backup/rsnapshot"
 13 DD_DIR="diskdump/`hostname`"
 14 LOCKFILE_MOVEPERM="move_perm.lck"
 15 ########## Funktionsdefinitionen ##########
 16 usage()
 17 {
 18    echo "Syntax: $0 [-h | -c <2...n> | -l <1-9> | -d | -s | -m]"
 19    echo ""
 20    echo " -h (help):             diese Hilfeseite"
 21    echo " -c (archive counter):  Anzahl der zu archivierenden Backupversionen (gueltiger Wert=>1)"
 22    echo " -l (compress level):   Qualitaet der Komprimierung (1=schnellste Kompr. 9=beste Kompr.)"
 23    echo " -d (delete only):      nur Anzahl n Backupversionen behalten, keine Komprimierung durchfuehren"
 24    echo " -s (shrink):           Backup durchfuehren"
 25    echo " -m (move):             Aeltesten Backupsatz auf externe Disk verschieben"
 26    exit 1
 27 }
 28 
 29 do_lock_moveperm()
 30 {
 31    LOCKFILE_TIMESTAMP=`stat $LOCKPATH/$LOCKFILE_MOVEPERM|egrep ^Modify:|cut -d" " -f2,3|cut -d"." -f1`
 32    if [ "$1" = "on" ]
 33       then
 34       if [ ! -e "$LOCKPATH/$LOCKFILE_MOVEPERM" ]
 35          then
 36          echo "Attention no log file $LOCKPATH/$LOCKFILE_MOVEPERM exists" >> $LOGFILE 2>&1
 37          echo "Trying create a lock file for moving the monthly backup sets" >> $LOGFILE 2>&1
 38          date '+%s' > $LOCKPATH/$LOCKFILE_MOVEPERM
 39          RC=$?
 40          if [ $RC -eq 0 ]
 41             then
 42             echo "Lock file $LOCKPATH/$LOCKFILE_MOVEPERM was created" >> $LOGFILE 2>&1
 43          elif [ $RC -ne 0 ]
 44             then
 45             echo "Lock file $LOCKPATH/$LOCKFILE_MOVEPERM could not be created" >> $LOGFILE 2>&1
 46          fi
 47       else
 48          echo "Verify lock file $LOCKPATH/$LOCKFILE_MOVEPERM from $LOCKFILE_TIMESTAMP for moving the monthly backup set" >> $LOGFILE 2>&1
 49          HISTORICAL_MOVEDATE=`cat $LOCKPATH/$LOCKFILE_MOVEPERM`
 50          CURRENT_MOVEDATE=`date '+%s'`
 51          # 30 Tage = 2592000 Sekunden 
 52          MOVE_INTERVAL=2592000
 53          # Alle 30 Tage einen Backup verschieben
 54          TIME2MOVE=`expr $CURRENT_MOVEDATE - $HISTORICAL_MOVEDATE`
 55       fi
 56    elif [ "$1" = "off" ]
 57       then
 58       rm -v $LOCKPATH/$LOCKFILE_MOVEPERM >> $LOGFILE 2>&1
 59       echo "Old lock file $LOCKPATH/$LOCKFILE_MOVEPERM is deleted from $LOCKFILE_TIMESTAMP" >> $LOGFILE 2>&1
 60 
 61 
 62    fi
 63 }
 64 
 65 do_lock()
 66 {
 67    if [ "$1" = "on" ]
 68       then
 69       echo " --> Processing begins now" >> $LOGFILE 2>&1
 70       pidof -x $0 > $LOCKPATH/$LOCKFILE
 71       echo "Set lock in file $LOCKPATH/$LOCKFILE" >> $LOGFILE 2>&1
 72    elif [ "$1" = "off" ]
 73       then
 74       rm -v $LOCKPATH/$LOCKFILE >> $LOGFILE 2>&1
 75       echo " --> Script run complete" >> $LOGFILE 2>&1
 76    fi
 77 }
 78 
 79 do_runtest()
 80 {
 81    if [ ! -e "$LOCKPATH/$LOCKFILE" -a ! -e "$LOCKPATH/$LOCKFILE_LVMSNAP" ]
 82       then
 83       do_lock on
 84    elif [ -e "$LOCKPATH/$LOCKFILE" ]
 85       then
 86       echo "No run available (lockfile: $LOCKPATH/$LOCKFILE)" >> $LOGFILE 2>&1
 87       print_message LOCK1
 88       create_logentry stop
 89       exit 2
 90    elif [ -e "$LOCKPATH/$LOCKFILE_LVMSNAP" ]
 91       then
 92       echo "No run available (lockfile: $LOCKPATH/$LOCKFILE_LVMSNAP)" >> $LOGFILE 2>&1
 93       print_message LOCK2
 94       create_logentry stop
 95       exit 3
 96    fi
 97    
 98 }
 99 
100 create_logentry()
101 {
102    if [ "$1" = "start" ]
103       then
104       LOGFILE_START_TIMESTAMP=`date '+%Y-%m-%d %H:%M:%S'`
105       echo "[Log started: $LOGFILE_START_TIMESTAMP]" >> $LOGFILE
106    elif [ "$1" = "stop" ]
107       then
108       LOGFILE_END_TIMESTAMP=`date '+%Y-%m-%d %H:%M:%S'`
109       echo "[Log ended: $LOGFILE_END_TIMESTAMP]" >> $LOGFILE
110    fi
111 }
112 
113 do_compress()
114 {
115    find $BACKUP_PATH -name "*.dd" -exec pbzip2 -q -$COMPRESS_LEVEL {} \; >> $LOGFILE 2>&1
116    RC=$?
117 }
118 
119 do_del_oldest_backup()
120 {
121    if [ $RC -eq 0 ]
122       then
123       BACKUP_COUNTER=`find $BACKUP_PATH -type d -name "????-??-??-????" | wc -l`
124       if [ $BACKUP_COUNTER -gt $ARCHIVE_COUNTER ]
125          then
126          SUM_BACKUPDIR=`ls -1r $BACKUP_PATH | wc -l`
127          TO_DEL_BACKUPDIR=`expr $SUM_BACKUPDIR - $ARCHIVE_COUNTER`
128          OLDEST_BACKUPDIR=`ls -1r $BACKUP_PATH | tail -$TO_DEL_BACKUPDIR`
129          for DEL in $OLDEST_BACKUPDIR
130             do
131             rm -rv $BACKUP_PATH/$DEL >> $LOGFILE 2>&1
132          done
133 
134       elif [ $BACKUP_COUNTER -le $ARCHIVE_COUNTER ]
135          then
136          echo "Number of archive versions ($ARCHIVE_COUNTER) within limits" >> $LOGFILE 2>&1
137       fi
138    elif [ $RC -ne 0 ]
139       then
140       echo "Error (RC=$RC) during compression" >> $LOGFILE 2>&1
141    fi
142    }
143 
144 print_message()
145 {
146    MISSINGARGUMENT="Bei der Option -$OPTION wurde ein falscher Wert angegeben"
147    NOTAVAILIBLEOPT="Option $2 ist nicht erlaubt und wird ignoriert"
148    TOSMALLARGUMENT="Bei der Option -$OPTION wurde ein zu kleiner Wert angegeben"
149    NO_RUN1="Es laueft noch die Komprimierung der LVM-Snapshots, bitte spaeter versuchen"
150    NO_RUN2="Es laeuft noch ein LVM-Snapshot, bitte spaeter versuchen"
151 
152    if [ "$1" = "MISSINGARGUMENT" ]
153       then
154       echo $MISSINGARGUMENT
155    elif [ "$1" = "NOTAVAILIBLEOPT" ]
156       then
157       echo $NOTAVAILIBLEOPT
158    elif [ "$1" = "$TOSMALLARGUMENT" ]
159       then
160       echo $TOSMALLARGUMENT
161    elif [ "$1" = "LOCK1" ]
162       then
163       echo $NO_RUN1
164    elif [ "$1" = "LOCK2" ]
165       then
166       echo $NO_RUN2
167    fi
168 }
169 
170 do_mailsend()
171 {
172    MAILSUBJECT=$1
173    sed -n "/\[Log started: $LOGFILE_START_TIMESTAMP\]/,/\[Log ended: $LOGFILE_END_TIMESTAMP\]/p" $LOGFILE | mailx -s "$MAILSUBJECT" $MAILRECIPIENT
174 }
175 
176 
177 exits_usbdisk()
178 {
179    if [ -e "$USBDISK_DEV" ]
180       then
181       BACKUP_ROOTDIR_CHECK=`mount|egrep rsnapshot|cut -d" " -f3`
182       if [ "$BACKUP_ROOTDIR_CHECK" = "$BACKUP_ROOTDIR" ]
183          then
184          RC=0
185       else
186          mount $USBDISK_DEV $BACKUP_ROOTDIR
187          if [ $? = "0" ]
188             then
189             RC=0
190          else
191             RC=1
192          fi
193       fi
194    else
195       RC=2
196    fi
197 }
198 
199 move_backup()
200 {
201    if [ ! -d "$BACKUP_ROOTDIR/$DD_DIR" ]
202       then
203       mkdir $BACKUP_ROOTDIR/$DD_DIR
204    fi
205    BACKUP_COUNTER=`find $BACKUP_PATH -type d -name "????-??-??-????" | wc -l`
206    if [ $BACKUP_COUNTER -gt 1 ]
207       then
208       echo "Try to move the oldest backup sets on external hard drives" >> $LOGFILE 2>&1
209       OLDEST_BACKUPDIR=`ls -1r $BACKUP_PATH | tail -1`
210       mv -v $BACKUP_PATH/$OLDEST_BACKUPDIR $BACKUP_ROOTDIR/$DD_DIR >> $LOGFILE 2>&1
211       echo "All backup sets on the external hard drive moved" >> $LOGFILE 2>&1
212    fi
213 }
214 
215 if [ $# -eq 0 ]; then
216    usage
217 fi
218 ########## Hauptprogramm ##########
219 while getopts hc:l:dsm OPTION
220    do
221    case $OPTION in
222       h)
223       usage
224       ;;
225 
226       l)
227       if [ $OPTARG -gt 0 -a $OPTARG -lt 10 ]
228          then
229          COMPRESS_LEVEL=$OPTARG
230          echo "Compress Level: "$COMPRESS_LEVEL >> $LOGFILE 2>&1
231       else
232          FALSEOPT=$1
233          print_message MISSINGARGUMENT $FALSEOPT
234       fi
235       ;;
236 
237       c)
238       if [ $OPTARG -lt 2 ]
239          then
240          FALSEOPT=$1
241          print_message TOSMALLARGUMENT $FALSEOPT
242          exit 2
243       elif [ $OPTARG -ge 2 ]
244          then
245          ARCHIVE_COUNTER=$OPTARG
246          echo "Archive Versions: "$ARCHIVE_COUNTER >> $LOGFILE 2>&1
247       else
248          FALSEOPT=$1
249          print_message MISSINGARGUMENT $FALSEOPT
250          exit 3
251       fi
252       ;;
253 
254       s)
255       create_logentry start
256       do_runtest
257       do_compress
258       do_del_oldest_backup
259       do_lock off
260       create_logentry stop
261       do_mailsend "[DD-BZIP2]: Komprimierung und Loeschen der aeltesten Diskdumps abgeschlossen"
262       ;;
263 
264       d)
265       RC=0
266       create_logentry start
267       do_runtest
268       do_del_oldest_backup
269       do_lock off
270       create_logentry stop
271       do_mailsend "[DD-BZIP2]: Loeschen der Diskdumps erfolgreich abgeschlossen"
272       ;;
273 
274       m)
275       create_logentry start
276       do_runtest
277       exits_usbdisk
278       if [ "$RC" != "0" ]
279          then
280          do_lock off
281          create_logentry stop
282          do_mailsend "[BACKUP-MOVE]: Verschieben der Diskdumps nicht durchfuehrbar [RC=$RC] USB-Device nicht verfuegbar!  Bitte USB-Anschlusskabel pruefen bzw. USB-Disk anschliessen."
283       fi
284 
285       do_lock_moveperm on
286       if [ $TIME2MOVE -gt $MOVE_INTERVAL ]
287          then
288          move_backup
289          do_lock off
290          do_lock_moveperm off
291          do_lock_moveperm on
292          create_logentry stop
293          do_mailsend "[BACKUP-MOVE]: Verschieben der Diskdumps erfolgreich durchgefuehrt"
294       else
295          do_lock off
296          create_logentry stop
297          do_mailsend "[BACKUP-MOVE]: Verschieben der Diskdumps nicht noetig, da Zeitintervall von 30 Tagen noch nicht erreicht"
298       fi    
299       ;;
300 
301       \?)
302       FALSEOPT=$1
303       print_message NOTAVAILIBLEOPT $FALSEOPT
304       exit 4
305       ;;
306    esac
307 done
308 shift `expr $OPTIND - 1`
309 
310 exit 0
311 #EOF