#获取本机ip地址(云服务器需要手动配置IP地址,需要根据实际环境确认IP是否正确)
#IP=$(/sbin/ip addr |grep inet |grep -v inet6 |grep em1|awk '{print $2}' |awk -F "/" '{print $1}')
IP=x.x.x.x
work_dir=$1
file_name=$2
backup_dir=备份路径
date=$(date "+%Y%m%d") ##获取系统时间
#判断work_dir和file_name是否为空:
if [[ ! -n "$work_dir" || ! -n "$file_name" ]] ;then
echo "ERROR:work_dir或file_name参数为空"
echo "commd:sh file_backup.sh work_dir file_name;两个参数都为必填参数)"
exit
else
#Log存放路径
LogFile=$backup_dir/log/${IP}_${file_name}_${date}.log
if [ ! -d '$backup_dir/log' ]; then
mkdir -p $backup_dir/log
fi
#新建日志文件
touch $LogFile
#追加日志到日志文件
echo "start backup, start at $(date +"%Y-%m-%d %H:%M:%S")" > $LogFile
echo "-------------------Start!---------------------------------" >> $LogFile
if [ ! -d '$backup_dir/$file_name' ]; then
mkdir -p $backup_dir/$file_name
fi
#备份文件
echo "备份相关文件 $file_name" >> $LogFile
echo "tar czf ${backup_dir}/${file_name}/${file_name}_${date}.tar.gz --exclude=不需要备份的文件可以使用此参数过滤掉 -C $work_dir $file_name" >> $LogFile
tar czf ${backup_dir}/${file_name}/${file_name}_${date}.tar.gz -C $work_dir $file_name
if [ $? -eq 0 ];then ##判断命令是否执行即数据是否备份成功
echo "Backup_Status="SUCCESS"" >> $LogFile
else
echo "Backup_Status="FAILED"" >> $LogFile
fi
##查看备份文件大小
Bar_Size=`du -sh $backup_dir/$file_name/${file_name}_${date}.tar.gz |awk '{print $1}'`
##获取备份文件的md5值
MD5=`/usr/bin/md5sum $backup_dir/$file_name/${file_name}_${date}.tar.gz | awk '{print $1}'`
echo "备份文件路径:$backup_dir/$file_name/${file_name}_${date}.tar.gz" >> $LogFile
echo "文件大小:${Bar_Size}">> $LogFile
echo "MD5: $MD5" >> $LogFile
echo "-----------------------------------------------------" >> $LogFile
###备份文件需要定时清理,要不会导致磁盘爆满。
###备份数据保留5天(保留时间可根据需求自行修改)
find $backup_dir/$file_name/ -mtime +5 -name "*.tar.gz" -exec rm -rf {} \;
###日志文件保存3天(保留时间可根据需求自行修改)
find $backup_dir/log -mtime +3 -name "*.log" -exec rm -rf {} \;
#备份完成追加日志到日志文件
echo "-----------------------------------Complete!----------------------------------------" >> $LogFile
echo "backup end, end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
##同步备份文件到备份服务器
echo "start rsync,$(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
rsync -rauvt --progress --password-file=/data/scripts/backup.pass --bwlimit=500 $backup_dir/$file_name/${file_name}_${date}.tar.gz backup@异地备份服务器的ip::backup/$IP/$file_name/
if [ $? -eq 0 ];then
echo "Rsync_Status="SUCCESS"" >> $LogFile
echo "rsync end,$(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
else
echo "Rsync_Status="FAILED"" >> $LogFile
echo "rsync end,$(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
fi
##拷贝日志文件到远程服务器
rsync -rauvt --progress --password-file=/data/scripts/backup.pass $LogFile backup@异地备份服务器的ip::logs
fi
pro_dir="/home/www"
pro_name="admin"
bak_dir="/home/backup"
bak_conf="/home/backup/bak_conf"
DATE=$(date +'%Y%m%d%H%M%S')
release_dir="/bak/release"
function bak_code() {
cd ${pro_dir}
test ! -d ${bak_dir} && mkdir ${bak_dir}
test ! -d ${bak_conf} && mkdir -p ${bak_conf}
tar -czf ${bak_dir}/${pro_name}_${DATE}.tar.gz ${pro_name} --exclude=${pro_name}/storage/* --exclude=${pro_name}/public/uploads/* --exclude=${pro_name}/public/storage/*
cd /usr/local/gacp/nginx/conf/vhosts.d/
tar -czf ${bak_conf}/back_conf_${DATE}_conf.tar.gz *
echo "${bak_dir}/${pro_name}_${DATE}.tar.gz"
echo "${bak_conf}/back_conf_${DATE}_conf.tar.gz"
}