Recursively batch rename files in Linux

If you’re using Kerio Mail Server (Kerio Connect) under Linux and you’ve moved around (using rsync or such) some mail folders, sometimes you’ll need to recreate the folder index, otherwise some email might not show up in your email program (Outlook, Mail.app, Webmail).
The only thing you’ll need to do is to rename the index.fld files inside each (!) folder into index.bad.

Yeah, nasty. So: fire up your Vi editor and put this little script inside your store folder:

badindex.sh
#!/bin/bash
find . -name "index.fld" -exec sh -c 'mv -v "$0" "${0%.fld}.bad"' {} \;

How to install language support for Ubuntu

How to install language support for Ubutnu:

If you want to add additional locales on your Ubuntu installation, check this out!  Go to the locales folder:

$ cd /usr/share/locales

In that directory is a script to add a new locale or language pack.  If you want to find out which language pack to install you can look at the /etc/locale.alias file.  To install, for examples,  Italian language support on Ubuntu, just run this command:

$ ./install-language-pack it_IT

Followed by:

$ dpkg-reconfigure locales

That’s it! Repeat these steps for any other locale you need.

Disable service startup in linux

My eTrayz NAS starts up with lots of services that I don’t need, like iTunes-server (mt-daapd), MiniDLNA, etc.
I put together this little script do easily check the current runlevel services and to disable them. Nothing spectacular but still 🙂

#!/bin/sh
chkconfig --list | grep "3:on"

echo "Enter service name:"
read service

echo "Removing $service"
chkconfig --del $service
for i in 1 2 3 4 5 6; do
chkconfig --level $i $service off
echo "Service $service in runlevel $i turned off."
done
chkconfig --list | grep -i $service

Linux: copy folders only

I needed a simple script that would copy the entire folder structure of certain users (under Linux) to a backup destination. This is how I did it.
You would need a file call users.txt with the names of the users, divided by linebreaks.

users.txt:
user1
user2
user3

And this little script:

createfolders.sh:

#!/bin/sh
dest='/backup'
cat users.txt | while read user;
do
base="/users/$user"
find $base -type d -print | cpio -pvdum $dest
done

Getting gcc on my eTRAYz


feed=http://ipkg.nslu2-linux.org/feeds/optware/cs05q3armel/cross/stable/
ipk_name=$(wget -qO- $feed/Packages | awk '/^Filename: ipkg-opt/ {print $2}')
wget $feed/$ipk_name
tar -xOvzf $ipk_name ./data.tar.gz | tar -C / -xzvf -
mkdir -p /opt/etc/ipkg
echo "src armel http://ipkg.nslu2-linux.org/feeds/optware/cs05q3armel/cross/stable/" > /opt/etc/ipkg/armel-feed.conf
wget http://mybookworld.wikidot.com/local--files/optware/sort_dirname.tar.gz
tar xvfz sort_dirname.tar.gz -C /
export PATH=$PATH:/opt/bin:.
ipkg update
ipkg install gcc

MRTG and SNMP on an eTRAYZ

ANd the next eTRAYZ mod: MRTG.

1. Install net-snmp
ipkg install net-snmp

2. Install mrtg
ipkg install mrtg

3. Change config snmpd.conf in /opt/etc/snmpd.conf to reflect these changes:
# rocommunity: a SNMPv1/SNMPv2c read-only access community name
# arguments: community [default|hostname|network/bits] [oid]

rocommunity public

# rwcommunity: a SNMPv1/SNMPv2c read-write access community name
# arguments: community [default|hostname|network/bits] [oid]

#rwcommunity SNMPv1_RW

4. Restart snmpd daemon with
/opt/etc/init.d/S70net-snmp restart

5. Make mrtg folder with
mkdir /opt/etc/mrtg

6. Create mrtg profile for localhost with
cfgmaker public@localhost >> /opt/etc/mrtg/mrtg.cfg

7. Make mrtg WorkDir with
mkdir /home/sysadmin/WWW/mrtg

8. Reflect this folder as WorkDir in
vi /opt/etc/mrtg/mrtg.conf

