前言
一、shell脚本基于tar进行自动备份
二、脚本
#!/bin/sh
dateTime=`date +%Y-%m-%d`
days=7
bakuser=user1
bakdir=/data/backupdata
bakdata=${bakuser}_${dateTime}.tar.gz
baklog=${bakuser}_${dateTime}.log
baksrcdir=/db/mysql/data
remotePath=/backupdata/dbdata
remoteIP="192.168.56.66"
cd ${bakdir}
mkdir -p ${bakuser}
cd ${bakuser}
echo "backup start at ${dateTime}" > ${baklog}
echo "---------------------------" >> ${baklog}
tar -zcvf ${bakdata} ${baksrcdir} >> ${baklog}
find ${bakdir}/${bakuser} -type f -name "*.log" -exec rm -rf {} \;
find ${bakdir}/${bakuser} -type f -name "*.tar.gz" -mtime +$days -exec rm -rf {} \;
rsync -avzPL ${bakdir}/${bakuser}/${bakdata} ${remoteIP}:${remotePath}