inotify+rsync

backup_to_rsync.sh
#!/bin/bash
#source function library
. /etc/init.d/functions
rsync_host=rsync.etiantian.org

#define variables
IP=$(ifconfig eth1|awk -F '[ :]+' 'NR==2 {print $4}')
Path="/backup/$IP"
TIME=`/bin/data +%F`
BackupFile=/server/scripts/backuplist

[ ! -d $Path ] && mkdir -p $Path
[ ! -f $BackupFile ] && [
echo "Please give me $BackupFile"
exit 1
]

function Msg(){
if [ $? -eq 0 ];then
action "$*" /bin/true
else
action "$*" /bin/false
fi
}

# Backup config files
tar zcfh $Path/conf_${TIME}.tar.gz `cat $BackupFile` &>/dev/null
Msg 'Backup config files'

#Make a flag for backup
find $Path -type f -name "*${TIME}.tar.gz"|xargs md5sum >$Path/flag_$TIME 2>/dev/null
Msg 'Make a flag for backup'

# Send backup to backup server
rsync -az $Path rsync_backup@${rsync_host}::backup --password-file=/etc/rsync.password &>/dev/null
Msg 'Send backup to backup server'

#Delete backup a week ago
find ${Path} -type f -name "*.tar.gz" -mtime +7 | xargs rm -f &>/dev/null
Msg 'Delete backup a week ago'


echo -e "#backup data\n00 00 * * * /bin/sh /server/scripts/backup_to_rsync.sh &>/dev/null" >> /var/spool/cron/root


/server/scripts/rsync.sh
#!/bin/bash
###################
# file name : rsync.sh
# description : install and config rsync server
###################
. /etc/init.d/functions

function Msg(){
if [ $? -eq 0 ];then
action "$*" /bin/ture
else
action "$*" /bin/false
fi
}

function rsyncd_conf(){
cat >/etc/rsyncd.conf<<EOF
sync server
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/24
auth users = rsync_backup
secrets file = /etc/rsync.password
##################
[backup]
comment = back server by
path = /backup
EOF
Msg "rsyncd_conf"
}

function rsync(){
useradd -u 666 rsync -M -s /sbin/nologin
[ ! -d /backup ] && mkdir -p /backup
[ ! -d /server ] && mkdir -p /server/tools
[ ! -d /server/scripts ] && mkdir -p /server/scripts
chown -R rsync.rsync /backup
echo "rsync_backup:123456" > /etc/rsync.password &&\
chmod 600 /etc/rsync.password
# 判断rsync是否启动
[ ! -f /server/scripts/rsyncd ] && {
echo "/server/scripts/rsyncd is not exist,error."
exit 2
}
\cp /server/scripts/rsyncd /etc/init.d/
chmod 755 /etc/init.d/rsyncd
netstat -ntulp | grep "873"
if [ $? -eq 0 ];then
echo "rsync is running" > /dev/null
else
/etc/init.d/rsyncd restart
fi
/sbin/chkconfig --add rsyncd
/sbin/chkconfig rsyncd on
Msg "rsync"
sleep 1
}

function postfix(){
if [ `grep "oldboyedu" /etc/mail.rc|wc -l` -eq 0 ];then
/bin/cp /etc/mail.rc /etc/mail.rc.$RANDOM
echo -e "set from=oldboyedu@163.com smtp=smtp.163.com\nset smtp-auth-user=oldboyedu smtp-auth-password=oldboy123 smtp-auth=login" >> /etc/mail.rc
/etc/init.d/postfix restart
else
echo "postfix is not need to config it."
fi
sleep 1
}

function crond_check(){
[ `crontab -l | grep "check.sh"| wc -l` -eq 0 ] &&{
echo -e "#check backup\n00 08 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1" >> /var/spool/cron/root
crontab -l
sleep 2
} || {
echo "check.sh cron is exist, no config"
}
}
function main(){
rsyncd_conf
rsync
postfix
crond_check
}
main


#!/bin/bash
# filename : rsyncd
# description :
#chkconfig: 2345 56 24
. /etc/init.d/functions
RETVAL=0
prog="rsync"
#check if rsync is already running
start(){
if [ ! -f /var/run/rsyncd.pid ];then
/usr/vin/rsync --daemon --config=/etc/rsyncd.conf
fi
RETVAL=$?
sleep 1
if [ $RETVAL -eq 0 ];then
action "starting $prog ..." /bin/true
else
action "starting $prog ..." /bin/false
fi
return $RETVAL
}

stop(){
killproc /usr/sbin/rsync
RETVAL=$?
sleep 1
if [ $RETVAL -eq 0 ];then
rm -f /var/run/rsyncd.pid
action "stopped $prog ..." /bin/true
else
action "stopped $prog ..." /bin/false
fi
}
restart(){
$0 stop
$0 start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo "Usage:$0 {start|stop|restart}"
exit 1
esac


#!/bin/bash
# filename : check_bak.sh
DIR=/backup
TIME=`/bin/date +%F`
log=/tmp/$TIME-check.log

[ -d $DIR ] && {
find $DIR -type f -name "flag_$TIME" |xargs md5sum -c >$log 2>/dev/null
mail -s "$(date +%F_%T) back check result" 12343qq.com <&log
}


#!/bin/bash
# filename : inotifyd.sh
[ ! -f /etc/yum.repos.d/epel.repo ] && \
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
if [ $? -eq 0 -a `rpm -qa inotify-tools|wc -l` -ne 1 ];then
yum -y install inotify-tools
fi
inotify=/usr/bin/inotifywait
rsync_host=rsync.etiantian.org
[ ! -d /data0 ] && mkdir /data0
$inotify -mrq --format '%w%f' -e create,close_write,delete /data0 \
| while read file
do
cd / &&
rsync -az /data0 --delete rsync_backup@${rsync_host}::backup/ --password-file=/etc/rsync.password
done

posted @ 2017-08-23 16:02  盛碗米饭  阅读(219)  评论(0编辑  收藏  举报