9. Make HTML file with
indexmaker --output=/home/sysadmin/WWW/mrtg/index.html /opt/etc/mrtg/mrtg.cfg

10. Make crontab entry with crontab -e and add
*/5 * * * * root cd /opt/bin/ && ./mrtg /opt/etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log

11. Restart cron with
crond restart

12. To be sure, reset permissions:
chown -R sysadmin:etrayz /home/sysadmin/WWW/mrtg/

13. Browse to http://etrayz/mrtg/index.html and enjoy.

vnstat Network stats on your eTRAYZ

I want to monitor the network usage on my eTRAYZ. So I installed vnstat and a php frontend.
Here’s a guide.

1. Install vnstat using ipkg.

ipkg update
ipkg install vnstat

2. Create vnstat database.
vnstat -u -i eth0

3. Change to the web folder and download the PHP frontend

cd /home/sysadmin/WWW
wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.4.tar.gz

4. Expand the archive, delete it and rename the folder
tar xvzf vnstat_php_frontend-1.4.tar.gz
rm vnstat_php_frontend-1.4.tar.gz
mv vnstat_php_frontend-1.4 vnstat

5. Change to the new directory and edit config.php using command

cd vnstat
vi config.php

and change the variable $iface_list so that the line reads
$iface_list = array('eth0');

6. Delete these two lines:
$iface_title['eth1'] = 'Internet';
$iface_title['sixxs'] = 'SixXS IPv6';

If you want you can change the friendly name for $iface_title[‘eth0’]. Do not change the definition of variable $vnstat_bin, this must be left blank because the php script that calls vnstat uses a “\” which is not allowed by the php service in safe mode. Safe and quit vi.

7. Consequently we have to set up a cronjob to dump the vnstat data in to the “dumps” directory defined in variable $data_dir. Make the directory dumps:
mkdir dumps

8. Now we need to make a script and cronjob to update the nvstat data and create the dumpfile. Make a folder for your scripts
mkdir /home/sysadmin/my_scripts

9. Create a script file for cronjob to run
vi /home/sysadmin/my_scripts/vnstatdump.sh
…and paste the following lines into it

#!/bin/bash
/opt/bin/vnstat -u
sleep 10
/opt/bin/vnstat --dumpdb -i eth0 >> /home/sysadmin/WWW/vnstat/dumps/vnstat_dump_eth0

10. Make the script executable by doing:

chmod +x /home/sysadmin/my_scripts/vnstatdump.sh

and test the script by running it
sh /home/sysadmin/my_scripts/vnstatdump.shvnstatdump.sh

There shouldn’t be any error messages.

11. Create the cronjob schedule

vi /etc/crontab

… and enter new lines for however frequently you want the web page data to be updated. For instance to have it updated every hour at 1 minute past the hour enter a line like
1 * * * * root /home/sysadmin/my_scripts/vnstatdump.sh

12. Restart crond, i.e. “crond restart”
Use your internet browser and open the index.php page: http://etrayz/vnstat/index.php. Done.

Rsync Kerio mailserver Store folder

This is the script I use on my Mac Mini Server to remotely backup the store directory on another server using rsync.


#!/bin/sh

if [ `id -u` != "0" ]; then
echo "Sorry, you are not root."
exit 1
fi

username=root
ip=192.168.0.7
dest=/opt/kerio/mailserver/store
folder=/usr/local/kerio/mailserver/store/

if [ $1 = "dry" ]; then
echo "Dry run activated. Simulating copy.";
dry=-n
fi;

if [ $1 = "log" ]; then
echo "Logging activated, directing output to /usr/local/kerio/log";
echo `date` >>/usr/local/kerio/log;
echo " " >>/usr/local/kerio/log;
options='-rumzh -v'
else
options='-rumzh -v --progress'
fi;

echo Options actived: $options $dry
echo ""
rsync $options $dry --delete-after --exclude=Outbox --exclude=tmp --exclude=logs --exclude=queue $username@$ip:$folder/. $dest

And this is the line in crontab:

