전체 글 (356)

유닉스 환경에서 자동백업 쉘 스크립트를 만들다가 얻은 지식을 공유합니다.

 

백업파일을 자동으로 생성하도록 스케쥴링을 걸어놓은 상태에서 가장 최신 백업파일을 골라서 다른 원격서버로 ftp를 이용해서 전송하려고 했습니다. 이때 가장 최신 파일명을 알아야 FTP를 이용해서 전송을 하겠죠?

 

그래서 최신파일명만 얻어낼 수 있는 명령어를 한번 만들어보도록 하겠습니다.

 

우선 1차로 만들어본 것은 아래와 같습니다.

 

# ls -lrt *.gz|tail -1|cut -d ' ' -f 8

 

하지만 위 처럼 할 경우 만약 스페이스가 두개 이상이 있는 경우 파일명이 아닌 파일생성 시간 값을 얻어오게되는 엉뚱한 결과가 나올 때가 있습니다. 그래서...

 

# ls -lrt *.gz|tail -1|tr -s ' '|cut -d ' ' -f 8

 

이렇게 tr 명령어를 중간에 끼워넣었습니다.

 

이 명령어와 동일한 결과를 얻을 수 있는게 하나 더 있는데 그건 아래와 같습니다.

 

# ls -lrt *.gz|tail -1|awk '{print $8}`

 

이렇게하면 좀 더 짧고 간단하게 파일명을 얻어올 수 있습니다.

서버시간이 로컬시간과 10분가량 차이가나서 통계 수집시 확인할 때마다 헷갈려서 시간 변경을 어떻게 해야하는지 알아보다가 찾은 정보입니다.

Below information is from here

-----------------------------------------------------------------------------

Changing the timezone, date, and time

Setting your time zone

In /etc the file, localtime, is a link to or copy of a file containing information about your time zone. Zone information files are usually in /usr/share/zoneinfo but this depends on your distribution. So if your localtime file points to a zone info file that is not your time zone you can change it by browsing the directories in /usr/share/zoneinfo to find your country, then find your city or a city in the same time zone and link localtime to it.
    $ ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
Some applications may use the configuration file /etc/sysconfig/clock to determine the current time zone so it's a good idea to set the ZONE entry (e.g. "America/Los_Angeles").

Changing the date and time

Changing the date and time requires two steps. First, Linux's date and time must be changed and then the new time has to be written to the hardware clock.
The date command can be used for both viewing and changing the date and time.
To change the time use date followed by the month, day, hour, minute, and year all numeric and no spaces. So, to set the date and time to November 2nd, 2003 12:57
The hardware clock can be updated in UTC (coordinated universal time) or your local time. It is standard practice to update it in UTC.
To update it to your local time leave off the --utc or add --localtime and leave off the --utc.

Alternatively

The date and time can be changed directly to the hardware clock and then used to update the system clock.

Using NTP (Network Time Protocol)

NTP will connect to a server to get the atomic time. It can be downloaded from www.ntp.org/downloads.html To get started with NTP simply download it, install it, use the ntpdate command followed by a public time server, and update your hardware clock.
    $ ntpdate "server DNS name or IP address"
    4 Nov 22:31:28 ntpdate[26157]: step time server 209.81.9.7 offset 22317290.440932 sec
    $ hwclock --systohc
A public time server can be found at http://support.ntp.org/bin/view/Servers/WebHome

To keep your time accurate you can create a cron job that executes:
(the -w option is the same as --systohc)
    ntpdate "server name" && hwclock -w

To stay independent of a particiluar server you can use 0.pool.ntp.org (0, 1, or 2) for the server name. This domain uses DNS round robin to choose different time servers every so often. This keeps certain nameservers from having high loads. The only disadvantage is the increased potential of updating time from a nameserver who is in the pool but has an incorrect time settings.

These are volunteer public servers so be polite, do not constantly access the public servers, use only public servers (not private), and if you have multiple machines, set up a ntp server and have your other machines retrieve the time from your local server. Check http://www.eecis.udel.edu/~mills/ntp/servers.html for detailed rules.


출처 : http://www.hypexr.org/linux_date_time_help.php

특정 테이블의 제약사항( not null, fk 과 같은 )의 이름, 테이블명, 소유자의 목록을 보여줍니다.

Below query command queries constraints'(not null, fk) name , owner, table name, column name, and position of the column.


SQL > select * from user_cons_columns where table_name='table_name';

테이블 백업을 할때에 export / import 를 이용할 수도 있지만 단순하게 rename 명령어를 이용할 수도 있습니다. 또한 create ~ as select ~ 를 이용할 수도 있습니다.

하지만 오늘은 테이블 명을 변경하고 새로 테이블을 생성함으로써 백업을 할 수 있는 방법을 소개하도록 하겠습니다. 참고로 이 방법은 blob데이타가 많은 경우에 상당히 유용합니다.


Step by Step

1) Query table name and index names linked to the table you want to backup

2) Rename table name

3) Rename indexes' name ( if specified explicitly when it is created )

4) Rename constraints' name ( if specified explicitly when it is created )

4) Use the same DDL used to create the renamed table


※ This way is very simple and fast then using "create table ~ as select * from ~" statement when the table has a huge blob data.



1) 변경하고자하는 테이블에 어떤 인덱스들이 있는지 조회합니다.

SQL > select index_name, table_name from user_indexes;


2) 테이블명을 변경합니다.

SQL > rename old_table to new_table;


3) 인덱스명을 변경합니다. ( 인덱스명이 자동생성된 것이 아닌 경우에만 )

SQL > alter index pk_old_table rename to pk_new_table;


4) 제약사항명을 변경합니다. ( 제약사항명이 자동생성된 것이 아닌 경우에만 )

SQL > select * from user_cons_columns where table_name='{table_name};

SQL > alter table {table name} rename constraint {constraint name} to {new constraint name}


5) 기존 테이블을 생성했던 DDL을 사용하여 테이블을 새로 생성합니다.

SQL > create table .........



오늘은 테이블 명 변경하는 법에 대해서 알아보도록 하겠습니다.


테이블 명을 변경할 때는 아래처럼 rename 명령어를 사용합니다.

Use rename command to rename a table to a new one.


SQL > rename {old table name} to {new table name}

Table renamed.


변경이 완료되었는지 아래쿼리를 이용해서 확인해보세요.

Check if renaming was successful using below query. Below statement queries the table names owned by you.


SQL > select table_name from user_tables;


You would see renamed table name if it was successful.





💻 Programming/Oracle 11g

인덱스 목록 조회 ( Querying Index List )

일반유저로 내가 가지고 있는 인덱스 목록을 조회하려면 아래 쿼리를 사용합니다.

Querying index list owned by me.


SQL > select * from user_indexes;

Better to use below query to know which index is from which table because there are so many information on the user_indexes table.

위 쿼리를 실행하면 상당히 많은 양의 정보가 나오게 됩니다. 어느 테이블에 어느 인덱스가 있는지가 궁금하다면 아래 쿼리를 이용하시면 됩니다.

SQL > select index_name, table_name from user_indexes;