Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts
Saturday, August 20, 2011

zfs Snapshot Commands Example


As i have already discussed in my previous post zfs filesystem and MySQL about zfs overview and two most important command zpool and zfs. I am going to continue with usage of zfs snapshots. It includes create a pool, Create file system, Taking a snapshot, Renaming Snapshots, Listing all snapshots, restoring from snapshot and Moving the snapshot to other location.

snapshot is a read-only copy of a file system or volume. Snapshots can be created almost instantly, and initially consume no additional disk space within the pool. However, as data within the active dataset changes, the snapshot consumes disk space by continuing to reference the old data and so prevents the space from being freed. Snapshots of volumes cannot be accessed directly, but they can be cloned, backed up, rolled back to.

Creating a Pool:
# zpool create zpool1 c2t0d0

List pool:
# zpool list

Create file system under above create pool:
Once you have a storage pool, you can build file systems on it:

# zfs create zpool1/data # zfs create zpool1/logs
Here we have built “/data” file system on pool zpool1

List all zfs file systems:
# zfs list

Taking a Snapshot:
zfs snapshot < pool name>/<filesystem name>@<snapshot name>
Example:
# zfs snapshot zpool1/data01@Snapshot1

Remove/Destroy a Snapshot:
zfs destroy < pool name>/<filesystem name>@<snapshot name>
Example:
# zfs destroy zpool1/data01@Snapshot1

Rename Snapshots:
You can rename snapshots but they must be renamed within the pool and dataset from which they were created.
zfs rename < pool name>/<filesystem name>@<snapshot name> < pool name>/<filesystem name>@<snapshot name>
Example:
# zfs rename zpool1/data01@Snapshot1 zpool1/data01@Snapshot2

Below snapshot rename operation is not supported because the target pool and file system name are different from the pool and file system where the snapshot was created.

# zfs rename zpool1/data01@Snapshot1 zpool3/data01@Snapshot2

Displaying zfs Snapshots:
zfs list
zfs list -t snapshot

You can also list snapshots that were created for a particular file system:
zfs list -r -t snapshot -o <name>,<creation> <pool>/<home>

Restore/Rolling Back zfs snapshots:
zfs rollback < pool name>/<filesystem name>@<snapshot name>
Example:
# zfs rollback zpool1/data01@Snapshot1

This will restore the entire file system with snapshot.

Restoring individual files:
It is possible to copy individual file from a snapshot by changing into the hidden “.zfs” directory of the pool that has been snapped.

cd /<pool name>/<file system name>
cd .zfs
cp <required file source location> <destination>

Example:
cd /zpool1/data01
cd .zfs
cp <required file source location> <destination>

Moving a  Snapshot to another system:
Wecan move the snapshot to another system and install it there as a usable file system. But at first we need to create a pool to receive the snapshot on the target system.

Step1: Create Pool on another system.
# zpool create -f zpool11 c2t0d0

Step2: Send the snapshot over the network and receive it into the pool using a combination of zfs send/receive command and a netwolrk pipe.
# zfs send zpool1/data01@snapshot1 | ssh <destination host> “usr/sbin/zfs receive zpool11/<myfilesystem>

Here zpool11 is the name of pool on another system which we have created above and myfilesystem is the name of filesystem you wish to put.

Thursday, July 14, 2011

getopts in shell script

getopts
The getopts command simplifies the task of validating and parsing command line options and arguments for your shell scripts.

Syntax:


getopts <optstring name> [arg...]

Example:


Step1: First I define all my option holding variables.

ListFiles=0
MoveFiles=0
email=""


Step2: While loop.

The following while statement loops through all the options and sets them to the corresponding variable. getopts returns true while there are options to be processed. The argument string, here "lme:h", specifies which options the script accepts. If the user specifies an option which is not in this string, it will go into * section which will display a help to use this script with examples. If the option is succeeded by a colon, the value immediately following the option is placed in the variable $OPTARG.


while getopts "lme:h" option; do
case "$option" in
l) ListFiles=1;;
m) MoveFiles=1;;
e) email="$OPTARG";;
h|*) helpFunction;;
esac
done

Script Call:
-----------------------------------------------------------------------------------------------------------------------------------------------
ExampleScript.sh -l

#It will go into case l) and set ListFiles=1.

ExampleScript.sh -m
#It will go into case m) and set MoveFiles=1.

ExampleScript.sh -m -e "example@gmail.com"
#It will go into case m) and e) and set MoveFiles=1 as well as get the email address in $Ovariable and set it to "email" variable.

ExampleScript.sh -h
#It will go into case h|*) and call the function helpFunction to show help.

ExampleScript.sh -<anything apart from optstringname we have provided>
#It will also go into case h|*) and will be treated as "*" and show the help.
------------------------------------------------------------------------------------------------------------------------------------------------


Comments/suggestions are welcome.. Happy scripting ... :)
 
Friday, July 1, 2011

GET SIZE OF DIRECTORY (EXCLUDE SUBDIRECTORY) IN UNIX

We all know du command to get the size of a directory. But the problem is when you use "du <directory name>" it will give you the list of all subdirectory including the directory you want with size.

Bt what if i only want the size of directory which i have passed as an argument and not all the subdirectory?

In that senario we can use:

du -sh <directory name>                              

Example 1:

du -h /home/mysql/admin/                             
   1K   /home/mysql/admin/scripts/neel               
   8K   /home/mysql/admin/scripts                    
   1K   /home/mysql/admin/bin-logs/test_instance_4   
   1K   /home/mysql/admin/bin-logs/test_instance_3   
   1K   /home/mysql/admin/bin-logs/orphan            
   1K   /home/mysql/admin/bin-logs/test_instance_1   
   1K   /home/mysql/admin/bin-logs/test_instance_2   
   9K   /home/mysql/admin/bin-logs                   
  20K   /home/mysql/admin                            


In the above example i have have passed "/home/mysql/admin/" as an argument of du and it results all subdirectory with size. (Please note -h switch converts the size into human redable and understandable format i.e KB).

Example 2:

 du -sh /home/mysql/admin/
  20K   /home/mysql/admin

In this example i have used switch "s" ( to show the size of current directory and not the subdirectory) along with "h" (human redable format) and it gave me the size of "/home/mysql/admin"
directory only rather than all subdirectories also.

I hope this will help someone. :)
Tuesday, June 28, 2011

Turn on or off color syntax highlighting in vi or vim editor

Vim or vi is a text editor. It can be used to edit all kinds of plain text. It is especially useful for editing programs or UNIX/Linux configuration files.

Turn on syntax highlighting:


Open file (for example test.sh):
$ vi test.sh
Now press ESC key to enter into command mode then type  ” : syntax on OR syn on”
:syntax on
OR
:syn on
That’s it.. the color syntax highlighting will be enabled until you close that file (useful when you working on server where you can’t enable it permanently because of restriction and not having desired access of .vimrc file.)

Turn off syntax highlighting:


Press ESC key to enter into command mode then type “: syntax off OR syn off”
:syntax off
OR
: syn off

Enable color syntax highlighting permanently:


You may need to add "syntax on" (or "syn on") in your $HOME/.vimrc file
$ vi HOME/.vimrc
Add "syntax on" (or "syn on")
Press ESC to enter into command mode then
Save and close the file.
:wq <enter>

Please note: .vimrc is a system file so it will be hidden and you can see it by using command “ls”

Any comment would be appreciated thanks J

Tuesday, February 15, 2011

Linux Administration

Check out this SlideShare Presentation:

Linux Introduction (Commands)

Check out this SlideShare Presentation:
Thursday, February 3, 2011

Linux performance tuning & stabilization tips (mysqlconf2010)

 
© Copyright 2010-2012 Learn MySQL All Rights Reserved.
Template powered by Blogger.com.