*/5 * * * * /usr/local/kerio/sync.sh log > /usr/local/kerio/log

Resize LVM root volume

I created a standard Ubuntu 8.04 Server virtual machine in ESX 3.5 on a 2GB disk but then needed to resize the disk to 60GB. Here’s a guide how to do that, without having to reformat anything. I used LVM using the Ubuntu installer wizard.

Resize your existing disk in VMware Client (using an GParted ISO), and then reboot the virtual host. Then login and check new partition size:

root@ubuntu:~# parted /dev/sda print

Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags

1 32.3kB 255MB 255MB primary ext3 boot
2 255MB 2147MB 1892MB extended
5 255MB 2147MB 1892MB logical lvm

Information: Don’t forget to update /etc/fstab, if necessary.

Create new partition from available space. Start with the last end size (2147MB) and end with the total disk size (64.4GB).

root@ubuntu:~# parted /dev/sda "mkpart primary 2147MB 64.4G"

Check newly created partition:

root@ubuntu:~# parted /dev/sda print

Disk /dev/sda: 64.4GB Sector size (logical/physical): 512B/512B Partition Table: msdos

Number Start End Size Type File system Flags

1 32.3kB 255MB 255MB primary ext3 boot
2 255MB 2147MB 1892MB extended
5 255MB 2147MB 1892MB logical lvm
3 2147MB 64.4GB 62.3GB primary

Information: Don’t forget to update /etc/fstab, if necessary.

Create the physical volume from your new parition:

root@ubuntu:~# pvcreate /dev/sda3

Physical volume “/dev/sda3” successfully created

Check newly created physical volume:

root@ubuntu:~# pvdisplay /dev/sda3

— NEW Physical volume —
PV Name /dev/sda3
VG Name
PV Size 58.00 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 7gsSxU-Vsf7-QpJp-ZPlR-a3BG-x1b8-tz4pyV


root@ubuntu:~# pvs

PV VG Fmt Attr PSize PFree
/dev/sda3 lvm2 — 58.00G 58.00G
/dev/sda5 ubuntu lvm2 a- 1.76G 0

Extend your existing volume group with the newly created physical volume

root@ubuntu:~# vgextend ubuntu /dev/sda3

Volume group “ubuntu” successfully extended

Check it:

root@ubuntu:~# pvs

PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu lvm2 a- 58.00G 58.00G
/dev/sda5 ubuntu lvm2 a- 1.76G 0

root@ubuntu:~# vgdisplay

— Volume group —
VG Name ubuntu
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 59.75 GB
PE Size 4.00 MB
Total PE 15297
Alloc PE / Size 450 / 1.76 GB
Free PE / Size 14847 / 58.00 GB
VG UUID 5hZbAz-0AgU-I0ui-WNvs-2IgD-IjKi-5sHKi4

 

Now extend your logical volume with the free PE space (14547):

root@ubuntu:~# lvextend -l +100%FREE /dev/ubuntu/root

Extending logical volume root to 59.62 GB
Logical volume root successfully resized

Resize your root volume (can be done on a live sysyem):

root@ubuntu:~# resize2fs /dev/ubuntu/root

resize2fs 1.40.8 (13-Mar-2008)
Filesystem at /dev/ubuntu/root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 4

Performing an on-line resize of /dev/ubuntu/root to 15628288 (4k) blocks.

Perform a e2fsck on your root system, if needed. Or just reboot your server, which is safer as it will check the filesystem while it’s not mounted.

root@ubuntu:~# e2fsck -f /dev/ubuntu/root

/dev/ubuntu/root is mounted.

WARNING!!! Running e2fsck on a mounted filesystem may cause SEVERE filesystem damage.

Do you really want to continue (y/n)? yes

/dev/ubuntu/root: recovering journal

Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts /dev/ubuntu/root:
***** FILE SYSTEM WAS MODIFIED ***** /dev/ubuntu/root:
***** REBOOT LINUX *****
/dev/ubuntu/root: 45642/3907584 files (0.5% non-contiguous), 344059/15628288 blocks

Reboot your virtual guest and you’re done!