Monday 22 March 2010

Linux folder backup script

Here is my script I use to backup a linux folder, firstly it mounts the backup drive and checks to see if it has mounted by reading a file named NOT_MOUNTED created by using
touch NOT_MOUNTED

Then using rsync it copies only new and modified files to the backup drive and excludes all .* files, this is to stop the backing up folders like the .Trash folder. It also creates and appends a log file stored in $LOGDIR.

Add as a cron job by:
crontab -e
add @daily /root/{filename} <-- name of script

Remember to change the file to make it executable
chmod +x {filename}

Usage:

$LOGDIR = log file
$SOURCE DIR =Source directory
$DESTDIR = Destination directory
# Backup SH file by Chris Hawkins
# Get date and time
DATETIME=$(date)
LOGDIR=/usr/log
SOURCEDIR=/home
DESTDIR=/backup
mount $DESTDIR

if [ -f $DESTDIR/NOT_MOUNTED ];
then

LOGMESSAGE='Backup NOT Mounted (ERROR) - Backup NOT completed'

echo $DATETIME $LOGMESSAGE >>$LOGDIR/backuplog

else

LOGMESSAGE='Backup Started'

echo $DATETIME $LOGMESSAGE >>$LOGDIR/backuplog

rsync -auqlP --delete --exclude='.*' $SOURCEDIR $DESTDIR
# Get date and time
DATETIME=$(date)
LOGMESSAGE='Backup Completed'
echo $DATETIME $LOGMESSAGE >>$LOGDIR/backuplog

fi

cd $LOGDIR
umount $DESTDIR

Friday 5 March 2010

Arduino and TMP36 (temperature sensor) Part 2

I have now added conversion from Celsius and Fahrenheit via a push button, so you just need to press and hold the button until the display shows either a C or an F then you can release the button and the conversion is done.

Latest code v1.4 can be found here http://code.google.com/p/arduino-fun/downloads/list
// Arduino - TMP36 and 7 segment LED v1.3
// By Chris Hawkins
// February 2010
// Temperature code from http://oomlout.com/TMP36/TMP36-Guide.pdf
//
// using bitRead help from http://arduinofun.com/blog/2009/12/06/connecting-a-7-segment-led-to-the-arduino-build-it/
//
// Changes from 1.2
// Push button added to pin 12
// Added conversion scales from Celsius and Fahrenheit
// streamlined splitting the two digits

// Code Start
// pins that 7 segment is connect to
int ledSegment[] = {
  2,3,4,5,6,7,8,9};

// Analog pin that TMP36 is connect to
int tempMonitor =0;

//pin for Switching from C to F
int pinSwitch =12;

// Button state for temperature scales
boolean displayType = false;
// encode the on/off state of the LED segments for the characters
// '0' to '9' and 'DP' into the bits of the bytes
const byte numDef[11] = {
  63, 6, 91, 79, 102, 109, 124, 7, 127, 103,128 };

void setup()
{
  // Serial.begin (9600);

  // set pinmode for switch
  pinMode (pinSwitch,INPUT);

  // Sets the pinMode for all LEDs
  for (int x = 0; x<=7; x++)
  {
    pinMode (ledSegment[x],OUTPUT);
  }

  // enable on by one each LED to test them.
  for (int x =0;x<=7;x++){
    digitalWrite (ledSegment[x],HIGH);
    delay (50);

  }
  // disable one by one to finish test of each LED
  for (int x =0;x<=7;x++){

    digitalWrite (ledSegment[x],LOW);
    delay (50);
  }
}

void loop(){

  // Check if button is pressed
  if (digitalRead(pinSwitch)) {
    displayType = !displayType;
    if (displayType) {
      setSegments(113); // send byte for C to display
      delay(1500);
    }
    else{
      setSegments(57); // send byte to F to display
      delay(1500);
    }
  }

  float temperature = getVoltage(tempMonitor);  //getting the voltage reading from the temperature sensor
  temperature = (temperature - .5) * 100;          //converting from 10 mv per degree wit 500 mV offset

  if (displayType){
    temperature = ((temperature *1.8) +32);
  }

  // Print temperature to serial for use in processing
  //Serial.println (temperature);

  // Split two digit number to send to display
  int digit1 = temperature / 10;
  int digit2 = int(temperature) % 10;
  // send ten's number to display
  setSegments( numDef[digit1] );

  // Delay to display the 10's digit of the temperature
  delay (1000);

  // send unit number to display and DP
  setSegments( numDef[digit2] +  numDef[10] );

  // Delay to display the unit digit of the temperature
  delay (2000);
}

void setSegments(byte segments)
{
  // for each of the segments of the LED
  for (int s = 0; s < 8; s++)
  {
    int bitVal = bitRead( segments, s ); // grab the bit
    digitalWrite(s+2, bitVal); // set the segment
  }
}

float getVoltage(int pin){
  return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
  // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}

Breadboard view of how I configured the Arduino:

[caption id="attachment_82" align="alignnone" width="300" caption="Designed using the Fritzing program"]Breadboard view[/caption]