Bash

Disk replace in Linux raid software

Sometimes happen that, after a long time of usage, one disk is going to be damage and starts to give some troubles… What’s happened when the disk is part of a software raid? If the failing disk is only one, all data are safe but you have to replace the disk as soon as possible in order to avoid very ugly surprises! I know that is always a very frustrating thing to change a disk from a raid software…

However, when a disk in RAID 5 or in RIAD 1 should be replaced, you have to follow these steps:

  1. Take a look at /proc/mdstat, if is all ok (i.e. you have to replace a disk that contains some corrupted sectors but the raid is yet integer) mark the bad partition as failed (sdb1 in my case), otherwise continue from step 2:
    mdadm --manage /dev/md0 --fail /dev/sdb1
  2. Remove that partition from the Raid array:
    mdadm --manage /dev/md0 --remove /dev/sdb1
  3. Shutdown the pc, change the disk and power on the pc again.
  4. At this point if you type
    cat /proc/mdstat

    you should see [U_U].

  5. Copy now the partition table from a working disk (sda) to the new inserted disk (sdb):
    sfdisk -d /dev/sda | sfdisk /dev/sdb
  6. Add the new partition to the array:
    mdadm --manage /dev/md0 --add /dev/sdb1

Done! ;-)

When you have finished, type

cat /proc/mdstat

and wait array rebuilding.

At the end of the process (that can take also some hours, depending on the size of the partition) you should have all “U” ([UUU]).

Script to remove .svn directories from a project

When you want to distribute your own sources without any .svn directories is sufficient to create an export of the project with this command:
svn export svn://path_to_repository projectname

But often I’ve not access to the repository, so I remove any .svn directory by hand.

For a couple of directories is not a problem but today I’ve a big project with hundreds of directory, so I realized a little script to help me:

find . -type d -name .svn -exec rm -r '{}' \;

Script to remove trailing tilde (~) from temporary files opened with kate

Using kate as text editor, temporary files that have been saved present an annoying trailing tilde that personally I consider useless. I’m used to be careful when I use something like text editor (good vim school!). So I realized this mini script in bash to remove temporary files when I’ve just saved the final document.

#!/bin/bash
rm `find $1 | grep "~$"`

The file above I’ve called rmm (I don’t say why) and I’m put it in /usr/bin/
After written and saved something, I only open a shell, go to source directory and run this command:
$ rmm .

Or run directly this command without moving to source directory: $ rmm /home/matteo/src
This script is trivial and can be obviously improved. If you want to contribute I will be happy to add your improvements.