Saturday, November 10, 2012

LVM Snapshot

LVM Snapshot is one of the good methods  data backup and restore. You can take a lvm snapshot for huge data. Here i took lvm snapshot for 60G with 300M but you should actually calculate the size according to the data you take the backup.

-s, --snapshot OriginalLogicalVolume{Name|Path}

              Create  a  snapshot  logical volume (or snapshot) for an existing, so called original logical volume (or origin).  Snapshots provide a ’frozen image’ of  the  contents  of the  origin while the origin can still be updated. They enable consistent backups and online recovery of removed/overwritten data/files.  Thin snapshot is created when the origin  is  a  thin  volume  and the size is not specified. Thin snapshot shares same blocks within the thin pool volume.  The snapshot with the specified  size  does  not need  the  same amount of storage the origin has. In a typical scenario, 15-20% might be enough.  In case the snapshot runs out of storage, use  lvextend(8)  to  grow  it. Shrinking  a  snapshot  is  supported by lvreduce(8) as well. Run lvdisplay(8) on the snapshot in order to check how much data is allocated  to  it.   Note  that  a  small
amount  of  the  space you allocate to the snapshot is used to track the locations of the chunks of data, so you should allocate slightly more space than you actually need and  monitor  the rate at which the snapshot data is growing so you can avoid running  out of space.

So, for a 100 GB LV, We can start with 20 GB for the snapshot volume.


A snapshot volume ONLY contains the files that have been changed since it was created. i.e. If you only changed 2kb of files since the snapshot... the snapshot consumes 2kb of disk space. It is a good idea to plan on allowing the snapshot sufficient space to do what you need before the snapshot is removed... i.e. if you are going to make 100gb of changes after the snaphot is taken... you need to allocate 100gb to the snapshot.. so it can consume 100gb if needed.

Actually, the space that is in actual use by the snapshot can be easily seen after activating the volumes (e.g., with vgchange -a y) -- in percents of the total space allocated for the snapshot:

it is shown in the Allocated to snapshot row of the output of lvdisplay, or by the snap_percent option of lvs.

(I simply was examining the volumes without activating them -- that's why I couldn't see this info straight on.)

If, for example, it's 5%, then you can simply use lvreduce -l 5%LV (or a bit more) to recalim all the unused space from the snapshot volume.

We can take a backup of this using copy command or the dd command.

There is also auto extend option for copy on write in lvm snapshot, we need to specify the percent in /etc/lvm.conf file  auto extend  line.

Example:

[root@ tester~]# lvcreate -L300M -s -n test /dev/VG1/public
  Rounding up size to full physical extent 320.00 MB
  Logical volume "test" created
Here test is the name of the snapshot and its size will be 300M This is a snapshot for logical
volume public.

[root@ tester~]# mkdir /mnt/test
[root@ tester~]# lvdisplay

 --- Logical volume ---
  LV Name                /dev/VG1/test
  VG Name                VG1
  LV UUID                SetE4Z-x9oD-72dH-tKc3-PltJ-vbPk-HXGZmv
  LV Write Access        read/write
  LV snapshot status     active destination for /dev/VG1/public
  LV Status              available
  # open                 0
  LV Size                60.00 GB
  Current LE             1920
  COW-table size         320.00 MB
  COW-table LE           10
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

[root@ tester ~]# mount -t ext3 /dev/VG1/test /mnt/test

cd /mnt/test

[root@ test]# ls

It now has the same data like the logical group public.
cd

You can now remove a file or data from the original one and test it.
Then copy the same file from the snapshot. It works..

[root@ tester~]# tar -cf /dev/VG1/test /mnt/test
To take backup of the lvm snapshot.

[root@tester ~]# umount /dev/VG1/test /mnt/test
umount: /dev/VG1/test: not mounted
umount: /mnt/test: not mounted

[root@ tester~]# lvremove /dev/VG1/test
Do you really want to remove active logical volume test? [y/n]: y
  Logical volume "test" successfully removed

Monday, November 5, 2012

Reset the system default permissions using --setperms and --setugids.

Restore default system permissions in Linux using --setperms and --setugids.

1) To permissions on files and directories

for a in $(rpm -qa); do rpm --setperms $a; done

2) To reset uids and gids on files and directories :

for b in $(rpm -qa); do rpm --setugids $b; done












Password Protect a Directory Using htpasswd.

Password protect a directory in apache using .htaccess, so that only users with user name and password can access the website.

In order to set the password protected directory, you need to create a .htaccess with the  below lines

AuthType Basic
AuthName "Restricted Access"
AuthUserFile
Require user 


 And your httpd conf file must contain these lines to make the .htaccess work

Directory
Options Indexes Includes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny

For example if you need to protect a directory test, first check the user,group of the apache and change the permissions.

[root@tester ~]# egrep -iw 'User|Group' --color=auto /etc/httpd/conf/httpd.conf
# If you wish httpd to run as a different user or group, you must run
# User/Group: The name (or #number) of the user/group to run httpd as.
#  . On SCO (ODT 3) use "User nouser" and "Group nogroup".
#    suggested workaround is to create a user www and use that user.
#  NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
#  when the value of (unsigned)Group is above 60000;
#  don't use Group #-1 on these systems!
User apache
Group apache
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
# The path to the end user account 'public_html' directory must be
    # To enable requests to /~user/ to serve the user's public_html
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{User-agent}i" agent
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
# file in a language the user can understand.

[root@tester ~]# chown apache:apache test
[root@tester ~]# chmod 0660 test


Now you can generate the password using the htpassword for the user. here the user is tester

[root@tester ~]# htpasswd -c /home/tester/password tester
New password:
Re-type new password:
Adding password for user tester

[root@tester ~]# cat /home/tester/password
tester:V8Lg1v0SHqo72

Now you can keep the password file in the same location or different location and make a entry in the .htaccess file. Keep the password file in different location so that it cannot be accessible by the user when viewing the website.

.htaccess
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /etc/httpd/conf/password
Require user tester

Here i placed the password file in different location and entered the required user as tester for accessing.

It works!!

You also have the option of deleting the htpasswd user using the below syntax

[root@tester ~]# htpasswd -D /home/tester/password tester
Deleting password for user tester