Oct 26, 2010

Shrink EXT3 partition Without Losing Data

Shrink EXT3 partition Without Losing Data


RedHat Enterprise Linux


Resize the ext3 partition without losing data


This article shows how to shrink the ext3 partition without losing data. This can be quite useful if you do not use LVM

Note: The partition that is to be resized must be unmounted when we do the resizing

Shrinking an ext3 Partition:

1) Check the disk usage of the server before proceeding

          # df –h

          Filesystem   Size       Used     Avail      Use%       Mounted on
          /dev/sda3    4.4G       2.4G     1.8G       58%          /
          /dev/sda1    99M        14M      80M        15%          /boot
          tmpfs       125M       0        125M        0%          /dev/shm

2) Boot the server into linux rescue mode

Note: If the partition you want to resize doesn't hold any system files, you can do everything from the original system; the steps are the same, just omit booting into rescue system

3) Run the filesystem-specific fsck on the filesystem.

               # fsck –n /dev/sda3

         e2fsck 1.39 (29-May-2006)
         /dev/sda3: clean, 115974/577152 files, 636274/1152640 blocks

4) Remove the journal from /dev/sda3, thus turning it into an ext2 partition

         # tune2fs -O ^has_journal /dev/sda3

         tune2fs 1.39 (29-May-2006)

5) Run fsck on the filesystem

         # e2fsck –f /dev/sda3

         e2fsck 1.39 (29-May-2006)
         Pass 1: Checking inodes, blocks, and sizes
         Pass 2: Checking directory structure
         Pass 3: Checking directory connectivity
         Pass 4: Checking reference counts
         Pass 5: Checking group summary information
         /dev/sda3: clean, 115974/577152 files (0.5% non-contiguous), 636274/1152640 blocks

6) Now resize the filesystem using resize2fs. resize2fs can resize ext2 file systems, but not ext3 file systems

         # resize2fs /dev/sda1 3600M

         resize2fs 1.39 (29-May-2006)
         Resizing the filesystem on /dev/sda3 to 921600 (4k) blocks.
         The filesystem on /dev/sda3 is now 921600 blocks long.

Please take note of the amount of blocks (921600) and their size (4k).

Note: Currently, 2.3 GB are used on /dev/sda3 (see the df -h output above), so it's safe to shrink it from 4.4GB to about 3.6GB (if you make it smaller than 2.3GB, you will lose data!).

7) Now we delete our /dev/sda1 partition (don't be afraid, no data will be lost) and create a new, smaller one (but still big enough to hold our resized file system!).

        # fdisk /dev/sda

        Command (m for help): d
        Partition number (1-4): 3
        Command (m for help): n
        Command action
        l logical (5 or over)
        p primary partition (1-4)
        Partition number (1-4): 3
        First cylinder (79-652, default 79):
        Using default value 79
        Last cylinder or +size or +sizeM or +sizeK (79-652, default 652): +3870720K

        Command (m for help): w
        The partition table has been altered!
        Calling ioctl() to re-read partition table.
        Syncing disks.

Note: We multiply the amount of blocks from the resize2fs output (921600) by the size of a block (4k), and to go sure the partition is big enough, we add 3 to 5% to it (5% was enough for me, but if you want to go sure take 3%)

                   921600 * 4k * 1.05 = 3870720k

8) Now restart the server once and boot into the rescue mode once again

        # exit

9) Once again run the filesystem-specific fsck on the filesystem

        # fsck –n /dev/sda3

        fsck 1.39 (29-May-2006)
        e2fsck 1.39 (29-May-2006)
        /dev/sda3: clean, 115962/464928 files, 599592/921600 blocks

10) Now create the journal on our new /dev/sda3, thus turning it into an ext3 partition again

        # tune2fs -j /dev/sda3

        tune2fs 1.39 (29-May-2006)
        Creating journal inode: done
        This filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.

11) Now restart the server and boot from the harddisk.

        # exit

Oct 17, 2010

Sync Data between Two Servers

Sync Data between Two Servers


RedHat Enterprise Linux


Sync the data between two linux servers



a) Source server ip address:

b) Destination server ip address:

Source server - The server we are connecting from to upload the data

Destination server - The server we are connecting to receive the data

Setting the SSH key authentication:

1) Make sure the Destination server have the ability to use key authentication enabled. In the sshd configuration file (usually ‘/etc/ssh/sshd_config’) enable the following options if they are not already set.

# vi /etc/ssh/sshd.conf

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

2) In Source server create the public / private key pair to used for authentication with the following command

# ssh-keygen -t rsa

Note: Do not enter a passphrase for this, just hit enter when prompted.

3) Now two files (public and private key) are created in the home directory of the user. If you are execute this as a root user means the files will be in,

# /root/.ssh/id_rsa.pub (public key file)

#/root/.ssh/id_rsa (private key file)

4) Now upload the public key to the Destination Server

# scp /root/.ssh/id_rsa.pub

Note: Be sure to keep this private key safe. With it anyone will be able to connect to the Destination Server that contains the public key.

5) In the Destination Server rename the public key file ( id_rsa.pub) to “authorized_keys”

# cd /root/.ssh

# mv id_rsa.pub authorized_keys

6) Change the file permission of that public key as well as ssh folder permission also

# chmod 600 /root/.ssh/authorized_keys

# chmod 700 /root/.ssh

7) Test the keys are working or not , by connecting the Destination Server from the Source Server

# ssh root@

If all is working it should not be prompted for a password but instead connected directly to a shell on the Destination Server.

Creating rsync script:

8) Create one simple rsync script to sync both the servers and place it into the user’s home directory

# vi /root/rsync.sh







echo $’\n\n’ >> $LOGFILE


echo “Completed at: `/bin/date`” >> $LOGFILE

Note: In this script 4 variables are used

SOURCEPATH - Source path to be synced

DESTPATH - Destination path to be synced

DESTHOST - Destination IP address or host name

DESTUSER - User on the destination server

The script will send all output to the ‘rsync.log’ file specified in the script

9) Give the executable permission for this script

# chmod 700 /root/rsync.sh

10) Now run the script and check, it is connect to the Destination Server, and transfer the files all without your interaction.

Setting up the cron job:

11) Setup a cron job to run the script automatically at a predefined interval.

# crontab –e

0 * * * * /root/rsync.sh

This will run the script once in every hour. Your 2 servers should now be syncing the chosen directory once every hour.