rsync服务器部署

#!/bin/bash

u="${1:-"rsync"}"
p="${2:-"123.com@"}"
d="${3:-"/backup"}"
check_system() {
    y_cmd=$1
    u_cmd=$2
    # Check if CentOS or Ubuntu
    if [ -f /etc/redhat-release ]; then
        DISTRO="centos"
    elif [ -f /etc/lsb-release ]; then
        DISTRO="ubuntu"
    else
        echo "Unsupported distribution"
        exit 1
    fi

    # Install packages based on distribution
    if [ "$DISTRO" == "centos" ]; then
        # Using yum
        echo "centos "
        /bin/bash -c "$y_cmd"
    elif [ "$DISTRO" == "ubuntu" ]; then
        # Using apt
        echo "ubuntu "

        /bin/bash -c "$u_cmd"
    fi
}
check_system "yum update && yum -y install rsync" "apt-get update && apt-get -y install rsync"

mkdir $d -p
chown -R $u.$u $d
useradd -s /sbin/nologin -M rsync

echo "$u:$p" >/etc/rsync.secrets
cat /etc/rsync.secrets
chmod 600 /etc/rsync.secrets
ls -l /etc/rsync.secrets
cat >/etc/rsyncd.conf <<EOF
uid = $u					     
gid = $u					    
use chroot = yes					
port 873						   
log file = /var/log/rsyncd.log		
pid file = /var/run/rsyncd.pid			
[server]	
read only = no				       
path = $d				
comment = Document Root of www.test.com
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z *.mp4 #同步时不再压缩的文件类型
file = /etc/rsync.secrets
EOF
cat >/etc/systemd/system/rsync.service <<EOF
[Unit]
Description=Rsync Daemon

[Service]
ExecStart=/usr/bin/rsync --daemon
Type=forking

[Install]
WantedBy=multi-user.target

EOF
sudo systemctl restart rsync
sudo systemctl enable rsync
sudo systemctl status rsync
netstat -lntup | grep 873
echo "/usr/bin/rsync --daemon" >>/etc/rc.local
tail -1 /etc/rc.local

echo """
推送
echo "$p" >/tmp/rsync.password
chmod 600 /tmp/rsync.password 
echo "hello" >hello
rsync -avz  hello [email protected]::server  --password-file=/tmp/rsync.password 

拉取
rsync -avz [email protected]::server ./ --password-file=/tmp/rsync.password

自定义端口拉取
rsync -avz --port 1873  [email protected]::server ./ --password-file=/tmp/rsync.password


"""
 
 
 

你可能感兴趣的:(服务器,运维)