網頁

2013年1月31日

PostgreSQL Backup

Origin: Restore One Table with psql from a pg_dump PostgreSQL Backup

Backing up and restoring PostgreSQL is fairly easy using pg_dump andpsql. One more complicated scenario I have run into is doing a complete database backup with pg_dump and at some point down the road needing to just split out one table and restore it. This can be a pain because of how the pg_dump organizes the .sql file it outputs. I have found the best way to do this is to use pg_dump to backup one table to understand the format of what is needed for a restore. Then search the .sql file that was output from the full pg_dump output and split out the necessary data to restore into one table. Below I provide some examples as well as syntax to use for backing up and/or restoring PostgreSQL using pg_dump and psql.

First lets look at the syntax required to backup a PostgreSQL database using pg_dump which I have done below. The first command will backup the entire database and output to a SQL file in the default format.

        pg_dump -U postgres dbname > data-dump.sql

2013年1月30日

如何避免Linux zombie process的產生?

Origin: 如何避免Linux zombie process的產生?

所謂zombie process(殭屍行程)的成因,在於某行程已結束,但其父行程(parent process)並未取得該行程之結束狀態,則該行程就會變成zombie process,直到其父行程呼叫wait()取得該行程結束狀態。

用更精確的說法,來自Advanced Programming in the Unix Environment這本書:
The process that has terminated, but whose parent hasn’t yet waited for it.

Zombie process會造成kernel內部有多餘的一筆process資料儲存,佔了一筆資料量(process id, termination status, the amount of CPU time taken by the process等等資訊),但其卻已經結束了,若是系統load很高,造成的影響就更大了,譬如process數量到達上限,又發生有zombie process,可能就會產生無法執行程式的問題。因此我們必須避免zombie process的產生。

正常的multiprocess程式執行的流程,如下圖所示,parent process必須wait其子行程的結束。

2013年1月28日

使用getopt處理shell腳本的參數

getopt命令並不是bash的內建命令,它是由util-linux包提供的外部命令。相比較bash 的內置命令,getopt不只支持短参-s,還支持--longopt的長参數,甚至支持-longop

#!/bin/bash  

# A small example program for using the new getopt(1) program.
# This program will only work with bash(1)
# An similar program using the tcsh(1) script language can be found
# as parse.tcsh

# Example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
# Option a
# Option c, no argument
# Option c, argument `more'
# Option b, argument ` very long '
# Remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'

# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
-n 'example.bash' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument \`$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"\`$arg'" ; done

Passing arguments to a shell script

From: Passing arguments to a shell script

Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.

The following parameters are recognized:

$*

Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFSenvironment variable).

$@

Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.

$1, $2 ... $n

Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.

$0

Refers to the name of the script itself.

$#

Refers to the number of arguments specified on a command line.

2013年1月27日

如何用Windows Live Writer寫Blog

Windows Live Writer是一款免費的「寫作撰稿軟體」,它可以讓你「離線」編輯、管理部落格上的文章內容;Windows Live Writer相對的比同類工具更加優秀,它有好用的內容編輯介面和外掛附加功能,它可以直接連結你的圖床、影片庫,它能讓你同步管理、發佈多個部落格的文章,它也可以讓你從軟體端直接預覽文章發佈到部落格後的樣式

這一篇文章不是要教大家怎麼用,因為網路上已經有很多相關的文章教學。這一篇文章主要是關於設定的部分,告訴大家怎麼搭配各項工具與參數,讓我們在寫Blog的時候更輕易上手。

測試環境如下,同樣的方式應該也適用於其他的平台與環境

  • Windows Live Writer
  • Google Blogger
  • Precode plugin for windos live writer

2013年1月26日

Bson

BSON  is a computer data interchange format used mainly as a data storage and network transfer format in the MongoDB database. It is a binary form for representing simple data structures and associative arrays (called objects or documents in MongoDB). The name "BSON" is based on the term JSON and stands for "Binary JSON".

2013年1月25日

SVN Snippet

Apache Subversion (often abbreviated SVN, after the command name svn) is a software versioning and revision control system distributed under an open source license.

SVN Command

svn help

provides a summary of the available commands

svn checkout/co

This command is used to pull an SVN tree from the server. You should only need to do this once.

svn add

When you are creating a new file or directory, you need to tell the SVN server about it. This command does that. Note that the file won't appear in the repository until you do an svn commit