将A机的应用定时、自动备份到B机,有3种方式:nfs方式,ssh方式,lftp方式。
■■nfs方式
■远端配置nfs服务【略】
■本机配置nfs客户端【略】
■本机部署备份脚本
参见backup_app.sh
#!/bin/bash ###说明 #本脚本使用tar压缩方式,备份1个或多个指定路径的所有文件,存放到指定的路径 #然后,补充备份主机配置信息,并删除10天前的旧备份 #每次备份生成3个文件,即:应用备份、主机配置、备份日志 # IP地址+主机名+时间.tar.gz # IP地址+主机名+时间.hostconf # IP地址+主机名+时间.log #使用方法: backup_app.sh bak_path app_path1 [... app_pathn] #第1个参数:备份存放路径 #第2-n个参数:应用全路径【每个参数以反斜杠开头,最后不要跟反斜杠】 #将脚本加上x属性: chmod +x backup_app.sh #配置crontab定时任务示例: 15 01 * * * /root/backup_app.sh /root /root/app_path1 /root/app_path2 /root/app_path3 >/dev/null 2>&1 & #建议此处载入本机备份用户的环境变量 . /etc/profile . ~/.bash_profile #备份文件存放路径 bak_path=$1 if ! test -d ${bak_path} then mkdir -p ${bak_path} fi #要备份的应用全路径,以空格分隔 app_paths=echo $@|awk -F" " '{for(i=2;i<=NF;i++)name[i-1]=$i} {for(i=1;i<NF;i++)print name[i]}'
#备份保留时间,单位:天 days='10' localip=/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -1
hostname=hostname
baktime=date "+%Y-%m-%d_%H.%M.%S"
bakname=${localip}.${hostname}.${baktime} #备份所选路径 echo "开始备份:"date "+%Y-%m-%d_%H.%M.%S"
>> $bak_path/${bakname}.log cd $bak_path tar zcvf ${bakname}.tar.gz --exclude=*.tar --exclude=*.out --exclude=*.log --exclude=*.zip --exclude=*.csv --exclude=*.gz --exclude=*.dat $app_paths #同时备份主机配置信息:hosts文件、文件系统挂载配置、IP配置、环境变量配置;用户crontab任务、磁盘使用情况、文件系统mount现状 echo -e "======= etc_profile =======\n" > ${bakname}.hostconf cat /etc/profile >> ${bakname}.hostconf echo -e "\n======= etc_hosts =======\n" >> ${bakname}.hostconf cat /etc/hosts >> ${bakname}.hostconf echo -e "\n======= etc_fstab =======\n" >> ${bakname}.hostconf cat /etc/fstab >> ${bakname}.hostconf echo -e "\n======= ifconfig =======\n" >> ${bakname}.hostconf ifconfig >> ${bakname}.hostconf echo -e "\n======= bash_profile =======\n" >> ${bakname}.hostconf cat ~/.bash_profile >> ${bakname}.hostconf echo -e "\n======= crontab =======\n" >> ${bakname}.hostconf crontab -l >> ${bakname}.hostconf echo -e "\n======= df -h =======\n" >> ${bakname}.hostconf df -h >> ${bakname}.hostconf echo -e "\n======= mount =======\n" >> ${bakname}.hostconf mount >> ${bakname}.hostconf #删除X天之前的备份 find ${bak_path} -name "${localip}.${hostname}.\*" -mtime +${days} -exec rm -f "{} \;" echo "本次备份得到的备份文件名:" >> $bak_path/${bakname}.log ls -l ${bakname}.tar.gz ${bakname}.hostconf >> $bak_path/${bakname}.log echo "完成备份:"date "+%Y-%m-%d-%H:%M:%S"
>> $bak_path/${bakname}.log
■■ssh方式
■ssh登陆信任配置
【略】
■部署备份脚本
参见backup_app_ssh.sh
#!/bin/bash #建议此处载入本机备份用户的环境变量 . /etc/profile . ~/.bash_profile #定义本机备份文件存放路径,确保空间够用 workdir=/root/appbak #定义对端IP地址、ssh端口号、用户名、备份路径、删除几天前的备份 ip='192.168.2.12' port='22' username='root' passwd='hadoop' remotepath='/root/appbak' days='8' localip=/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -1
hostname=hostname
tmp=echo $1|awk -F"/" '{for(i=2;i<NF;i++)name[i-2]=$i"_"} {i=NF-2;name[i]=$NF} {print name[NF-2]}'
baktime=date "+%Y-%m-%d_%H.%M.%S"
bakname=${localip}.${hostname}.${tmp}.${baktime} #备份所选路径 echo "开始备份:"date "+%Y-%m-%d_%H.%M.%S"
cd $workdir tar zcvf ${bakname}.tar.gz --exclude=*.tar --exclude=*.out --exclude=*.log --exclude=*.zip --exclude=*.csv --exclude=*.gz --exclude=*.dat $1 #同时备份主机配置信息:hosts文件、文件系统挂载配置、IP配置、环境变量配置;用户crontab任务、磁盘使用情况、文件系统mount现状 echo -e "======= etc_profile =======\n" > ${bakname}.hostconf cat /etc/profile >> ${bakname}.hostconf echo -e "\n======= etc_hosts =======\n" >> ${bakname}.hostconf cat /etc/hosts >> ${bakname}.hostconf echo -e "\n======= etc_fstab =======\n" >> ${bakname}.hostconf cat /etc/fstab >> ${bakname}.hostconf echo -e "\n======= ifconfig =======\n" >> ${bakname}.hostconf ifconfig >> ${bakname}.hostconf echo -e "\n======= bash_profile =======\n" >> ${bakname}.hostconf cat ~/.bash_profile >> ${bakname}.hostconf echo -e "\n======= crontab =======\n" >> ${bakname}.hostconf crontab -l >> ${bakname}.hostconf echo -e "\n======= df -h =======\n" >> ${bakname}.hostconf df -h >> ${bakname}.hostconf echo -e "\n======= mount =======\n" >> ${bakname}.hostconf mount >> ${bakname}.hostconf #ssh方式异地备份 scp -P ${port} ${bakname}* ${username}@${ip}:${remotepath} #删除X天之前的异地备份 ssh -p ${port} ${username}@${ip} find ${remotepath} -name "${bakname}\*" -mtime +${days} -exec rm -f "{} \;" echo "完成备份:"date "+%Y-%m-%d-%H:%M:%S"
#删除本地备份 rm -rf ${bakname}*
将脚本加上x属性:
chmod +x backup_app_ssh.sh
■配置定时任务
参见nfs方式
■■lftp方式
■lftp安装
yum install lftp
■部署备份脚本
参见backup_app_lftp.sh
#!/bin/bash #建议此处载入本机备份用户的环境变量 . /etc/profile . ~/.bash_profile #定义本机备份文件存放路径,确保空间够用 workdir=/root/appbak #定义对端IP地址、ssh端口号、用户名、备份路径、删除几天前的备份 ip='192.168.2.12' port='22' username='root' passwd='hadoop' remotepath='/root/appbak' days='8' localip=/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -1
hostname=hostname
tmp=echo $1|awk -F"/" '{for(i=2;i<NF;i++)name[i-2]=$i"_"} {i=NF-2;name[i]=$NF} {print name[NF-2]}'
baktime=date "+%Y-%m-%d_%H.%M.%S"
bakname=${localip}.${hostname}.${tmp}.${baktime} #备份所选路径 echo "开始备份:"date "+%Y-%m-%d_%H.%M.%S"
cd $workdir tar zcvf ${bakname}.tar.gz --exclude=*.tar --exclude=*.out --exclude=*.log --exclude=*.zip --exclude=*.csv --exclude=*.gz --exclude=*.dat $1 #同时备份主机配置信息:hosts文件、文件系统挂载配置、IP配置、环境变量配置;用户crontab任务、磁盘使用情况、文件系统mount现状 echo -e "======= etc_profile =======\n" > ${bakname}.hostconf cat /etc/profile >> ${bakname}.hostconf echo -e "\n======= etc_hosts =======\n" >> ${bakname}.hostconf cat /etc/hosts >> ${bakname}.hostconf echo -e "\n======= etc_fstab =======\n" >> ${bakname}.hostconf cat /etc/fstab >> ${bakname}.hostconf echo -e "\n======= ifconfig =======\n" >> ${bakname}.hostconf ifconfig >> ${bakname}.hostconf echo -e "\n======= bash_profile =======\n" >> ${bakname}.hostconf cat ~/.bash_profile >> ${bakname}.hostconf echo -e "\n======= crontab =======\n" >> ${bakname}.hostconf crontab -l >> ${bakname}.hostconf echo -e "\n======= df -h =======\n" >> ${bakname}.hostconf df -h >> ${bakname}.hostconf echo -e "\n======= mount =======\n" >> ${bakname}.hostconf mount >> ${bakname}.hostconf #lftp方式异地备份 lftp -p ${port} -u ${username},${passwd} sftp://${ip} << EOF lcd ${workdir} cd ${remotepath} mput ${bakname}* exit EOF echo "完成备份:"date "+%Y-%m-%d-%H:%M:%S"
#删除本地备份 rm -rf ${bakname}*
将脚本加上x属性:
chmod +x backup_app_lftp.sh
■配置定时任务
参见nfs方式
文章评论