Hướng dẫn dùng syslog facility trong PHP

Physical – Copper Wires, physical medium, cables, Ethernet, Optical fibres, FDDI, ATM, IPX/SPX, Token ring

  • MTU and fragmentation

  • 6-byte MAC addresses and “http://www.iana.org/assignments/ethernet-numbers for the first 3 bytes.

  • 4-byte quad-dot, IP addrs, virtual Loopback addr,Ports, /etc/services, sockets, Privileged and Well-known ports

  • Address types –

  • Unicast – addrs that refer to a single host [NIC actually]

  • Multicast – addrs that identify a group of hosts [224 to 239] Good for videoconferencing

  • Broadcast – addrs that include all the hosts on the local network

  • Routing – Routing Tables, ICMP redirects

  • Static routing

  • /etc/sysconfig/static-routes : eth0 net 192.168.0.20 netmask 255.255.255.0 gw 192.168.0.10

  • netstat -r, netstat –rn

  • The destination is a NW address

  • The gateway must be a host address of a m/c which specifies that pkts must be sent to it to reach the destination address.

  • Gateway is the default route

  • Configuring static routes

  • route add –net 192.168.0.10 netmask 255.255.255.0 gw 192.168.0.10 eth1

  • route add default gw 192.168.0.10 eth0

  • /etc/sysconfig/network : GATEWAY=x.x.x.x and the default route

  • Local DNS and name resolution : and /etc/host.conf, /etc/nsswitch.conf

  • Dynamic routing – routed, gated

  • ICMP redirects

  • ARP – The Addr resolution protocol

  • mii-tool – Configuring autonegotiation and other media-specific options


  • Network Interfaces Config tools :

  • netconfig

  • netconf

  • netcfg

  • redhat-config-network

  • neat

  • neat-tui

  • neat-control

  • redhat-config-network-cmd

  • redhat-config-network-druid

Rsync (Remote Sync) là một công cụ dùng để sao chép và đồng bộ file/thư mục được dùng rất phổ biến. Với sự trợ giúp của rsync, bạn có thể đồng bộ dữ liệu trên local hoặc giữa các server với nhau một cách dễ dàng.

Hướng dẫn dùng syslog facility trong PHP

Nội dung bài viết

I. Tính năng nổi bật của Rsync

II. Cài đặt Rsync

III. Sử dụng Rsync

1. Copy file và thư mục trên local

2. Copy file và thư mục giữa các server

3. Rsync qua SSH

4. Hiển thị tiến trình trong khi transfer dữ liệu với rsync

5. Sử dụng tùy chọn –include và –exclude

6. Sử dụng tùy chọn –delete

7. Giới hạn dung lượng tối đa của file được đồng bộ

8. Tự động xóa dữ liệu nguồn sau khi đồng bộ thành công

9. Chạy thử nghiệm Rsync

10. Giới hạn bandwidth

IV. Tổng kết

I. Tính năng nổi bật của Rsync

  • Rsync hỗ trợ copy giữ nguyên thông số của files/folder như Symbolic links, Permissions, TimeStamp, Owner và Group.
  • Rsync nhanh hơn scp vì Rsync sử dụng giao thức remote-update, chỉ transfer những dữ liệu thay đổi mà thôi.
  • Rsync tiết kiệm băng thông do sử dụng phương pháp nén và giải nén khi transfer.
  • Rsync không yêu cầu quyền super-user.

II. Cài đặt Rsync

Rsync được cài đặt dễ dàng với một dòng lệnh:

– Trên Red Hat/CentOS

yum install rsync

– Trên Debian/Ubuntu

apt-get install rsysnc

III. Sử dụng Rsync

Câu lệnh căn bản của rsync:

rsync options source destination

Trong đó:

  • Source: dữ liệu nguồn
  • Destination: dữ liệu đích
  • Options: một số tùy chọn thêm

