Useful snippets #1: Backup drive check and GPU/disk temperature

I thought to share here some of the bash scripts I’ve made for maintaining my Gentoo Linux system.

1. Checking for a correct detachable drive before backing up with rsync.

In my case, the script checks whether the correct eSATA backup drive is mounted before doing the actual backup. It first checks whether there are anything mounted on /mnt/esata, and tries to mount a drive if possible; a failure produces an error message. If a drive is found, it checks the UUID of the drive to confirm that the drive is really a correct one, to prevent accidental loss of data.

You can find the UUID of your drive in /dev/disk/by-uuid/

  1. #!/bin/bash
  2.  
  3. if ! grep -q /mnt/esata /proc/mounts ; then
  4.     if ! mount /mnt/esata ; then
  5.         echo "\033[1;31m*\033[0m Backup drive 'BackupOne' is not present."
  6.         exit 1
  7.     fi
  8. fi
  9.  
  10. DRIVELETTER=`cat /proc/mounts | grep /mnt/esata | cut -f 1 -d ' '`
  11. DRIVEUUID=`ls -l /dev/disk/by-uuid/ | grep ${DRIVELETTER/\/dev\//} | awk '{ print $9 }'`
  12. DRIVEUSAGE=`df -h | grep ${DRIVELETTER} | awk '{ print $5 }'`
  13.  
  14. if [ ! ${DRIVEUUID} = 735aa2fb-4022-4f41-80c7-4977e51aa967 ] ; then
  15.     echo -e "\033[1;31m*\033[0m Wrong drive mounted in /mnt/esata: ${DRIVEUUID}"
  16.     exit 1
  17. fi
  18.  
  19. echo -e "\033[1;32m*\033[0m Backup drive 'BackupOne', UUID=${DRIVEUUID},"
  20. echo -e "\033[1;32m*\033[0m is present and mounted as ${DRIVELETTER}, ${DRIVEUSAGE} used."
  21. echo -e "\033[1;32m*\033[0m Commencing rsync..."
  22. echo
  23.  
  24. rsync /what/ever/you/want/to/sync

NOTE: I have the corresponding line in /etc/fstab:

UUID=735aa2fb-4022-4f41-80c7-4977e51aa967 /mnt/esata ext3 noatime,users 0 1

2. Getting GPU and drive temperatures to a file.

This may seem somewhat complicated… I have four internal drives with SMART-compatible temperature sensors (sdb to sde, sda is a Compact Flash card). A cron job first reads the temperature of the ATI GPU, using /opt/bin/aticonfig --od-gettemperature and writes it on an intermediary file /tmp/atitemperature. The cron job then runs this script which reads the temperatures of the four internal drives and the two (possibly) mounted external drives, and writes these to the other intermediary file /tmp/temperature, which, when ready, is then added to the end of /var/log/temperature.log, which in turn is read by root-tail and displayed on the screen. The reason for those stupid intermediary files is the occasional unpredictable delays in reading the temperatures, which caused the output to flicker and jump around.

The checkremovable() function checks whether the removable drive exists and has SMART capability (i.e. if not, it is an USB thumb drive).

  1. #!/bin/bash
  2. # Check the temperatures of display adapter and hard drives.
  3.  
  4. checkremovable() {
  5.     if [ -b $1 ]; then
  6.         # Check whether it is a proper hard drive or an USB Flash drive
  7.         SDFTEMP=`/usr/sbin/hddtemp -q -f /usr/share/hddtemp/hddgentoo.db SATA:$1 2>&1`
  8.         if [ -n $"`echo $SDFTEMP | grep "no sensor"`" ]; then
  9.             /usr/sbin/hddtemp -q $1 >> /tmp/temperature
  10.         else
  11.             echo $SDFTEMP >> /tmp/temperature
  12.         fi
  13.     else
  14.         echo "$1: not present" >> /tmp/temperature
  15.     fi
  16. }
  17.  
  18. sleep 5
  19. cat /tmp/atitemperature | grep -A 1 "HD 46" > /tmp/temperature
  20. /usr/sbin/hddtemp -f /usr/share/hddtemp/hddgentoo.db SATA:/dev/sdb >> /tmp/temperature
  21. /usr/sbin/hddtemp -f /usr/share/hddtemp/hddgentoo.db SATA:/dev/sdc >> /tmp/temperature
  22. /usr/sbin/hddtemp -f /usr/share/hddtemp/hddgentoo.db SATA:/dev/sdd >> /tmp/temperature
  23. /usr/sbin/hddtemp -f /usr/share/hddtemp/hddgentoo.db SATA:/dev/sde >> /tmp/temperature
  24. checkremovable /dev/sdf
  25. checkremovable /dev/sdg
  26.  
  27. cat /tmp/temperature >> /var/log/temperature.log
This entry was posted in Linux and tagged , . Bookmark the permalink.