创建NFS服务器: 

# optimization by onekey

sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config

grep SELINUX=disabled /etc/selinux/config

setenforce 0

getenforce

/etc/init.d/iptables stop

/etc/init.d/iptables stop

chkconfig iptables off

chkconfig|egrep -v "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig",$1,"off"}'|bash

useradd oldboy

\cp /etc/sudoers /etc/sudoers.bak

echo "oldboy ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

tail -l /etc/sudoers

visudo -c

echo 'export TMOUT=1800' >> /etc/profile

echo 'export HISTSIZE=5'  >> /etc/profile

echo 'export HISTFILESIZE=5' >> /etc/profile

. /etc/profile

echo '*                -       nofile          65535' >> /etc/security/limits.conf

echo ">/etc/udev/rules.d/70-persistent-net.rules" >>/etc/rc.local

#about selinux

cat >> /etc/sysctl.conf <<EOF

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time = 600

net.ipv4.ip_local_port_range = 4000   65000

net.ipv4.tcp_max_syn_backlog = 16384

net.ipv4.tcp_max_tw_buckets = 36000

net.ipv4.route.gc_timeout = 100

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdv_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

net.nf_conntrack_max = 25000000

net.netfilter.nf_conntrack_max = 25000000

net.netfilter.nf_conntrack_tcp_timeout_established = 180

net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120

net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60

net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120

EOF


#set ip and hosts for nfs

cat >/etc/sysconfig/network-scripts/ifcfg-eth0<<EOF

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=10.0.0.31

NETSK=255.255.255.0

GATEWAY=10.0.0.2

nameserver=202.96.128.86

EOF

cat >/etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF

DEVICE=eth1

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

IPADDR=172.16.1.31

NETSK=255.255.255.0

EOF

cat >/etc/sysconfig/network<<EOF

NETWORKING=yes

HOSTNAME=nfs01

EOF

hostname nfs01

cat >/etc/hosts <<EOF

127.0.0.1    localhost localhost.localdomain localhost4 localhost4.localdomain4

::1          localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.1.5   lb01

172.16.1.6   lb02

172.16.1.7   web02

172.16.1.8   web01

172.16.1.31  nfs01

172.16.1.41  backup

172.16.1.51  db01

172.16.1.61  m01

EOF

#/etc/init.d/network reload


#make some dir

mkdir -p /server/scripts /server/tools /application /data


reboot


====================================================================================================


#yum repo and epel

yum -y install  wget

\cp /etc/yum.repos.d/CentOS-Base.repo{,.backup}

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

yum clean all

yum makecache

#setup some tools and develpment

yum -y install lrzsz nmap tree dos2unix nc wget

yum groupinstall -y "Base" "Compatibility libraries" "Debugging Tools" "Development tools"


#set ntp cron

echo '#time sync by NTP Server at 20170608' >>/var/spool/cron/root

echo '*/5 * * * * /usr/sbin/ntpdate 172.16.1.61 >/dev/null 2>&1' >>/var/spool/cron/root

crontab -l



#tar configs for every server rsync to backup

#install rsync

yum -y install rsync

#create passwordfile

echo "oldboy" >/etc/rsync.password

chmod 600 /etc/rsync.password

cat >/server/scripts/bak.sh <<EOF

#!/bin/sh

##create by 20has at 2017-5-18

IP=\$(ifconfig eth1|awk -F "[ :]+" 'NR==2{print \$4}')

TIME=\$(date +%F)

mkdir -p /backup/\$IP

cd / && \

tar -zcfh /backup/\$IP/\$TIME.tar.gz var/spool/cron/root etc/rc.local server/scripts etc/sysconfig/iptables

md5sum /backup/\$IP/\$TIME.tar.gz >/backup/\$IP/flag.log

rsync -az /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

#del

find /backup -type f -name "*.tar.gz" -mtime +7 |xargs rm -f

EOF

#add cron to rsync backup

echo '#add cron to rsync backup at 20170608' >>/var/spool/cron/root

