Advertisement
Guest User

hdd info backup

a guest
Jun 16th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.14 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. FROM='username@yandex.ru';
  4. TO='backup@yandex.ru';
  5. PASS='password';
  6.  
  7. FDISK='/sbin/fdisk -lu ';
  8. MDADM='/sbin/mdadm';
  9. LVM='/sbin/vgdisplay';
  10. MAIL_PRG='/usr/bin/sendEmail';
  11.  
  12. DIR='/tmp/hdd-info';
  13.  
  14. MBR_BYTES=2048;
  15. SEND_ATTEMPT_COUNT=3;
  16. SEND_ATTEMPT_PERIOD='1h';
  17. TEST_HOST='ya.ru';
  18. TEST_TRYS=3;
  19. TEST_DELAY='1h';
  20.  
  21. # ----------  Automatic params ----------
  22. HOST=`echo $FROM | cut -d@ -f 2`;
  23. USER=`echo $FROM | cut -d@ -f 1`;
  24. SERVER="smtp.$HOST";
  25. TITLE="hdd info (`hostname`)";
  26.  
  27.  
  28. # ==================== Functions ====================
  29.  
  30. debugInfo(){
  31.     [ 'qwerty' == "qwerty$VERBOSE" ] && return 0;
  32.     echo "$@";
  33. }
  34.  
  35. fname(){
  36.     echo $1 | grep -E -o [^/]+$ | sed -r 's/\.[^.]+$//';
  37. }
  38.  
  39. # Creates file in TMP with uniq (time based) name with given PREFIX.
  40. make_temp_file(){
  41.     local FUNC='makeTempFile()';
  42.  
  43.     [ 0 -eq $# ] && {
  44.         echo "$FUNC: prefix required" >&2;
  45.         exit 1;
  46.     }
  47.  
  48.     local PREFIX="$1";
  49.     local FILE="/tmp/$PREFIX-`date +%Y.%m.%d-%H.%M.%S`.txt";
  50.  
  51.     touch "$FILE" || {
  52.         echo "$FUNC: unable to create temp file '$FILE'" >&2;
  53.         exit 1;
  54.     }
  55.  
  56.     echo "$FILE";
  57. }
  58.  
  59. get_LVM_info(){
  60.     if [ -e $LVM -a -x $LVM ]; then
  61.         local FILE=`make_temp_file 'LVM'`;
  62.  
  63.         for cmd in vgdisplay lvdisplay pvdisplay; do
  64.             echo "---------- $cmd ----------" >> $FILE;
  65.             /sbin/$cmd --units b >> $FILE 2>/dev/null;
  66.             echo >> $FILE;
  67.         done
  68.  
  69.         for cmd in vgs lvs pvs; do
  70.             echo "---------- $cmd ----------" >> $FILE;
  71.             local OPT=$( echo $cmd | sed -r 's/s$/_all/' );
  72.             /sbin/$cmd --units b -o $OPT >> $FILE 2>/dev/null;
  73.             echo >> $FILE;
  74.         done
  75.     else
  76.         local FILE=`make_temp_file 'NO-LVM-HERE'`;
  77.     fi
  78.  
  79.     echo $FILE;
  80. }
  81.  
  82. get_RAID_info(){
  83.     if [ -e $MDADM -a -x $MDADM ]; then
  84.         local FILE=`make_temp_file 'RAID'`;
  85.  
  86.         for drive in /dev/sd* /dev/hd*; do
  87.             [ -b $drive ] || continue;
  88.        
  89.             echo "---------- $drive ----------" >> $FILE;
  90.             $MDADM --examine $drive >> $FILE 2>/dev/null;
  91.             echo >> $FILE;
  92.         done
  93.         unset drive;
  94.     else
  95.         local FILE=`make_temp_file 'NO-RAID-HERE'`;
  96.     fi
  97.  
  98.     echo $FILE;
  99. }
  100.  
  101. get_partitions_info(){
  102.     local FILE=`make_temp_file 'PARTITIONS'`;
  103.  
  104.     for drive in /dev/sd[a-z] /dev/hd[a-z]; do
  105.         [ -b $drive ] || continue;
  106.  
  107.         echo "---------- FDISK: $drive ----------" >> $FILE;
  108.         $FDISK $drive >> $FILE 2>/dev/null;
  109.         echo >> $FILE;
  110.     done
  111.     unset drive;
  112.  
  113.     echo $FILE;
  114. }
  115.  
  116. get_MBRs(){
  117.     local DIR=`make_temp_file 'MBR'` && rm -f $DIR;
  118.     DIR="/tmp/`fname $DIR`";
  119.     mkdir $DIR;
  120.  
  121.     for drive in /dev/sd[a-z] /dev/hd[a-z]; do
  122.         [ -b $drive ] || continue;
  123.  
  124.         local FILE="$DIR/mbr-`fname ${drive}-$MBR_BYTES`";
  125.         dd if=$drive of=$FILE bs=$MBR_BYTES count=1 2>/dev/null;
  126.     done
  127.  
  128.     local FILE="$DIR.tar.gz";
  129.     tar czf $FILE $DIR 2>/dev/null && \
  130.         rm -f $DIR/* && \
  131.         rmdir $DIR;
  132.  
  133.     echo $FILE;
  134. }
  135.  
  136.  
  137. # ==================== Logic ====================
  138. # ----- Настройка -----
  139. case $1 in
  140.     -v|--verbose|--debug)
  141.         echo "-= Debug mode =-";
  142.         VERBOSE='-v';
  143.         ;;
  144.     *)
  145.         VERBOSE='';
  146.         ;;
  147. esac
  148.  
  149.  
  150. # ----- Сбор инфы -----
  151. debugInfo "Info collect...";
  152.  
  153. PART_INFO=`get_partitions_info`;
  154. RAID_INFO=`get_RAID_info`;
  155. LVM_INFO=`get_LVM_info`;
  156. MBRs=`get_MBRs`;
  157.  
  158. [ -x $MAIL_PRG ] || {
  159.     echo "There is no email program '$MAIL_PRG'" >&2;
  160.     exit 1;
  161. }
  162.  
  163.  
  164. # ----- Проверка сети -----
  165. debugInfo "Network check...";
  166.  
  167. TEST_COUNT=0;
  168. while [ $TEST_COUNT -lt $TEST_TRYS ]; do
  169.     if ( ping -c 3 $TEST_HOST 1>/dev/null 2>/dev/null ); then
  170.         debugInfo "Network is UP";
  171.         break;
  172.     else
  173.         debugInfo "Network is DOWN, sleeping for $TEST_DELAY";
  174.         sleep $TEST_DELAY;
  175.     fi
  176.     TEST_COUNT=$(( TEST_COUNT + 1 ));
  177. done
  178.  
  179.  
  180. # ----- Отправка -----
  181. debugInfo "Sending...";
  182.  
  183. MAIL_CMD="$MAIL_PRG $VERBOSE -f $FROM -t $TO -s $SERVER -xu $USER -xp $PASS -o message-charset=UTF-8 -u '$TITLE' -m 'Будь счастлив' -a $PART_INFO -a $RAID_INFO -a $LVM_INFO -a $MBRs";
  184.  
  185. debugInfo "$MAIL_CMD";
  186.  
  187. $MAIL_CMD 1>/dev/null && debugInfo "HDD info is mailed successfully" || {
  188.     echo "Error sending email" >&2;
  189.     exit 1;
  190. }
  191.  
  192.  
  193. # ----- Заметание следов -----
  194. debugInfo "Removing temporary data...";
  195.  
  196. rm -f $PART_INFO;  
  197. rm -f $RAID_INFO;  
  198. rm -f $LVM_INFO;   
  199. rm -f $MBRs;
  200.  
  201. debugInfo "Good luck!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement