Single table inheritance (单表继承)

Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this:

class Company < ActiveRecord::Base; end
class Firm < Company; end
class Client < Company; end
class PriorityClient < Client; end

When you do Firm.create(:name => "37signals"), this record will be saved in the companies table with type = “Firm”. You can then fetch this row again using Company.where(:name => '37signals').first and it will return a Firm object.

If you don’t have a type column defined in your table, single-table inheritance won’t be triggered. In that case, it’ll work just like normal subclasses with no special magic for differentiating between them or reloading the right type with find.

Note, all the attributes for all the cases are kept in the same table. Read more: www.martinfowler.com/eaaCatalog/singleTableInheritance.html

Posted in Uncategorized

15 Things for a Ruby Beginner

The following is a post I had recently sent the Bangalore Ruby User Group. It has been slightly modified to address a larger audience.

There were many Ruby beginners in last week’s meetup, and the common question we heard was ‘after the very basics, what next?’

The best way to learn Ruby best practices is to pair with an experienced dev; the way I learned was by inheriting a reasonably small, but well-written codebase from an amazing colleague. In the absence of either, here is a checklist of 15 things (since ‘N things that you need to know about X’ is the in-thing these days!) that I’d recommend a Ruby beginner to consider:

1. The very basics

Our very own rubymonk.com has a Ruby primer which was written for exactly this purpose; we open our inbox everyday to gushing feedback from people who’ve found it to be a great way to learn Ruby. Try it and let us know how it goes!

tryruby.org also has a basic introduction to Ruby, and has been around longer. Edgecase’s Ruby Koan is an interesting concept, and covers the language both in breadth and depth, and is a very strong recommendation. It should take you anywhere between 5-10 hours to finish all of the Koans. Do try it!

 

I have heard good things about Learn Ruby the Hardway, but haven’t tried it out myself. Okay, I just skimmed through portions of it and I’m not really happy – LRTH seems to be mostly a line-to-line translation of Python code to Ruby. It uses  ‘while’ loop in places where equivalent Ruby idioms (Enumerables) would have made more sense. Also there is no mention of blocks, metaprogramming and duck-typing, which pretty much is a deal-breaker for me. But to be fair, the target audience for LRTH seem to be non-programmers for whom the concept of loops and objects would be new, and for them it does the job very well.

Wait, have you read Why’s Poignant Guide to Ruby? If this is the first time you’re hearing about why the lucky stiff, read this amazing piece on _why by the Smashing Magazine. And definitely read The Poignant Guide: http://mislav.uniqpath.com/poignant-guide/. It is full of cats, foxes, chunky bacon, cartoons that doesn’t always make much sense, space travel and what not. This was one of my first introductions to the Ruby community, and the guide lent the language and the community a fun, quirky and happy aura. You may or may not take away much Ruby knowledge from the guide – I couldn’t when I read it for the first time. However you’ll definitely understand some of the quirkiness and philosophies that influence the Ruby community. I’m a huge fan of _why, and here is my favourite quote:

 

when you don’t create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create.

 

2. The ecosystem – RVM/rbenv, RubyGems, Bundler, Git and Github

I think all of these tools are mandatory for being a productive Ruby programmer. You’ll encounter them soon enough:

– RVM/rbenv: these are tools used to manage multiple Ruby versions on the same machine. I’ve been using RVM without complaint for quite a while, though there are people who will go up in arms against RVM for various reasons. As a beginner, you should be comfortable with either.

– RubyGems: a gem for anything, a gem for everything. If you are using RVM, it will install RubyGems by default for you. http://docs.rubygems.org/read/chapter/4

– Bundler: You’ll learn it easy enough if you are using Rails. But even for non-Rails projects, Bundler is now the de-facto tool to handle gems and dependencies. It is one of those tools that when you see for the first time you would wonder how you ever lived without it.

– Git: You are a git if you don’t use git yet. If you are not even using any version control at all, good for you – there aren’t bad practices that you need to unlearn. If you are on SVN, or God forbid CVS, jump now.http://git-scm.com/book/en/Getting-Started-About-Version-Control

– Github: You have a Github handle, right? ’nuff said.

3. Editor

I don’t care. Pick one, use it well. If you’re on Vim and is on Insert mode all the time, then use Notepad instead. It would be more productive. Learn your editor.

Here is a list of editors/IDEs people generally use for Ruby development:

– Sublime Text
– Textmate
– RubyMine (My favourite, but needs a lot of memory and CPU)
– Vim
– emacs
– Aptana RadRails (recently saw a couple of people using it. don’t know how good it is)
– Redcar (I’ve used it very occassionaly, am yet to see someone using it as a primary editor)

If you are using Sublime Text, install and use its corresponding Ruby package. Ditto for Textmate.

If you are on Vim, using the right set of plugins is a requisite to be productive. There is the popularhttps://github.com/carlhuda/janus and Srushti’s https://github.com/srushti/vim-get which I use when I work with Vim. Even if you don’t go for these plugin distributions, spend enough time to find the right plugins for Ruby development.

I don’t know about the best plugins for emacs, but there are people who use emacs to develop in Ruby. Even Matz uses emacs; search and you shall find.

4. Ditch ‘while’, ‘for’ and array accumulation

Read this: http://martinfowler.com/bliki/CollectionClosureMethod.html

An apparent sign of a programmer who does not Ruby well is code that uses ‘for’ and ‘while’ loops for iteration and accumulation. I’m hardpressed to remember occasions where I had to use them instead of the Enumerable methods #each, #map, #select, #inject, #reject and #detect. Learn these methods, chew on them, and use it everywhere!

(infinite loops are almost always written using the loop do..end construct though. but how often do you write infinite loops anyway?)

5. Hash

At the time when I started writing Ruby, the languages that I had written in for a reasonable period of time before were CA-Clipper, Borland Turbo C and some VB 6.

The first two did not have a hash, associative array or dictionary – whichever you prefer to call it. As to VB6, the only thing I can remember is DataGrid and ADODB. Ah, the failed promises of drag and drop programming!

So Hash was a revelation and I started using it anywhere and everywhere. Do you want to build a CRUD app to manage customer info? Forget databases, I’ll build a Hash and serialize to and deserialize from a YAML file. There were even more crimes committed using Hash that I dare not mention here. You would have gone through enough exercises that uses Hash when working through RubyMonk or Ruby Koans. But if you haven’t, make sure you understand Hashes well enough. Specifically:

– iterating over a hash
– assigning default values for undefined keys in a hash
– Hash#keys and Hash#values for extracting just the keys and values
– In Ruby 1.8 Hashes are un-ordered: ie, you can’t rely on the ordering of the hash to be same as the order in which you added elements. In Ruby 1.9, a Hash is sorted on the basis of order of insertion.

6. JSON and YAML

These are not Ruby specific concepts, but find great use in the ecosystem. Know them well, they’ll come in handy.

7. Understand Immutability and how Ruby passes object references around

This has slightly got to do with the above point – all the Enumerable methods are immutable, and it is a good introduction to how functional Ruby veer towards immutability.  Immutability is more of a good programming practice than a Ruby specific idea – it helps you write clean predictable code, leave aside concurrent programming and race conditions. A method that mutates its parameter is a vile creature, don’t bring it forth into existence.

If you come from a C programming background, building new objects willy-nilly would be a little hard to digest. So much memory put to waste! I remember reading somewhere that programmers who use high level languages leave a higher carbon footprint because their code is inefficient. I leave you to ponder over it.

For understanding some quirks around Ruby’s immutability and interesting effects of how Ruby passes object references around, figure out where Array#clone is used. I remember wasting many a debugger breakpoint during my early days of Ruby because I didn’t realize this. Don’t let that happen to you! Understand the difference between a shallow clone and a deep clone. Even better, go write your own deep_clone routine! (limit yourselves to objects that can have strings, numbers, arrays and hashes)

Also read: http://ruby-doc.org/docs/Newcomers/ruby.html#objects

8. Ruby’s Object Hierarchy

 

# All objects are instances of the class Object.
"a string".is_a? BasicObject         # true

# All classes are instances of the class Class.
String.is_a? Class                   # true

# Class is a subclass of BasicObject.
Class.is_a? BasicObject              # true

# Class is not an instance of BasicObject
Class.instance_of? BasicObject       # false

# BasicObject is an instance and a sub-class of Class
BasicObject.is_a? Class              # true
BasicObject.instance_of? Class       # true

 

Okay, I lost it. It is pretty crazy: http://stackoverflow.com/questions/4967556/ruby-craziness-class-vs-object. As a beginner, you wouldn’t need to understand the nitty-gritties. I’ve been programming in Ruby for about three years now, and it still confuses the heck out of me.

For now it is safe to understand that BasicObject is usually the root object of all objects in Ruby. And everything in Ruby is an object. This has a very useful side-effect (try this in IRB):

"some random string".methods - Object.methods

Also,

Array.new.methods - Object.methods

 

The above commands will show you methods that are specific to just strings and arrays, excluding all methods that are always present in any Ruby object (inherited from Object -like instance_of, is_a? etc.).

Tip:You might have noticed that the ‘-‘ operator gives you the difference between two arrays. Whenever you need a general purpose method and wonder whether Ruby comes with it, just try some plausible syntax in IRB. You might be surprised at what you find.

Even though Ruby lets you Object Oriented and procedural code, the language leans toward OO. Ruby treats even methods as objects:

"some string".method(:length) # gives you an object of the Method class.

The method object can be asked to run by invoking the ‘call’ method on it.

 

9. Creating your own Objects

Did you notice that the title wasn’t ‘Creating Classes’. That was one of the most useful advices I’ve ever received: Always think in terms of objects – not classes. Thinking in terms of Classes can subtly make you evolve your design upfront. Don’t. Let your objects guide you in how your class definition should look. As a rough analogy, when building a home, the blueprint is valuable only as a reference for building the actual home. You imagine what your home should look like and draw a blueprint accordingly, not the other way round.

Start with sparse classes, add methods and attributes as your objects demand it. Srushti puts it better: Imagine you’re an instance, and think about what you want to do and how you want to do it. You don’t want to give up your secrets (encapsulation). You don’t ask other people for information so you can do their work for them, you just tell them to do stuff for you (tell, don’t ask)

Ruby has a very simple syntax for defining classes and building objects. If you come from a Java/C# background, it’d be the first thing you look for. But even if you are a die-hard procedural ninja, trust me, thinking in terms of objects will help you write better programs, tackle complexity and be a more capable programmer.

So, what are the things that are specific to Ruby that you need to be aware of?

 

– Message Passing. “abcd”.length is in fact “abcd”.send(:length).
– Module vs Classes (hint: they’re very similar, but you can’t instantiate a Module)
– Mixins (Ruby’s answer to multiple inheritance and the greatest thing _before_ sliced bread)
– attr_reader, attr_writer, attr_accessor.
– instance methods and class methods
– instance variables and class variables.

And we all know that you don’t use class variables unless you have a very good reason. Class methods aren’t that bad, but are usually a smell. Whenever you find yourselves writing a class method, take a step back and make sure it can’t be rephrased as an instance method, perhaps in a child object?

There is a lot more to OO, some less specific to Ruby. As you go deep into the rabbit hole, ponder over these blanket statements:

– Primitives (Hash, Array etc.) are evil! Build objects.
– Inheritance is evil! Use Composition.
– Conditions (if..else, switch..case) are evil! Use Polymorphism.

10. Ruby is interpreted. It is malleable. Use that to your advantage.

Interpreted programs are almost always slower than native code (which includes JIT). By choosing to use such a language, you are accepting a compromise in the speed/efficiency of your programs. But this gives you a great advantage: the flexibility to change your code at runtime. Though we can’t claim ‘code is data, data is code’ like those hipster LISPers do, there is tremendous power in the dynamism (no reference to type systems) of Ruby. Learn it, use it, change the world!

I had briefly mentioned the ‘send’ method that is available for every object in Ruby:

   "abcd".send(:length)

is same as

   "abcd".length

That means you can do things like this:

 

puts "Hi, which method do you like to invoke on a string today?"
method_name = gets.strip
puts "a random string".send(method_name)

 

Did you see that? Unlike fully compiled languages like C/C++, Ruby lets you call arbitrary methods during runtime! (you can pass arguments to the method as parameters to the ‘send’ method)

Leave aside calling arbitrary methods, running arbitrary code during runtime is a breeze:

puts "What code do you want to run today, dear sir?"
arbitrary_code = STDIN.read         # press ctrl+d to stop input
eval(arbitrary_code)

Try it, type in some short valid Ruby code and see it in action.

 

Now that you know ‘eval’ exists, forget about it. It is too dangerous to be almost ever used. It is unsafe and unscoped, but there are better things to achieve similar and useful results. The point of this exercise though was to see Ruby’s dynamic nature in action. Since Ruby is interpreted, there is no limitation on what can be done during runtime. This can be used to great good as we will see in Metaprogramming.

11. Metaprogramming

Metaprogramming in Ruby more or less gives you ways to create/remove/redefine methods at runtime. If you have used Rails, you would have seen that you would write something like

class User < ActiveRecord::Base
end

and magically, the User class gives you methods like user.name, user.find_by_name, user.find_by_name_and_id. Depending on the fields in the database, Rails defines methods for you to use. This uses Metaprogramming where Rails defines the methods at runtime after consulting the table schema.

(talking about ‘magic’, usually when someone complain about ‘magic’ in Ruby code, she is most probably referring to some sort of metaprogramming in the code)

Metaprogramming is one of Ruby’s most powerful concepts (anything borrowed from FP is yummy!), but it is open to use and abuse. They say that someone who knows metaprogramming well enough, but not enough to know where not to use it, is a danger to himself and society. The internet is rife with discussions around it and you’ll find no shortage of flame wars, opinions and thankfully, documentation.

These are the methods you would want to look up to get a decent overview of metaprogramming in Ruby:

– define_method
– method_missing
– instance_eval
– class_eval

I would also recommend Yehuda Katz’s excellent explanation of Metaprogramming by relating it to the context of ‘self': http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/

Metaprogramming is a bit advanced, and if you don’t understand all or any of it the first time, don’t worry. Come back and take a look again later. Rinse and repeat. It is an acquired taste, give it time!

12. Closures (Blocks, Lambdas et al.)

Blocks are my favourite. They move mountains. Rather, they let you write beautiful DSLs when coupled with the right dose of metaprogramming. Have you seen factory_girl’s syntax? It is an unholy mix of method_missing and ‘yield’.

Factory :user, aliases: [:author, :commenter] do first_name "John" last_name "Doe" date_of_birth { 18.years.ago } end

It is not really hard to build a DSL that reads like this, and there is no better resource to learn all of this than http://rubysource.com/functional-programming-techniques-with-ruby-part-ii/. The first part of that series looks into the functional and immutable aspects of Ruby, and is also a recommended read:http://rubysource.com/functional-programming-techniques-with-ruby-part-i/

13. Styleguides

Whenever you are in doubt, or the self becomes too much with you, go read the Ruby style guides.

Github’s simpler style guide: https://github.com/styleguide/ruby

The comprehensive one: https://github.com/bbatsov/ruby-style-guide

14. Simplicity is virtue

Knowing what constructs to use where is a matter of knowledge and experience. Every approach has trade-offs in terms of readability, maintainability and efficiency. The battle between these have been the recurring theme in the battles programmers fight in their heads for years. Knowing the the trade-offs will help you make more informed decisions, but it might not always be enough. Some things need to be tried, tested and failed, and that is fine.

But be vary of Premature Optimization. When you have a choice between clever, short and maybe faster code Vs longer but readable code, go for readability.  Ruby makes it easy to write really bad code that people would fear to touch with a long pole. It also lets you write  beautiful and concise code. When you contemplate between the two, remember the joke about the psychopath who’ll inherit your codebase, knows where you live, and pings you from your local network! The choice is yours.

15. None of this matters

If you are overwhelmed by this document or any links referenced from here, just ignore it. Remember the10,000 hours rule. Happily go about writing code the way you know best! And write a bit more code. Try to pair with someone who knows things a bit more. Go read some well-written Ruby code from Github. Then come back and see what you’ve learned.

None of this is rocket science, but it takes time and practice for concepts to sink in, and that is just fine.


Have further questions? There are tons of resource on the internet to answer your questions!

Join one of your local Ruby Usergroups. The Ruby community is extremely helpful and accomodating towards newbies. Check this page to locate a usergroup near you: How To Find Ruby User Groups

Participate in the usergroups, ask your questions. Also hop on to Ruby’s IRC channel #ruby-lang on Freenode. Irrespective of the forum, just make sure that you give enough context about your question to help others understand your problem. If you haven’t read ESR’s “Ask questions the smart way” yet, *this* is the time. Go read it now and get enlightened on the ways of the interwebs!

And remember to have fun! In Matz’s own words:

 “For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, So Ruby is designed to make programmers happy.”

Happy hacking!

Posted in Uncategorized

Using Resque to send mail for Devise

Since sending email synchronously is not a good idea, you’ll probably want to have Devise enqueuing it’s notification emails for background processing.

Although Devise doesn’t support this out of the box you can achieve it easily by using the devise-async gem.

To do so, first add it to your Gemfile:

gem "devise-async"

Then tell Devise to use the proxy mailer in config/initializers/devise.rb:

# Configure the class responsible to send e-mails.
config.mailer = "Devise::Async::Proxy"

And last but not least, set your queuing backend by creating config/initializers/devise_async.rb:

# Supported options: :resque, :sidekiq, :delayed_job
Devise::Async.backend = :resque

Your notification emails should now be gracefully enqueued for background processing.

 

#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path(‘../config/application’, __FILE__)
require ‘resque/tasks’

Askjane::Application.load_tasks

task “resque:setup” => :environment do
ENV['QUEUE'] ||= ‘*’
#for redistogo on heroku http://stackoverflow.com/questions/2611747/rails-resque-workers-fail-with-pgerror-server-closed-the-connection-unexpectedl
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
####run below command
#QUEUE=* rake environment resque:work

Posted in Uncategorized

解决Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER

部署完项目,测试一下,诶,数据出来了 没有多大问题(暗舒一口气)。继续测吧,一点新建完了,报错了,看看什么错误

一看完了 java.sql.SQLException: Illegal mix of collations (latin1_swedish_ci,IMPLICIT)

and (utf8_general_ci,COERCIBLE) for operation ‘=’ 是这个错误 ,什么原因呢,第一次遇到。

头大了。去查文档说是:结果集中有两种字符集。 我晕了 ,怎么会这样呢,看看表结构,一种啊。 继续查吧。

 代码如下 复制代码
SHOW VARIABLES LIKE ‘character_set_%'; 查看一下 显示+————————–+—————————-+

| Variable_name | Value |

+————————–+—————————-+

| character_set_client | utf8|

| character_set_connection | utf8|

| character_set_database | latin1 |

| character_set_results | utf8|

| character_set_server | latin1 |

| character_set_system | utf8 |

| character_sets_dir | /home/jh/mysql/share/mysql/charsets |

+————————–+—————————-+

再用 SHOW VARIABLES LIKE ‘collation_%'; 查看一下 显示

 代码如下 复制代码
+———————-+——————-+| Variable_name | Value |

+———————-+——————-+

| collation_connection | utf8_swedish_ci |

| collation_database | latin1_swedish_ci |

| collation_server | latin1_swedish_ci |

+———————-+——————-+

原来如此啊 哈哈 知道错在哪里 剩下的就好办了

解决方法:

依次执行:

 代码如下 复制代码
set character_set_database =utf8;

 

set character_set_results =utf8;

 

set character_set_server =utf8;

 

set character_set_system =utf8; –此处utf-8也可以

 

然后执行:

 

SET collation_server = utf8_general_ci

 

 

SET collation_database = utf8_general_ci

执行完之后,请检查mysql下每个数据库,表,字段是否都是utf8,不是则改过来,这样子就不会出现

 

最笨的方法是重装一下数据库。(一般不要用这种方法呀)

最终解决方法:

1.1 如果是windows版本的mysql,那么在安装的时候,系统就会提示用哪种编码。

如果安装的时候设置错误了,修改mysql安装目录下的my.ini文件:

 代码如下 复制代码
[mysql]default-character-set=utf8

# The default character set that will be used when a new schema or table is

# created and no character set is defined

default-character-set=utf8

配置好后,重启mysql。

1.2 如果是linux版本的mysql

 

修改mysql的配置文件,使数据库与服务器操作系统的字符集设置一致。

vi /etc/my.cnf 设置(如果没有发现这个文件,就新建1个)

 代码如下 复制代码
[mysqld]datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

default-character-set=utf8

(增加的关键一句,使得数据库缺省以utf8存储)

当然,修改后,要重启数据库。(这样设置后对新建的数据库表才起作用)

 

 代码如下 复制代码
用SHOW VARIABLES LIKE ‘character_set_%';命令查看到如下内容:+————————–+———————————————————————–+

| Variable_name | Value |

+————————–+———————————————————————–+

| character_set_client | utf8|

| character_set_connection | utf8|

| |character_set_database |utf8 |

| character_set_filesystem | binary |

| character_set_results | utf8|

| character_set_server | utf8 |

| character_set_system | utf8 |

| character_sets_dir | /home/jh/mysql/share/mysql/charsets |

+————————–+———————————————————————–+

发现关键项目已经用了utf8,但这样还不够,还要保证客户端也是用utf8的字符集来操作的。

登录的时候,要用以下命令:mysql –default-character-set=utf8 -u root -p

再次用SHOW VARIABLES LIKE ‘character_set_%';命令查看,结果变成了:

 代码如下 复制代码
+————————–+———————————————————————–+| Variable_name | Value |

+————————–+———————————————————————–+

| character_set_client | utf8 |

| character_set_connection | utf8 |

| character_set_database | utf8 |

| character_set_filesystem | binary |

| character_set_results | utf8 |

| character_set_server | utf8 |

| character_set_system | utf8 |

| character_sets_dir | /home/jh/mysql/share/mysql/charsets/ |

+————————–+———————————————————————–+

这样才能保证客户端所发命令都是基于utf8格式的,比如说建立数据库和表,默认就会以utf8编码,而无须再次指定。(再次说一句对新建的数据库和表起作用)。

另外:

第三种方法:网上看到的,先记录一下。

1.如果安装mysql的编码已不能更改,很多朋友是购买虚拟主机建立网站,无权更改MYSQL的安装编码,这一关我们可以跳过,因为只要后面的步聚正确,一样能解决乱码问题
2.修改数据库编码,如果是数据库编码不正确: 可以在phpmyadmin 执行如下命令: ALTER DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
以上命令就是将test数据库的编码设为utf8
3.修改表的编码:

 代码如下 复制代码
ALTER TABLE `category` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin

以上命令就是将一个表category的编码改为utf8
4.修改字段的编码:

 代码如下 复制代码
mysql> use askjane_development;
Database changed
mysql> alter database mydb character set utf8;
ERROR 1 (HY000): Can't create/write to file './mydb/db.opt' (Errcode: 2)
mysql> ALTER TABLE tags CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;Query OK, 2 rows affected (0.35 sec)

以上命令就是将test表中 dd的字段编码改为utf8
5.如果是这种情况容易解决,只需检查下页面,修改源文件的charset即可
, //这个正确就无问题了
6.这种情况也是修改页面charset即可.

 

Posted in Uncategorized

Logs Are Streams, Not Files

Server daemons (such as PostgreSQL or Nginx) and applications (such as a Rails or Django app) sometimes offer a configuration parameter for a path to the program’s logfile. This can lead us to think of logs as files.

But a better conceptual model is to treat logs as time-ordered streams: there is no beginning or end, but rather an ongoing, collated collection of events which we may wish to view in realtime as they happen (e.g. via tail -f or heroku logs --tail) or which we may wish to search in some time window (e.g. via grep or Splunk).

Using the power of unix for logs

Unix provides some excellent tools for handling streams. There are two default output streams, stdout andstderr, available automatically to all programs. Streams can be turned into files with a redirect operator, but they can also be channeled in more powerful ways, such as splitting the streams to multiple locations or pipelining the stream to another program for further processing.

A program that uses stdout for its logging can easily log to any file you wish:

$ mydaemon >> /var/log/mydaemon.log

(Typically you would not invoke this command directly, but would run this from an init program such as Upstart or Systemd.)

Programs that send their logs directly to a logfile lose all the power and flexibility of unix streams. What’s worse is that they end up reinventing some of these capabilities, badly. How many programs end up re-writing log rotation, for example?

Distributed logging with syslog

Logging on any reasonably large distributed system will generally end up using the syslog protocol to send logs from many components to a single location. Programs that treat logs as files are now on the wrong path: if they wisht to log to syslog, each program needs to implement syslog internally – and provide yet more logging configuration options to set the various syslog fields.

A program using stdout for logging can use syslog without needing to implement any syslog awareness into the program, by piping to the standard logger command available on all modern unixes:

$ mydaemon | logger

Perhaps we want to split the stream and log to a local file as well as syslog:

$ mydaemon | tee /var/log/mydaemon.log | logger

A program which uses stdout is equipped to log in a variety of ways without adding any weight to its codebase or configuration format.

Other distributed logging protocols

Syslog is an entrenched standard for distributed logging, but there are other, more modern options as well.Splunk, fast becoming a indispensable tool for anyone running a large software service, can accept syslog; but it also has its own custom protocol which offers additional features like authentication and encryption.Scribe is another example of a modern logging protocol.

Programs that log to stdout can be adapted to work with a new protocol without needing to modify the program. Simply pipe the program’s output to a receiving daemon just as you would with the logger program for syslog. Treating your logs as streams is a form of future-proofing for your application.

Logging in the Ruby world

Most Rack frameworks (Sinatra, Ramaze, etc) and Rack webservers (Mongrel, Thin, etc) do the right thing: they log to stdout. If you run them in the foreground, as is typical of development mode, you see the output right in your terminal. This is exactly what you want. If you run in production mode, you can redirect the output to a file, to syslog, to both, or to any other logging system that can accept an input stream.

Unfortunately, Rails stands out as a major exception to this simple principle. It creates its own log directory and writes various files into it; some plugins even take it upon themselves to write their own, separate logfiles. This hurts the local development experience: what you see in your terminal isn’t complete, so you have to open a separate window with tail -f log/*.log to get the information you want. But it hurts the deployment experience even more, because you end up having to tinker around with a bunch of Rails logger configuration options to get your logs from all your web machines to merge into a single stream.

Logging on Heroku

The need to treat application logs as a stream is especially poignant with Heroku’s new logging system. On the backend, we route logs with a syslog router written in Erlang called Logplex.

Logplex handles input streams (which we call “sinks”) from many different sources: all the dynos running on the app, system components like our HTTP router, and (currently in alpha) logs from add-on providers. Sinks are merged together into channels (each app has its own channel) which is a unified stream of all logs relevant to the app. This allows developers to see a holistic view of everything happening with their app, or to filter down to logs from a particular type of sink (for example: just logs from the HTTP router, or just logs from worker processes).

Further, log streams can also be sent outbound, which we call “drains.” Users can configure syslog drains, and we’re currently working up a technical design for how add-on providers can automatically add drains. This latter item will enable a new class of log search and archival add-on, most notably the emerging syslog-as-a-service products like Loggly and Papertrail.

This logging system works quite well, and it gets even better with the new features on the way – but it only works where all programs output their logs as streams. Programs that write logfiles, such as Rails in its default configuration, don’t make sense in this world.

As a workaround, Heroku injects the rails_log_stdout plugin into Rails apps at deploy time. We’d prefer not to have to do this (injecting code is a dicey way to solve problems), but it’s the best way to get Rails logs into the app’s logstream without requiring extra configuration from the app developer.

Conclusion

Logs are a stream, and it behooves everyone to treat them as such. Your programs should log to stdoutand/or stderr and omit any attempt to handle log paths, log rotation, or sending logs over the syslog protocol. Directing where the program’s log stream goes can be left up to the runtime container: a local terminal or IDE (in development environments), an Upstart / Systemd launch script (in traditional hosting environments), or a system like Logplex/Heroku (in a platform environment).

Posted in Uncategorized

Configuring Gedit for Rails

I haven’t kept my feelings about IDEs hidden, I’m a big believer in using text editors instead.

I know, I should earn my chops and become a Vim guy, and some day I hope to sit down and make that conversion, but for now it’s all about Gedit for me. With a few plugins and a little TLC gedit can be a lighter version of the more powerful, more intensive ide.

The first thing we’re going to install gMate, an addon designed to make Gedit run like TextMate

sudo add-apt-repository ppa:ubuntu-on-rails/ppa

sudo apt-get update

sudo apt-get install gedit-gmate

Additionally we’re going to install the standard plugins package

sudo apt-get install gedit-plugins

Now lets fire up Gedit and turn on our preferences.

To get to our plugins go to Edit > Preferences > Plugins.

We’re going to enable the following options:

  • Snippets
  • Code Comment
  • Embedded Terminal
  • Find in Files
  • Rails Extract Partial
  • Rails File Loader
  • Session Saver (Optional)
  • Smart Indent (Optional)
  • Tab Switch (Optional)
  • TextMate Style AutoCompletion

This is going to enable a lot of different functionality, and while this is the setup I use, it may be more than you need.

Now if you’re in a ruby file and you type def, tabbing over will add the end and place you straight on the method name, control+tab will switch between documents, and syntax highlighting will work correctly in html.erb files.

Also, going to view > bottom pane will display a terminal window that I find convenient for running irb.

While these instructions will enable a lot of different useful environments, and the target of this post was rails, I do have to sadly add that Google Go, which I cover quite often, does not, to my knowledge, have a plugin for Gedit currently.

Posted in Uncategorized

创建基于Rails 3的纯净Ajax CRUD程序

Rails 3利用scaffold可以很容易地创建CRUD程序,但那是多页面的,现在很多场景需要使用Ajax在一个页面上实现CRUD。这对Rails 来说也是很简单的,下面就来创建一个符合Rails风格的Ajax CRUD程序。整个过程大概15分钟,建议先把整个代码照应敲一遍,然后再慢慢理解。

目标:

只有1个页面,CRUD全部基于Ajax在一个页面完成。使用无入侵式的Javascript风格。

平台:

适用于Rails 3.*,使用jQuery+sqlite。
Rails 3.1开始默认自带jQuery,如果是Rails 3.0.*需要手动添加jQuery。
如果想用Prototype要把那些js做相应修改。

Let’s GO:

1、创建项目

rails new AjaxCRUD
# 创建scaffold
rails g scaffold Entry name:string address:text phone:string email:string
# 建立数据库和表
rake db:create
rake db:migrate

2、修改controller

因为创建的是基于Ajax的CRUD,所以controller返回的数据要支持‘.js’格式,可以把默认的json格式删掉,其实除了index会返回网页,其它都是返回js,所以index外的html格式返回也可以删除。

respond_to do |format|
format.html
format.js
end

基于个思想,修改后的controller如下:

class EntriesController < ApplicationController
  def index
    @entries = Entry.all
    @entry = Entry.new

    respond_to do |format|
      format.html # index.html.erb
      format.js
    end
  end

  def show
    @entry = Entry.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.js
    end
  end

  def new
    @entry = Entry.new

    respond_to do |format|
      format.html # new.html.erb
      format.js
    end
  end

  def edit
    @entry = Entry.find(params[:id])

    respond_to do |format|
      format.html
      format.js
    end
  end

  def create
    @entry = Entry.new(params[:entry])

    respond_to do |format|
      if @entry.save
        format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
        format.js
      else
        format.html { render action: "new" }
        format.js { render action: "new" }
      end
    end
  end

  def update
    @entry = Entry.find(params[:id])

    respond_to do |format|
      if @entry.update_attributes(params[:entry])
        format.html { redirect_to @entry, notice: 'Entry was successfully updated.' }
        format.js
      else
        format.html { render action: "edit" }
        format.js { render action: "edit" }
      end
    end
  end

  def destroy
    @entry = Entry.find(params[:id])
    @entry.destroy

    respond_to do |format|
      format.html { redirect_to entries_url }
      format.js
    end
  end
end

3、修改Views

修改index页面(app/views/entries/index.html.erb)来显示Entry表单。

Listing entries

Name Phone Email Address

Entry form

"form" %>

注意:为了后面用jQuery操作DOM,table设置id=”entries”。

修改partial(app/views/entries/_form.html.erb),注意在form_for中添加remote以发送异步请求。

<%= form_for(@entry, :remote => true) do |f| %>
  <% if @entry.errors.any? %>
    

<%= pluralize(@entry.errors.count, "error") %> prohibited this entry from being saved:

    <% @entry.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :phone %>
<%= f.text_field :phone %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :address %>
<%= f.text_area :address, :rows => 3 %>
<%= f.submit %>
<% end %>

新建Entry partial(app/views/entries/_entry.html.erb)。
为了发送AJAX请求,edit,destroy都设置remote=true。
为了区分不同的entry,调用dom_id来根据entry生成不同的id


  <%= entry.name %>
  <%= entry.phone %>
  <%= entry.email %>
  <%= simple_format entry.address %>
  <%= link_to 'Show', entry %>
  <%= link_to 'Edit', edit_entry_path(entry), :remote => true %>
  <%= link_to 'Destroy', entry, confirm: 'Are you sure?', method: :delete, :remote => true %>

4、添加js.erb来响应Ajax请求

下面是关键,首先先说下操作流程:浏览器发起Ajax请求(CRUD)–>controller收到请求,并调用model更新数据–>返回js代码–>浏览器收到js代码,使用jQuery来更新index页面中的DOM对象,包括列表和表单。

新建 app/views/entries/create.js.erb 响应添加Entry的请求
1、在index页面的entry列表中添加刚才新增的entry对象。
2、清空index页面中添加entry表单中的数据。

$('<%= escape_javascript(render(:partial => @entry)) %>').appendTo('#entries').hide().fadeIn();
$("#new_entry")[0].reset();

escape_javascript(render(:partial => @entry))可以缩写成:j render @entry。

新建 app/views/entries/edit.js.erb 处理点击编辑时的请求
在index页面的新建entry的form中填充要编辑entry的数据。

$("#form > form").replaceWith("<%= j render "form" %>");

新建 app/views/entries/update.js.erb 来更新列表中的Entry对象。新建Entry并清空form

$("#<%= dom_id @entry %>").replaceWith("<%= j render @entry %>");
<% @entry = Entry.new # reset for new form %>
$(".edit_entry").replaceWith("<%= j render "form" %>")
$(".new_entry")[0].reset();

新建 app/views/entries/destroy.js.erb 来删除list中的Entry

$("#<%= dom_id @entry %>").remove();

5、设置主页

设置entries页面为主页。修改 config/routes.rb

AjaxCRUD::Application.routes.draw do
  resources :entries
  root :to => "entries#index"
end

删除静态首页文件。

rm public/index.html

6、启动程序

rails s

此时打开 http://127.0.0.1:3000
就可以使用了。

源代码托管在
https://github.com/camelsong/AjaxCRUD

英文原文地址:http://codefundas.blogspot.com/2010/12/create-ajax-based-curd-using-rails-3.html

Posted in Uncategorized

LVM (简体中文)

 介绍

LVM 是一个应用于 Linux 内核的本地卷管理器 (Logical Volume Manager)。 使用 LVM 你可以抽象你的存储空间,并且可以有很容易更改的“虚拟分区”。LVM的基本模块如下:

  • Physical volume (PV): 物理卷,例如一个硬盘,或一个Software RAID设备; 硬盘的一个分区 (或者甚至硬盘本身或者回环文件),在它上面可以建立卷组。It has a special header and is divided into physical extents. Think of physical volumes as big building blocks which can be used to build your hard drive.
  • Volume group (VG): 卷组,将一组物理卷收集为一个管理单元;Group of physical volumes that are used as storage volume (as one disk). They contain logical volumes. Think of volume groups as hard drives.
  • Logical volume(LV): 逻辑卷,等同于传统分区,可看作便准的块设备,以容纳文件系统;A “virtual/logical partition” that resides in a volume group and is composed of physical extents. Think of logical volumes as normal partitions.
  • Physical extent (PE): 物理块,划分物理卷的数据块;A small part of a disk (usually 4MB) that can be assigned to a logical Volume. Think of physical extents as parts of disks that can be allocated to any partition.

使用 LVM 你可以比正常的硬盘分区更容易的管理硬盘分区(逻辑卷)。例如,你可以:

  • 使用卷组(VG),使众多硬盘空间看起来像一个大硬盘。
  • 使用逻辑卷(LV),可以创建跨越众多硬盘空间的分区。
  • 可以根据需要,对分区(LV)和硬盘空间(VG)进行创建、删除、调整大小等操作。(it doesn’t depend on position of the logical volumes within volume groups as with normal partitions)
  • Resize/create/delete partitions(LV) and disks(VG) online (filesystems on them still need to be resized, but some support online resizing)
  • Name your disks(VG) and partitions(LV) as you like
  • Create small partitions(LV) and resize them “dynamically” as they get more filled (growing must be still done by hand, but you can do it online with some filesystems)

示例:

两块物理硬盘

  硬盘1 (/dev/sda):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    |分区1 50GB (Physical volume) |分区2 80GB (Physical volume)     |
    |/dev/sda1                   |/dev/sda2                       |
    |_ _ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |

  硬盘2 (/dev/sdb):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    |分区1 120GB (Physical volume)                 |
    |/dev/sdb1                                    |
    | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _|
LVM方式

  卷组VG1 (/dev/MyStorage/ = /dev/sda1 + /dev/sda2 + /dev/sdb1):
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    |逻辑卷lv1 15GB         |逻辑卷lv2 35GB              |逻辑卷lv3 200GB               |
    |/dev/MyStorage/rootvol|/dev/MyStorage/usrvol     |/dev/MyStorage/homevol       |
    |_ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _|

总而言之: With LVM you can use all your storage space as one big disk (volume group) and have more flexibility over your partitions (logical volumes).

Advantages

Here are some things you can do with LVM that you can’t (or can’t do easily) with just mdadm, MBR partitions, GPT partitions, parted/gparted and a file-level tool like rsync.

  1. Online/live partition resizing
  2. No need for an extended partition (not relevant for GPT)
  3. Resize partitions regardless of their order on disk (no need to ensure surrounding available space)
  4. Online/live migration of partitions being used by services without having to restart services

These can be very helpful in a server situation, desktop less so, but you must decide if the features are worth the abstraction.

安装

在做其他工作之前,我们需要加载合适的模块:

# modprobe dm-mod

如果你已经安装好了操作系统,并只是想利用增加或尝试一个LVM分区,请跳到这 partition disks.

在 LVM 上安装 Arch Linux

在开始安装arch之前(即输入:/arch/setup之前),先使用cfdisk等工具来规划分区。因为grub不能从LVM逻辑卷引导启动 (版本1.0时),所以需要先创建一个/boot引导区,100MB应该够了。另外的解决办法就是使用lilo或者高于1.95版本的grub。

创建 LVM 分区

接下来,要创建LVM将使用的分区。文件类型使用’Linux LVM’,所以使用分区id 0x8e (文件系统类型:8E)。在需要使用LVM的每块硬盘上,各创建一个LVM分区。 Your logical volumes will reside inside these partitions so size them accordingly. If you will use only LVM and no other external partitions, use all of free space on each disk.

警告: /boot不能包含在LVM分区里,因为grub (version<1.95)不能从LVM中引导grub。
小贴士: 所有硬盘的所有LVM分区可以被设置成看起来就像一个大硬盘。

创建物理卷(PV)

接下来,要加载使用lvm所需的相应模块:

# modprobe dm-mod

用命令’fdisk -l’查看那个分区的文件系统类型是’Linux LVM’,然后在其上创建一个物理卷组pv(假设是/dev/sda2),输入如下命令:

# pvcreate /dev/sda2

Substitute /dev/sda2 with all your partitions to create physical volumes on all of them. This command creates a header on each partition so it can be used for LVM. 查看物理卷情况:

# pvdisplay

创建卷组(VG)

创建完成物理卷之后,就是开始创建卷组了。 如果有两个以上的物理卷pv(比如下面例子,有两个/dev/sda2和/dev/sdb1),首先必须先在其中一个创建一个卷组vg,然后让该卷组vg 扩大到其他所有的物理卷pv(这里假设你只使用一个卷组vg来管理其他所有的物理卷pv。):

# vgcreate VolGroup00 /dev/sda2
# vgextend VolGroup00 /dev/sdb1

其中,“VolGroup00”名字换成你自己起的名字即可。接下来看看卷组情况:

# vgdisplay
注意: 可以创建多于一个的卷组,但因此将让你的硬盘空间看起来不像一块硬盘。

创建逻辑卷(LV)

创建完卷组vg之后,就可以开始创建逻辑卷了。输入下面命令:

# lvcreate -L 10G VolGroup00 -n lvolhome

其中10G是大小,VolGroup00是卷组vg名称,lvolhome是逻辑卷lv名称,这些都可以根据你自己喜欢设定,以后可以使用/dev/mapper/Volgroup00-lvolhome 或者 /dev/VolGroup00/lvolhome来操作.

查看逻辑卷情况:

# lvdisplay
注意: You may need to load the device-mapper kernel module (modprobe dm-mod) for the above commands to succeed.
小贴士: 一开始可以创建小一点的逻辑卷lv,然后留一部分未使用空间在卷组vg里,以后可以根据需要再扩展各个逻辑卷。

建立文件系统与挂载逻辑卷

Your logical volumes should now be located in /dev/mapper/ and /dev/YourVolumeGroupName. If you can’t find them use the next commands to bring up the module for creating device nodes and to make volume groups availabile:

# modprobe dm-mod
# vgscan
# vgchange -ay

Now you can create filesystems on logical volumes and mount them as normal partitions (if you are installing Arch linux, skip this step):

# mkfs.ext3 /dev/mapper/VolGroup00-lvolhome
# mount /dev/mapper/VolGroup00-lvolhome /home

如果你正在安装Archlinux,到 Prepare Hard Drive 这一步时,转到第三项 Set Filesystem Mountpoints ,请 在进入安装前,阅读下面的 重要部分 !

重要

有几点在使用/安装带有 LVM 的 Arch Linux 时你需要特别注意的地方。(括号里是相关的安装过程中的菜单):

设置文件系统挂载点

  • 当选择挂载点时(除了/boot),千万不要选择实际存在的逻辑卷(比如:/dev/sda2),只需选择由lv创建的逻辑卷(比如: /dev/mapper/Volgroup00-lvolhome)。

配置系统

  • 确保在 /etc/rc.conf 中,把USELVM="no"修改成 USELVM="yes"rc.sysinit 脚本处理 USELVM 变量时只会识别yes 或者 YES,不支持大小写混合。
  • 确保 lvm2/etc/mkinitcpio.conf HOOKS 部分的 filesystems 前面,这样您的内核就可以在启动时找到 LVM 卷。
  • If your root filesystem ( “/” ) is put onto a logical volume, make sure regenerate kernel image based on above modified /etc/mkinitcpio.conf by using below command so that bootloader can find your root during booting
     cd /boot
     mkinitcpio -p linux
  • 确保为 root 使用了正确的卷。
对于 GRUB,编辑/boot/grub/menu.lst
     ...
     (0) Arch Linux
     title  Arch Linux
     root   (hd0,0)
     kernel /vmlinuz-linux root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro
     initrd /initramfs-linux.img
     ...
For SYSLINUX, edit /boot/syslinux/syslinux.cfg:
     ...
     # (0) Arch Linux
     LABEL arch
       MENU LABEL Arch Linux
       LINUX ../vmlinuz-linux
       APPEND root=/dev/mapper/VolGroup00-lvolroot ro
       INITRD ../initramfs-linux.img
对于 LILO ,检查 /etc/lilo.conf:
     ...
     image=/boot/vmlinuz-linux
       label=arch
       append="root=/dev/mapper/VolGroup00-lvolroot resume=/dev/mapper/VolGroup00-lvolswap ro"
       initrd=/boot/initramfs-linux.img
     ...

配置

扩大逻辑卷

To grow a logical volume you first need to grow the logical volume and then the filesystem to use the newly created free space. Let’s say we have a logical volume of 15GB with ext3 on it and we want to grow it to 20G. We need to do the following steps:

# lvextend -L 20G VolGroup00/lvolhome (or lvresize -L +5G VolGroup00/lvolhome)
# resize2fs /dev/VolGroup00/lvolhome

You may use lvresize insted of lvextend.

If you want to fill all the free space on a volume group use the next command:

# lvextend -l +100%FREE VolGroup00/lvolhome
警告: 并非所有的文件系统都支持无损或在线扩大逻辑卷。
注意: If you do not resize your filesystem, you will still have a volume with the same size as before (volume will be bigger but partly unused).

缩小逻辑卷

Because your filesystem is probably as big as the logical volume it resides on, you need to shrink the filesystem first and then shrink the logical volume. Depending on your filesystem, you may need to unmount it first. Let us say we have a logical volume of 15GB with ext3 on it and we want to shrink it to 10G. We need to do the following steps:

# resize2fs /dev/VolGroup00/lvolhome 9G
# lvreduce -L 10G VolGroup00/lvolhome (or lvresize -L -5G VolGroup00/lvolhome)
# resize2fs /dev/VolGroup00/lvolhome

Here we shrunk the filesystem more than needed so that when we shrunk the logical volume we did not accidentally cut off the end of the filesystem. After that we normally grow the filesystem to fill all free space left on logical volume. You may use lvresize instead of lvreduce.

Warning:

  • Do not reduce the filesystem size to less than the amount of space occupied by data or you risk data loss.
  • Not all filesystems support shrinking without loss of data and/or shrinking online.
Note: It is better to reduce the filesystem to a smaller size than the logical volume, so that after resizing the logical volume, we do not accidentally cut off some data from the end of the filesystem.

Remove logical volume

Warning: Before you remove a logical volume, make sure to move all data that you want to keep somewhere else, otherwise it will be lost!

First, find out the name of the logical volume you want to remove. You can get a list of all logical volumes installed on the system with:

# lvs

Next, look up the mountpoint for your chosen logical volume…:

$ df -h

… and unmount it:

# umount /your_mountpoint

Finally, remove the logical volume:

# lvremove /dev/yourVG/yourLV

Confirm by typing “y” and you are done.

Dont forget, to update /etc/fstab:

# sudo nano /etc/fstab

You can verify the removal of your logical volume by typing “lvs” as root again (see first step of this section).

添加分区到卷组中

To add a partition to your volume group you must first make its type ‘Linux LVM’ (for example with cfdisk). Then you need to create a physical volume on it and extend the volume group over it:

# pvcreate /dev/sdb1
# vgextend VolGroup00 /dev/sdb1

Now you have free space in your volume group that can be used by logical volumes in this group.

Tip: You can add partitions from any disks to volume groups.

从卷组中移除卷

All of the data on that partition needs to be moved to another partition. Fortunately, LVM makes this easy:

# pvmove /dev/sdb1

If you want to have the data on a specific physical volume, specify that as the second argument to pvmove:

# pvmove /dev/sdb1 /dev/sdf1

Then the physical volume needs to be removed from the volume group:

# vgreduce myVg /dev/sdb1

Or remove all empty physical volumes:

# vgreduce --all vg0

And lastly, if you want to use the partition for something else, and want to avoid LVM thinking that the partition is a physical volume:

# pvremove /dev/sdb1

快照功能

介绍

LVM可以给系统创建一个快照,由于使用了COW (copy-on-write) 策略,相比传统的备份更有效率。 The initial snapshot you take simply contains hard-links to the inodes of your actual data. So long as your data remains unchanged, the snapshot merely contains its inode pointers and not the data itself. Whenever you modify a file or directory that the snapshot points to, LVM automatically clones the data, the old copy referenced by the snapshot, and the new copy referenced by your active system. 这样的话,如果你只修改了不超过2G数据(包括原始的和快照的),你将可以只使用2G的空间,就能快照一个有35G的数据的系统。

配置

You create snapshot logical volumes just like normal ones.

# lvcreate --size 100M --snapshot --name snap01 /dev/mapper/vg0-pv

With that volume, you may modify less than 100M of data, before the snapshot volume fills up.

Todo: scripts to automate snapshots of root before updates, to rollback… updating menu.lst to boot snapshots (separate article?)

snapshots are primarily used to provide a frozen copy of a filesystem to make backups; a backup taking two hours provides a more consistent image of the filesystem than directly backing up the partition.

常见问题

LVM 命令不起作用

try preceeding commands with lvm like this:

# lvm pvdisplay

设定文件系统挂载点的页面不显示逻辑卷

If you are installing on a system where there is an existing volume group, you may find that even after doing “modprobe dm-mod” you don’t see the list of logical volumes.

In this case, you may also need to do:

# vgchange -ay <volgroup>

in order to activate the volume group and make the logical volumes available.

Receiving Input/Output Errors after plugging in a removable device with LVM partitions

Symptoms:

~$ sudo vgscan
 Reading all physical volumes.  This may take a while...
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 319836585984: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 319836643328: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 0: Input/output error
 /dev/backupdrive1/backup: read failed after 0 of 4096 at 4096: Input/output error
 Found volume group "backupdrive1" using metadata type lvm2
 Found volume group "networkdrive" using metadata type lvm2

Cause:

Removing an external LVM drive without deactivating the volume group(s) first. Before you disconnect, make sure to:
# vgchange -an <volume group name>

Fix: (assuming you already tried to activate the volume group with vgchange -ay <vg>, and are receiving the Input/output errors

# vgchange -an <volume group name>
Unplug the external drive and wait a few minutes
# vgscan
# vgchange -ay <volume group name>

技巧

更多资源

archwiki的其他关于LVM的文章:

外部资源:

Posted in Uncategorized