Matteo Mattei

Hello, my name is Matteo Mattei and this is my personal website. I am computer engineer with a long experience in Linux system administration and web software development.

linkedin rss twitter google+ github facebook

Disk replace in Linux raid software

Sometimes it happens 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```
  1. Remove that partition from the Raid array: mdadm --manage /dev/md0 --remove /dev/sdb1

  2. Shutdown the pc, change the disk and power on the pc again.
  3. At this point if you type cat /proc/mdstat you should see [U_U].
  4. Copy now the partition table from a working disk (sda) to the new inserted disk (sdb): sfdisk -d /dev/sda | sfdisk /dev/sdb
  5. Add the new partition to the array: mdadm --manage /dev/md0 --add /dev/sdb1
  6. Done!

When you have finished, type

cat /proc/mdstat

and wait for the array rebuild.

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

Backup & Restore LDAP database

The today question is: did you ever happened to make an hot backup of an LDAP database? I will show you how to do it creating an LDIF file and then doing the respective restore (in the hope you will not really need it). After all it is quite simple…


ldapsearch -x -b "dc=example,dc=com" -h -D "cn=manager,dc=example,dc=com" -w secret_password "(objectclass=*)" > backup_file.ldif


The new database must be empty!

ldapadd -D "cn=manager,dc=example,dc=com" -x -w secret_password -h -f backup_file.ldif

Here there is the meaning of the parameters used:

  • -x specifies that you want to use the “sample authentication” (rather than SASL)
  • -b “dc=example,dc=com” indicates the BaseDN of the server, thus the position where we want to copy all nodes and entries.
  • -h is the address of the remote LDAP server.
  • -D “cn=manager,dc=example,dc=com” specifies the LDAP user that will bind to the remote server.
  • -w secret lets you to specify the password for the user you have previously chosed.
  • “(objectclass=*)” specify all entries in the database.
  • backup_file.ldif is the file, in LDIF format, where the backup will be executed.

How to transfer an entire website via ftp with recursive lftp

In these days I have to transfer some big websites from a server to another and the only way to do it was an FTP connection because the destination server did not provide any other type of access. Because of the number of files was big (about 12GB) I have created a little script to use with lftp opened into a screen session to don’t busy the terminal for days.

So I created a script called with the following content:

set ftp:ssl-allow no
open -u username,password
mirror -c -R /source-path /destination-path

Where the following fields are respectively:

username: user name for ftp access
password: password for ftp access ftp destination server
source-path: source path on local server
destination-path: remote path on the ftp (where / is the ftp rootdir)

To run the script is sufficient to open a screen session (if you want to leave the process in background on the source server) and issue this command:

lftp -f

Install imagemagick with PHP imagick extension on CentOS

To install imagemagick with PHP imagick extension on Linux CentOS you must follow these steps:

yum install ImageMagick
yum install ImageMagick-devel
pecl install imagick

If you have an error like this:

root@myhost [~]# pecl install imagick
downloading imagick-3.0.1.tgz ...
Starting to download imagick-3.0.1.tgz (93,920 bytes)
.....................done: 93,920 bytes
13 source files, building
running: phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
Please provide the prefix of Imagemagick installation [autodetect] :
building in /var/tmp/pear-build-root/imagick-3.0.1
running: /root/tmp/pear/imagick/configure --with-imagick
checking for egrep... grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking for C compiler default output file name... a.out
checking whether the C compiler works... configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details.
ERROR: `/root/tmp/pear/imagick/configure --with-imagick' failed

Look at your /tmp folder… pretty surely it is mounted with noexec flag. Remount it without noexec and retry:

mount -o remount,rw /tmp

At the end of the installation, create an inclusion file for module and restart apache:

echo "" > /etc/php.d/imagick.ini
/etc/init.d/httpd restart

Test the correct load of the imagick module with:

php -m | grep imagick

MySQL error 1153 in max_allowed_packet

Today I have got an anomalous error during a database import on mysql:

ERROR 1153 (08S01) at line 3854: Got a packet bigger than 'max_allowed_packet' bytes

To solve this is sufficient to edit the mysql configuration file (/etc/my.cnf on Linux) and fill a suitable big value for max_allowed_packet. In my case I set it to 100M.

Restart mysqld daemon and now the import will gone fine!

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 have 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 have a big project with hundreds of directories, so I created a little script to help me:

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

Replace “.” with your root folder.