Very interesting thread. This has been a question of mine for some time, since I recently wrote a small script (be easy on me, this is my first script I wrote in linux) that would automate my backups some more.
I use 3x 2TB WD Green drives as my backup drives, used in a one hard drive at a time rotation. I also use a Thermaltake Hot Swap bay, which has the necessary cable configuration (I believe it actually disconnects the data cable first, and then the power. The ground being the last connection to sever. Don't quote me on this though)
I also use the SimpleBackup program to do the actual backing up of the files I want. I know it's not the most sophisticated, but it does the job I need it to do, and it is ... well, simple.
Here is the script I run to mount the hard drives. I run this 1 hour before the backup process starts. Also, note that I named the drives 'Backup HDD I', 'Backup HDD II', 'Backup HDD III', so the script looks for the 'Backup HDD' part to identify which drive to mount to '/media/Backups'.
Code:
#!/bin/bash
logfile=/home/gtrawoger/BackupDriveMount.log
seperator=------------------------------------
#partprobe # Commented out since it didn't make the system more stable, as intended
list=$(blkid | grep 'Backup HDD') # Search for 'Backup HDD' string in the listing of drives via the 'blkid' command
today=$(date) # Today's date for the logfile
drive=${list:0:9} # Extract the substring of the drive from the 'list' string
location=$(expr index "$list" LABEL=) # Start of procedure for finding the drivelabel. 'location' gets filled with the string location of 'LABEL='
location=$[$location+6] # Add 6 to location so that we are at the beginning of the disklabel string
temp=${list:$location} # Fill a temporary variable with the rest of the string, beginning with the disklabel.
location=$(expr index "$temp" '"') # To remove the rest of the string away from the disklabel, find the next occurence of a quote
location=$[$location-1] # Take one step back from there, and we know where the disklabel ends.
disklabel=${temp:0:$location} # Set the disklabel by extracting the substring for it.
if [ ${#drive} = 9 ] # If the length of the drive string is 9 (i.e. '/dev/sdc1' = 9) then the string is filled, otherwise it was not found
then
echo $seperator >> $logfile
echo $today >> $logfile
echo $drive >> $logfile
echo $disklabel >> $logfile
umount $drive # In case the drive got mounted some other way, try unmounting it that way. May produce error message.
e2fsck -p -f -C 0 $drive # Check the filesystem before mounting. This was added because of a suspicion that if the filesystem is corrupt, the computer hard locks.
mount $drive /media/Backups >> $logfile
rm -r /media/Backups/Backups/ # Remove Old Backups - only one, current backup needed
mkdir /media/Backups/Backups # Previous Command removes the Backup directory too. This recreates it.
else
echo $seperator >> $logfile
echo $today >> $logfile
echo Drive not Found! >> $logfile
fi
SimpleBackup looks for the 'Backups' directory under the '/media/Backups', and if it doesn't find it (no hard drive found) it stops the backup process and sends an email to me.
Now the reason this thread was interesting for me is because I have another script I run that checks to see if the backup process is done and if it is, unmount the drive ahead of time. This way, all I have to do at the end of the day is walk over to the server, pull out the hard drive and put another one in. I then take that hard drive home. This is how I get a daily offside backup of ~1TB of data.
Here is the unmount script I run at ~15:00. The SimpleBackup process will have both gzip and python running at the same time, so I simply check if they are running. Not the best method, but ok on a server that does nothing else.
Code:
#!/bin/bash
# init
if [ "$(pgrep gzip)" ]
then
if [ "$(pgrep python)" ]
then
echo "Backup is still running.";
exit
fi
else
umount /media/Backups
exit
fi
Is there a better way of doing this? It seems to run fine (no lock ups for weeks). Any suggestions? Do I need to run some of the mentioned commands, such as 'hdparm -Y /dev/sdX' or 'echo 0 - 0 > /sys/class/scsi_host/hostX/scan'???