echo '00 00 * * * /bin/sh /server/scripts/bak.sh &>/dev/null' >>/var/spool/cron/root


#setup nfs

#install rsync

yum -y install nfs-utils rpcbind

rpm -qa nfs-utils rpcbind

cat >/etc/exports<<EOF

#share /data by 20has for oldboy at 20170608

/data 172.16.1.0/24(rw,sync,all_squash,anonuid=888,anongid=888)

EOF

/etc/init.d/rpcbind start

chkconfig rpcbind on

chkconfig --list rpcbind

netstat -lntup |grep rpc

/etc/init.d/nfs start

chkconfig nfs on

chkconfig --list nfs

#rpcinfo -p localhost

useradd www -u888  -s /sbin/nologin -M

chown -R www.www /data

/etc/init.d/nfs reload

showmount -e 172.16.1.31



#inotify+rsync

#install inotify-tools

yum -y install inotify-tools

cd /server/scripts/

cat >inotify.sh<<EOF

#!/bin/sh

Path=/data

IP=172.16.1.41

/usr/bin/inotifywait -mrq --format '%w%f' -e close_write,delete \$Path \

|while read file

        do

        if [ -f \$file ];then

        rsync -az \$file  --delete rsync_backup@\$IP::nfsbackup \

        --password-file=/etc/rsync.password  &

        else

        cd \$Path

        rsync -az ./  --delete rsync_backup@\$IP::nfsbackup \

        --password-file=/etc/rsync.password  &

        fi

        done

EOF

#optimization inotify config    youhua

echo 655350 >/proc/sys/fs/inotify/max_user_watches

echo 655350 >/proc/sys/fs/inotify/max_queued_events

echo "echo 655350 >/proc/sys/fs/inotify/max_user_watches" >>/etc/rc.local

echo "echo 655350 >/proc/sys/fs/inotify/max_queued_events" >>/etc/rc.local

cat /etc/rc.local

#start inotify.sh

/bin/sh inotify.sh



每台服务器配置定时打包rsync到backup;

先搞好nfs搭建;

再实现rsync到backup,实现inotify实时备份到backup。

改进:使用keeplive方式 把ip漂移到nfsbackup的data目录下面 然后在backup上建立个nfs,保证nfs非单点,高可用!       此方法待实验(暂未完成)!

inotify.sh创建服务脚本,使用chkconfig管理   暂未成功!  (来自笔记:视频学习记录和规划day11)



客户端挂载nfs:

#install rsync

yum -y install nfs-utils rpcbind 

rpm -qa nfs-utils rpcbind

LANG=en

/etc/init.d/rpcbind start

chkconfig rpcbind on

chkconfig --list rpcbind

mount -t nfs 172.16.131:/data /mnt

# cat /proc/mounts

echo "mount -t nfs 172.16.1.31:/data /mnt" >>/etc/rc.local   #写入开机自动挂载nfs


客户端打包rsync+cron到backup:

#tar configs for every server rsync to backup

#install rsync

yum -y install rsync

#create passwordfile

echo "oldboy" >/etc/rsync.password

chmod 600 /etc/rsync.password

cat >/server/scripts/bak.sh <<EOF

#!/bin/sh

##create by 20has at 2017-5-18

IP=\$(ifconfig eth1|awk -F "[ :]+" 'NR==2{print \$4}')

TIME=\$(date +%F)

mkdir -p /backup/\$IP

cd / && \

tar -zcfh /backup/\$IP/\$TIME.tar.gz var/spool/cron/root etc/rc.local server/scripts etc/sysconfig/iptables

md5sum /backup/\$IP/\$TIME.tar.gz >/backup/\$IP/flag.log

rsync -az /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

#del

find /backup -type f -name "*.tar.gz" -mtime +7 |xargs rm -f

EOF

#add cron to rsync backup

echo '#add cron to rsync backup at 20170608' >>/var/spool/cron/root

echo '00 00 * * * /bin/sh /server/scripts/bak.sh &>/dev/null' >>/var/spool/cron/root