Các tham số cần biết khi dùng Rsync

  • [root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

    sending incremental file list

    rpmpkgs/

    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/nagios-3.5.0.tar.gz

    rpmpkgs/nagios-plugins-1.4.16.tar.gz

    sent 4.99M bytes received 92 bytes 3.33M bytes/sec

    total size is 4.99M speedup is 1.00
    4: hiển thị trạng thái kết quả
  • [root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

    sending incremental file list

    rpmpkgs/

    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/nagios-3.5.0.tar.gz

    rpmpkgs/nagios-plugins-1.4.16.tar.gz

    sent 4.99M bytes received 92 bytes 3.33M bytes/sec

    total size is 4.99M speedup is 1.00
    5: copy dữ liệu recursively, nhưng không đảm bảo thông số của file và thư mục
  • [root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

    sending incremental file list

    rpmpkgs/

    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/nagios-3.5.0.tar.gz

    rpmpkgs/nagios-plugins-1.4.16.tar.gz

    sent 4.99M bytes received 92 bytes 3.33M bytes/sec

    total size is 4.99M speedup is 1.00
    6: cho phép copy dữ liệu recursively, đồng thời giữ nguyên được tất cả các thông số của thư mục và file
  • [root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

    sending incremental file list

    rpmpkgs/

    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/nagios-3.5.0.tar.gz

    rpmpkgs/nagios-plugins-1.4.16.tar.gz

    sent 4.99M bytes received 92 bytes 3.33M bytes/sec

    total size is 4.99M speedup is 1.00
    7: nén dữ liệu khi transfer, tiết kiệm băng thông tuy nhiên tốn thêm một chút thời gian
  • [root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

    sending incremental file list

    rpmpkgs/

    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/nagios-3.5.0.tar.gz

    rpmpkgs/nagios-plugins-1.4.16.tar.gz

    sent 4.99M bytes received 92 bytes 3.33M bytes/sec

    total size is 4.99M speedup is 1.00
    8: human-readable, output kết quả dễ đọc
  • [root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

    sending incremental file list

    rpmpkgs/

    rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

    rpmpkgs/nagios-3.5.0.tar.gz

    rpmpkgs/nagios-plugins-1.4.16.tar.gz

    sent 4.99M bytes received 92 bytes 3.33M bytes/sec

    total size is 4.99M speedup is 1.00
    9: xóa dữ liệu ở destination nếu source không tồn tại dữ liệu đó.
  • [root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

    [email protected]'s password:

    sending incremental file list

    ./

    httpd-2.2.3-82.el5.centos.i386.rpm

    mod_ssl-2.2.3-82.el5.centos.i386.rpm

    nagios-3.5.0.tar.gz

    nagios-plugins-1.4.16.tar.gz

    sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

    total size is 4991313 speedup is 1.00
    0: loại trừ ra những dữ liệu không muốn truyền đi, nếu bạn cần loại ra nhiều file hoặc folder ở nhiều đường dẫn khác nhau thì mỗi cái bạn phải thêm 
    [root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

    [email protected]'s password:

    sending incremental file list

    ./

    httpd-2.2.3-82.el5.centos.i386.rpm

    mod_ssl-2.2.3-82.el5.centos.i386.rpm

    nagios-3.5.0.tar.gz

    nagios-plugins-1.4.16.tar.gz

    sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

    total size is 4991313 speedup is 1.00
    0tương ứng.

Rsync không tự động chạy nên thường được dùng kết hợp với crontab. Tiếp theo mình sẽ giới thiệu một số ví dụ thường dùng với Rsync và kết thúc bài là script mình dùng để tự động backup toàn bộ VPS hàng ngày.

Khi lần đầu chạy rsync, toàn bộ dữ liệu nguồn sẽ được copy đến server đích, từ lần chạy sau trở đi chỉ những dữ liệu chưa được copy mới được transfer – đây là quá trình đồng bộ dữ liệu. Do đó, bạn có thể hiểu rsync thực hiện việc copy hoặc đồng bộ đều đúng. Trong bài viết mình sẽ sử dụng duy nhất khái niệm Copy cho ngắn gọn nhé.

1. Copy file và thư mục trên local

Copy file trên local

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10

Ví dụ trên copy file backup.tar sang thư mục /tmp/backups/ trên cùng một máy. Như bạn thấy thư mục đích chưa có nên rsync tự động tạo trước khi copy.

Copy thư mục trên local

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00

Câu lệnh trên copy toàn bộ file từ thư mục 

[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
2 đến thư mục 
[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
3 trên cùng một máy.

2. Copy file và thư mục giữa các server

Copy thư mục từ Local lên Remote Server

[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00

Lệnh trên copy thư mục 

[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
4 từ Local lên Remote Server có IP 
[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
5, lưu ở thư mục
[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
6

Copy thư mục từ Remote Server về Local

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00

Lệnh trên sẽ copy dữ liệu ở thư mục 

[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
7 trên Remote Server 
[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
8về máy Local lưu ở thư mục 
[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
9

3. Rsync qua SSH

Với Rsync, bạn có thể transfer qua giao thức SSH, qua đó dữ liệu được bảo mật an toàn hơn.

Copy file từ Remote Server về Local Server qua SSH

Để xác định giao thức sẽ sử dụng với rsync, bạn cần thêm tùy chọn 

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
0 cùng với tên giao thức, ở đây là 
[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
1.

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77

Lệnh trên copy file 

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
2 trên Remote Server 
[root@hocvps]# rsync -avz rpmpkgs/ [email protected]:/home/

[email protected]'s password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes received 91 bytes 399476.80 bytes/sec

total size is 4991313 speedup is 1.00
8 về thư mục 
[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
4 trên máy Local.

Copy file từ Local lên Remote Server qua SSH

[root@hocvps]# rsync -avzhe ssh backup.tar [email protected]:/backups/

[email protected]'s password:

sending incremental file list

backup.tar

sent 14.71M bytes received 31 bytes 1.28M bytes/sec

total size is 16.18M speedup is 1.10

4. Hiển thị tiến trình trong khi transfer dữ liệu với rsync

Để hiển thị tiến độ transfer dữ liệu, bạn có thể sử dụng tùy chọn 

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
5

[root@hocvps]# rsync -avzhe ssh --progress /home/rpmpkgs [email protected]:/root/rpmpkgs

[email protected]'s password:

sending incremental file list

created directory /root/rpmpkgs

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

1.02M 100% 2.72MB/s 0:00:00 (xfer#1, to-check=3/5)

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

99.04K 100% 241.19kB/s 0:00:00 (xfer#2, to-check=2/5)

rpmpkgs/nagios-3.5.0.tar.gz

1.79M 100% 1.56MB/s 0:00:01 (xfer#3, to-check=1/5)

rpmpkgs/nagios-plugins-1.4.16.tar.gz

2.09M 100% 1.47MB/s 0:00:01 (xfer#4, to-check=0/5)

sent 4.99M bytes received 92 bytes 475.56K bytes/sec

total size is 4.99M speedup is 1.00

5. Sử dụng tùy chọn –include và –exclude

Hai tùy chọn này cho phép chúng ta thêm hoặc bớt file hoặc thư mục trong quá trình đồng bộ dữ liệu.

apt-get install rsysnc
0

Ở ví dụ trên, Rsync include toàn bộ những file hoặc thư mục có tên bắt đầu bởi ký tự ‘R’ và exclude toàn bộ những file hoặc thư mục còn lại.

6. Sử dụng tùy chọn –delete

Nếu muốn xóa một file hoặc thư mục không có ở thư mục nguồn, mà lại xuất hiện ở thư mục đích trong quá trình transfer, bạn hãy sử dụng tùy chọn 

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00
9.

apt-get install rsysnc
1

Server đích đã có file 

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
7, trong quá trình đồng bộ với option 
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00
9, file sẽ bị xóa.

7. Giới hạn dung lượng tối đa của file được đồng bộ

Để giới hạn những file lớn được đồng bộ, bạn có thể sử dụng option 

[root@hocvps]# rsync -avzh [email protected]:/home/tarunika/rpmpkgs /tmp/myrpms

[email protected]'s password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes received 4.99M bytes 322.16K bytes/sec

total size is 4.99M speedup is 1.00
9

apt-get install rsysnc
2

8. Tự động xóa dữ liệu nguồn sau khi đồng bộ thành công

Để rsync tự động xóa dữ liệu sau khi đồng bộ lên server đích thành công, bạn có thể sử dụng lựa chọn 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
0

apt-get install rsysnc
3

9. Chạy thử nghiệm Rsync

Nếu bạn không chắc câu lệnh có thực hiện chính xác những gì mong muốn hay không, hãy thêm tùy chọn 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
1.

Rsync lúc này sẽ không thay đổi gì dữ liệu cả mà chỉ show output mà thôi. Nếu mọi thứ hoạt động ổn, hãy bỏ tùy chọn 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
1 ra khỏi câu lệnh.

apt-get install rsysnc
4

10. Giới hạn bandwidth

apt-get install rsysnc
5

IV. Tổng kết

Ứng dụng của Rsync có rất nhiều, bạn có thể đồng bộ hóa file giữa các thư mục, giữa các server qua đó backup server sang một server khác hoặc synchronize real time. Tùy nhu cầu mà bạn hãy ứng dụng Rsync cho hiệu quả.

Chúc bạn thành công.

nguồn : http://hocvps.com/rsync/

Những biểu hiện thường gặp server hosing đang chứa mã độc là thực hiện gửi spam mail, brute force ssh, DOS… tới server khác. Thông thường bạn sẽ nhận được cảnh báo này từ nhà cung câp dịch vụ server, vps… Sau đây là một số thủ thuật giúp bạn nhanh chóng phát hiện tài khoản nào đang bị hacker kiểm soát.

Hướng dẫn dùng syslog facility trong PHP

1 Kiểm tra bằng lệnh top

Trên server hosting sẽ có nhiều tài khoản chạy đồng thời. Tài khoản bị tấn công thường xuất hiện liên tục ở những hàng đầu khi gõ lệnh top. Ở cột command sẽ xuất hiện những lệnh thường gặp như perl, python, host, mail…
Hình bên trên cho biết tài khoản noithat đang chạy các tiến trình perl

2 Kiêm tra file log apache
Các tài khoản hosting chữa mã độc thường được hacker điều khiển bằng cách gửi POST request tới website.

Hướng dẫn dùng syslog facility trong PHP

Một tài khoản đang bị tấn công thông qua đường dẫn http://domain.com/wp-content/themes/saladmag/css/general19.php


3 Tìm kiếm các file nghi vấn khác trên tài khoản user. 
Tìm kiếm các file mới được up lên.

# cd /home/noithat/public_html
# find . -name “*.php” -mtime -14



Tìm kiếm các script có khả năng chữa mã độc.

# find . -type f -name ‘*.php’ | xargs egrep -i “(mail|fsockopen|pfsockopen|stream_socket_client|exec|system|passthru|base64_decode) *\(”



4 Tìm kiếm mã độc thông qua những email đang gửi spam
Các tài khoản bị tấn công đa phần sẽ thực hiện gửi spam mail. Do đó việc kiểm tra script đang gửi spam mail sẽ tìm ra được vị trí mã nguồn bị tấn công.

nguồn: http://hotrovps.info/linux/tim-kiem-ma-doc-tren-server-hosting-linux.html

Today’s Blog is all about networking stuffs and I will be covering following points:

  1. Know your Network Controller Device with lspci
  2. Display all interfaces which are currently available, even if down with ifconfig
  3. Enable the service networking 
  4. Enable Apache server
  5. Then Enable Network Manager with network-manager
  6. Then  Enable your WiFi with nmcli nm

AND REMEMBER ALL THESE THINGS WILL BE DONE WITH OUR TERMINAL THIS IS WHY LINUX IS SO COOL…


Then we are all set to run the internet.And now we are some basic troubleshooter about networking, though networking is a huge this would be the first step. Not actually first first step in networking is always theory The Basics of Networking, but we are not dealing with it today. So lets begin


1. In order to know  the hardware devices assembled in your laptop open your TERMINALCTRL+ALT+T and then type lspci .

This displays all the hardware devices  like PCI BRIDGE, SATA controller, SMBus, USB Controller, Audio Device assembled in your laptop / computer. 


Hướng dẫn dùng syslog facility trong PHP



My Network controller is:

02:00.0 Network controller: Broadcom Corporation BCM4313 802.11bgn Wireless Network Adapter (rev 01)


2. So now we need to know the interfaces that are available in our UBUNTU.  To see this typeifconfig in the terminal and you will see the output like this :

Hướng dẫn dùng syslog facility trong PHP


3. To access the internet first your hardware device should be activated, In order to do so type 

sudo service networking  and press tab twice which displays  arguments like:

 force-reload       reload        restart       start         stop

Perform any you need.


4.  To enable apache server in terminal type:


      sudo service apache2 start


5.  Now enable the network manager. In order to do so in terminal type:

    

     sudo service network-manager start 


6. Now First view the available connecting points Wireless point or Ethernet. To view the available wifi points in terminal type

    

     sudo nmcli con list


Hướng dẫn dùng syslog facility trong PHP


This displays the available devices With its UID and SSID.

Now to enable the wifi in terminal type:


      Sudo nmcli con up SSID or the UID

You can manipulate these basic networking commands and  triubleshoot on your own.


source: http://www.techjhola.com/2015/05/troubleshoot-basic-networking-wifi.html

Khái niệm facility level và secrity level:

Facility level: đại diện cho đối tượng tạo ra thông báo (kernel, process).

acility NumberKeywordFacility Description0kemkernel messages1useruser-level messages2mailmail system3daemonsystem daemons4authsecurity/authorization messages5syslogmessages generated internally by syslog6lprline printer subsystem7newsnetwork news subsystem8uucpUUCP subsystem9–clock daemon10authprivsecurity/authorization messages11ftpFTP daemon12–network news subsystem13–log audit14–log alert15cronclock daemon16local0local use 0 (local0)17local1local use 1 (local1)18local2local use 2 (local2)19local3local use 3 (local3)20local4local use 4 (local4)21local5local use 5 (local5)22local6local use 6 (local6)23local7local use 7 (local7)


Việc sử dụng facility cho phép chúng ta kiểm soát log được dễ dàng hơn, dựa vào nguồn gốc của nó.

Severity levels: xác định một ngưỡng mà messages sẽ được logged

CodeSeverityKeywordDescription0Emergencyemerg (panic)System is unstable1AlertalertAction must be taken immediately2CriticalcritCritical conditions3Errorerr (error)Error conditions4Warningwarning (warn)Warning conditions5NoticenoticeNormal but significant condition6InformationalinfoInformational messages7DebugdebugDebug-level messages


Ngoài ra còn một mức đặc biệt được gọi là none, mức này sẽ disable facility đi cùng . Severity levels định nghĩa một số lượng các bản ghi chi tiết trong log file. Dấu sao[*] có thể được sử dụng để miêu tả cho tất cả cácfacilities level hoặc tất cả Severity levels

Cài đặt syslog-ng:

Yêu cầu: 
· Hệ điều hành CentOS 6.x trở lên
· Kết nối internet

Cài đặt EPEL repo:

Code:

apt-get install rsysnc
6

kiểm tra lại EPEL repo đã cài đặt thành công hay không

Code:

apt-get install rsysnc
7

cài đặt syslog-ng

Code:

apt-get install rsysnc
8

Nếu hệ thống yêu cầu import GPG key, gõ y để đồng ý

Code:

apt-get install rsysnc
9

Hệ thống đã cài đặt thành công syslog-ng. Nhưng chưa thể chạy được vì mặc định hệ thống đang dùng rsylog.

Cấu hình hệ thống dùng syslog-ng:
tắt rsyslog

Code:

rsync options source destination
0

Kiểm tra lại kết quả

Code:

rsync options source destination
1

bật syslog-ng

Code:

rsync options source destination
2

kiểm tra lại kết quả

Code:

rsync options source destination
3

Stop Rsyslog

Code:

rsync options source destination
4

Start Syslog-ng

Code:

rsync options source destination
5

Cấu hình syslog-ng nhận log từ các thiết bị trong mạng:
Thêm dòng cấu hình sau vào phần cuối của file cấu hình syslog-ng /etc/syslog-ng/syslog-ng.conf:

Code:

rsync options source destination
6

với cấu hình trên hệ thống sẽ nhận log từ tất cả các thiết bị trong mạng với địa chỉ ip nguồn là bất kỳ, trên port 514, giao thức tcp và upd. Và sẽ đặt file log của thiết bị trong thư mục /var/log/syslog. Tên của file log sẽ được đặt dựa vào tên của máy chủ và thời gian hiện hành của hệ thống.

khởi động lại dịch vụ syslog-ng để thực hiện các thay đổi.

Code:

rsync options source destination
7

Trong bài viết sau sẽ hướng dẫn các bạn cấu hình forward logs từ Windows & Linux đến syslog-ng server.

Trên hệ điều hành Linux

Sử dụng rsyslog.

Mở file rsyslog.conf và thêm dòng cấu hình sau vào cuối file cấu hình:

Code:

rsync options source destination
8

Khởi động lại dịch vụ để các thay đổi được thực hiện

Code:

rsync options source destination
9

Sử dụng syslog-ng.

Mở file cấu hình syslog-ng.conf, xóa toàn bộ nội dung bên trong file này và thêm vào phần cấu hình bên dưới.

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
0

Lưu ý: Trong file cấu hình trên server sẽ gởi log đến remote server bằng giao thức tcp. Nếu muốn dùng giao thức udp thay cho giao thức tcp chúng ta cần sửa phần cấu hình destination syslog-ng_server thành

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
1

Khởi động lại dịch vụ để các thay đổi được thực hiện

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
2

Kiểm tra ghi log

Chạy lệnh bên dưới để kiểm tra việc ghi log đến server syslog-ng trung tâm.

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
3

Trên server log trung tâm chạy lênh sau để kiểm tra quá trình ghi log có thành công hay không. Nếu không thì kiểm tra lại kết nối mạng và cấu hình tường lửa.

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
4

Trên hệ điều hành windows.

Download ứng dụng Evtsys theo link sau.
http://shink.in/7yRdX
Sau khi download file, chúng ta tiến hành giải nén.
Mở command line và di chuyển vào thư mục file được giải nén, chạy lệnh sau.

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
5

Tham khảo thêm phần cấu hình Syslog-ng theo link sauhttp://svuit.com/showthread.php?718-…3%AAn-CentOS-6

Cấu hình xoay vòng log file


Phần lớn các bản phân phối sẽ cài đặt một cấu hình syslog mặc định cho hệ thống, bao gồm logging to messages và các log file khác trong thư mục /var/log. Để ngăn cản nhưng file này ngày càng lớn trở nên cồng kềnh và có thể làm đầy ổ cứng gây ảnh hưởng đến các dịch vụ khác đang chạy. một hệ thống xoay vòng log (log rotation) nên được cài đặt. Hệ thống cron đưa ra các lệnh để thiết lập những log files mới, những log file cũ được đổi tên bằng cách thay một con số ở hậu tố. ví dụ khi có sự xoay vòng log xảy ra file /var/log/messages sẽ được chuyển thành /var/log/messages.1 và hệ thống sẽ tạo mới một file /var/log/mesages. Sự xoay vòng này có thể được cấu hình cho một số lượng lớn các file. Tùy theo cấu hình mà các file này sẽ được lưu lại bao nhiêu bản cũ nhất trên server trước khi bị xóa đi.
Tiện ích thi hành rotation là logrotate. Lệnh này được cấu hình sử dụng cho một hoặc nhiều files – được xác định bởi các tham số đi cùng. File cấu hình mặc định là /etc/logrotate.conf
File cấu hình logrotate sẽ tương tự như bên dưới

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
6

Có hai kiểu xoay vòng log thường được sử dụng:

  • Xoay vòng log theo dung lượng file
  • Xoay vòng log theo thời gian



Mỗi kiểu đều có ưu nhược điểm riêng. Tùy thuộc vào đặc điểm của từng hệ thống mà ta chọn kiểu xoay vòng log thích hợp
Để cấu hình xoay vòng log cho các dịch vụ ta sẽ đặt các file cấu hình xoay vòng log trong thư mục /etc/logrotate.d

Ví dụ: Cấu hình xoay vòng log cho log của web server apache ta làm như sau.
Tạo file cấu hình cấu hình xoay vòng log
Vim /etc/logrotate.d/httpd

Thêm phần cấu hình bên dưới vào file cấu hình.

Code:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
7

Size 70M: hệ thống sẽ xoay vòng log khi dung lượng file log lớn hơn 70M. Tại đây chúng ta cũng có thể cấu hình xoay vòng log dựa trên thời gian bằng cách dùng các chỉ thị sau (daily, weekly, monthly). Để xoay vòng log theo ngày, tuần và tháng.
Compress: nén các file log cũ theo chuẩn gzip. Mục đích giảm không gian đĩa cứng
Dateext: ngày sẽ được thêm vào trong tên file mới.
Rotate 7: hệ thống chỉ lưu giữ 7 file log trên server. Các file cũ hơn sẽ được xóa bỏ khỏi hệ thống
Misingok: hệ thống sẽ không tạo ra lỗi. Nếu tập tin không được tìm thấy.
Sharedscripts: chạy các script bất kỳ trước (prerotate) hoặc sau khi (postrotate) xoay vòng cho mỗi file log.

Tham khảo thêm tại: http://linuxcommand.org/man_pages/logrotate8.html

Mình thi thoảng phải quản lý linux server, mà trong đó có một số thao tác quản lý quan trong như quản lý đĩa cứng, quản lý mạng, quản lý đường truyền mà nếu phải tự tìm thông tin mà không có tool sẽ rất bất tiện. Ở dưới đây mình sẽ giới thiệu một sô tool quản lý mà cá nhân mình thấy “siêu tiện” với những bạn nào hay phải mày mò trên server linux:

iotop : quản lý disk IO

Cài đặt:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
8

Sau khi cài đặt xong thì bạn chỉ cần khởi động bằng lệnh 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
3 sẽ thấy một màn hình như sau:

Hướng dẫn dùng syslog facility trong PHP

Từ màn hình trên thì chúng ta có thể thấy câu lệnh 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
4 đang “ngốn” disk IO (read) với tốc độ đọc 9.4M 1s, và từ đó đưa ra được chính sách thích hợp 
Hướng dẫn dùng syslog facility trong PHP

iftop: quản lý đường truyền:

Cài đặt:

[root@hocvps]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes received 31 bytes 3.27M bytes/sec

total size is 16.18M speedup is 1.10
9

Cũng như trên bạn chỉ cần khởi động bằng : 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
5, sẽ cho chúng ta màn hình như dưới đây:

Hướng dẫn dùng syslog facility trong PHP


Bạn có thể thấy rất trực quan kết nối nào, port nào đang ngốn bao nhiêu đường truyền của bạn.
Bạn có thể dùng option 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
6 để chỉ định ethernet device:

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00
0

htop: quản lý tổng quan

Chắc tool này đã quá quen thuộc với mọi người rồi nên mình không cần phải nói thêm nhiều. Mình hay sử dụng htop để xem các thông số chính như load average và số lượng core được sử dụng.

Hướng dẫn dùng syslog facility trong PHP

Cách cài đặt và khởi động cũng tương tự như trên, bạn có thể sử dụng yum:

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00
1

ohmu : tool để visualize dung lượng đĩa cứng

Đây là một tiện ích rất cool để bạn xem được phân phối đĩa cứng của bạn, xem phần mềm nào đang “ngốn” dung lượng nhất.
Bạn cài đặt thông qua python:

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00
2

Sau khi cài đặt xong chỉ cần khởi động 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
7. Kết quả là một màn hình rất cool và dễ hiểu về phân phối dung lượng đía cứng:

Hướng dẫn dùng syslog facility trong PHP

Hy vọng bài viết sẽ có ích cho các bạn

nguon: http://kipalog.com/

Thông thường Varnish sử dụng chính để làm cache front end, tuy nhiên bạn hoàn toàn có thể cấu hình sử dụng để chống DDoS.

Hướng dẫn dùng syslog facility trong PHP

Hướng dẫn này viết từ trường hợp thực tế, mình đã chống DDoS thành công cho một người bạn. Nếu bạn cũng đang đau đầu về việc bị DDoS, hãy tham khảo bài viết này biết đâu có thể áp dụng được.

Ngay khi kết nối SSH, mình kiểm tra file access.log thì thấy ngay đúng là server đang bị DDoS.

Hướng dẫn dùng syslog facility trong PHP

Một loạt request có cách thức giống nhau, cùng một user-agent, IP khác nhau. Tính sơ sơ cũng phải đến cả trăm request mỗi giây, hỏi sao server không bị overload.

Nếu sử dụng CSF để block từng IP thì không ổn, vì quá nhiều IP. May thay các request này đều có một điểm chung là user-agent trong header giống nhau, như vậy có thể block lại được toàn bộ những request này dễ dàng.

Do CSF không hỗ trợ block theo header, nên mình phải sử dụng Varnish để filter riêng những request này ra và trả về Error 403. Với một đoạn code ngắn bên dưới để trong block 

[root@hocvps]# rsync -avzhe ssh [email protected]:/root/install.log /tmp/

[email protected]'s password:

receiving incremental file list

install.log

sent 30 bytes received 8.12K bytes 1.48K bytes/sec

total size is 30.74K speedup is 3.77
8, mình đã khống chế hoàn toàn được vụ DDoS.

[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes received 92 bytes 3.33M bytes/sec

total size is 4.99M speedup is 1.00
3

Ý nghĩa của đoạn code trên là filter những request có user-agent chứa Hotbar, nếu có thì trả lại lỗi 403, ngược lại thì pass cho Nginx webserver xử lý.