原创

批量备份脚本(scp传输文件)

1.文件目录格式

192.168.1.1:/app/1192.168.1.1:/app/2192.168.1.1:/app/3

按照以上格式(ip:备份文件完整目录)将需要备份文件整理成文本,命名为catbackupdir,与脚本放在同一目录即可。

2.脚本

在被备份服务器上需要创建bak用户,密码用base64加密后填写至脚本PASSWD变量中。

#!/bin/bash
DAYTIME=$(date +%Y%m%d)
BACKUPLOG="weekbackup.log"
BACKUPDIRFILE="catbackupdir"
FILELINE=$(cat $BACKUPDIRFILE|wc -l)
BACKUPDIR="/app/idc/appbackup/"
PASSWD=$(echo "********"|base64 -d)
SUCC=0
FAIL=0

echo $(date +%Y-%m-%d" "%H:%M:%S) "-----------------start-----------------" >>$BACKUPLOG
echo $(date +%Y-%m-%d" "%H:%M:%S) "$DAYTIME共需备份$FILELINE个文件或者目录" >>$BACKUPLOG
for ((i=1; i <= $FILELINE; i++))
do
echo $(date +%Y-%m-%d" "%H:%M:%S) "第$i个开始备份" >>$BACKUPLOG
COMM=`cat $BACKUPDIRFILE|head -$i|tail -1`
IP=`cat $BACKUPDIRFILE|head -$i|tail -1|awk -F ':' '{print $1}'`
DIR=`cat $BACKUPDIRFILE|head -$i|tail -1|awk -F ':' '{print $2}'`
TEMP="."
if [[ $DIR = *$TEMP* ]];then
        DIR=$(echo ${DIR%/*})
fi
DIR="$BACKUPDIR$DAYTIME/$IP$DIR"
if [ ! -d $DIR ]
        then
        mkdir -p $DIR
        echo $(date +%Y-%m-%d" "%H:%M:%S) "创建目录$DIR" >>$BACKUPLOG
else 
        echo $(date +%Y-%m-%d" "%H:%M:%S) "$DIR目录已经存在" >>$BACKUPLOG
fi
echo $(date +%Y-%m-%d" "%H:%M:%S) "开始备份$COMM" >>$BACKUPLOG

expect <<EOF
spawn scp -r bak@$COMM $DIR
expect "yes/no" {send "yes\n"}
expect "password" {send "$PASSWD\n"}
expect eof
EOF
if [ $? -eq 0 ]
        then
        let SUCC++
        echo $(date +%Y-%m-%d" "%H:%M:%S) "第$i个备份成功" >>$BACKUPLOG
else 
        let FAIL++
        echo $(date +%Y-%m-%d" "%H:%M:%S) "第$i个备份失败" >>$BACKUPLOG
fi
done
echo $(date +%Y-%m-%d" "%H:%M:%S) "备份完成,本次备份$FILELINE个文件或目录,成功$SUCC个,失败$FAIL个" >>$BACKUPLOG
echo $(date +%Y-%m-%d" "%H:%M:%S) "-----------------e n d-----------------" >>$BACKUPLOG

扩展说明:

1.使用echo 'passwd' | base64 命令生成加密字符

正文到此结束
温馨提示:
本文最后更新于 2024年05月20日,已超过 184 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
本文目录