#!/bin/bash
File=/var/log/messages
BackDir=/work/log-bak
Data=`date +%F`
IpNumbers=$(awk '{print $4}' $File|uniq -c|awk '{a[$2]+=$1}END{for(i in a) print i}')
cp $File $BackDir/logs/messages.$Data
echo "">$File
cp /var/log/secure $BackDir/logs/secure.$Data
echo "">/var/log/secure
for ip in $IpNumbers
do
[ ! -d $BackDir/ipsub/$ip ] && mkdir $BackDir/ipsub/$ip
grep $ip $BackDir/logs/messages.$Data >>$BackDir/ipsub/$ip/`date +%F`.messages
grep $ip $BackDir/logs/secure.$Data >>$BackDir/ipsub/$ip/`date +%F`.secure
done
数据库备份
第一种:
MySQL 数据库备份单循环
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=localhost
USER=backup
PASS=123.com
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")
for DB in $DB_LIST; do
BACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sql
if ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; then
echo "$BACKUP_NAME 备份失败!"
fi
done
第二种:
#!/bin/bash
#备份目录
backup_dir="/data/mysqlbackup"
#目录不在则创建
[ ! -d /mysqlbackup ] && mkdir /data/mysqlbackup
# MySQL 用户名和密码
mysql_username="your_username" #用户名
mysql_password="your_password" #密码
# 备份名称前缀
backup_prefix="nlpt"
# 当前日期
current_date=$(date +%Y-%m-%d-%H-%M-%S)
# 备份文件名
backup_file="${backup_prefix}-${current_date}.sql"
# 压缩文件名
compressed_file="${backup_prefix}-${current_date}.tar.gz"
# 备份数据库
mysqldump -u $mysql_username -p${mysql_password} --all-databases > "${backup_dir}/${backup_file}"
# 压缩备份文件
cd ${backup_dir}
tar -czvf "${compressed_file}" "${backup_file}"
# 删除原始备份文件
rm "${backup_dir}/${backup_file}"
# 删除30天前的备份文件
find $backup_dir -name $backup_file -type f -mtime +90 -exec rm {} \;
第三种:
mysql数据库备份脚本
#!/bin/bash
tool=mysqldump
username="root" #数据库账号
password="123456"#数据库密码
port="3306"#数据库端口
#是否采用--all-databases备份所有数据库,是填写Y,否填其他
bak_all=Y
#将要备份的数据库,填写将要备份的数据库名,和上个参数二选一
database_arr=('mysql' 'ceshi')
time=`date +%Y%m%d%H%M%S`
mkdir -p {/data/script,/data/backup/mysql}
backup_dir="$BASE_DIR/$(date +'%Y%m%d')"
log_file="/data/script/info.log"
days=7 #备份数据保留时间
if [ ! -d $backup_dir/mysqlbak_$time ];
then
mkdir -p $backup_dir/mysqlbak_$time;
fi
if [ $bak_all == "Y" ];
then
$tool -u$username -p$password -P$port --master-data \
--quick \
--max-allowed-packet=100M \
--extended-insert \
--set-gtid-purged=OFF \
--default-character-set=utf8 \
--single-transaction \
-all-databases \
|gzip \
> $backup_dir/mysqlbak_$time/mysqlbak_all_$time.sql.gz;
else
i=0
while [ $i -lt "${#database_arr[*]}" ]
do
database=${database_arr[$i]}
$tool -u$username -p$password -P$port --master-data \
--quick \
--max-allowed-packet=100M \
--extended-insert \
--set-gtid-purged=OFF \
--default-character-set=utf8 \
--single-transaction \
-all-databases \
$database \
|gzip \
>$backup_dir/mysqlbak_$time/mysqlbak_${database}_${time}.sql.gz;
let i+=1
done
fi
#删除7天前的备份
find $backup_dir -maxdepth 1 -type d -mtime +$days -name 'mysqlbak*' -exec rm -rf {} \;