shell脚本:


备份的脚本比较简单:
#!/bin/bash

# 定义年 月 日

year=`date  +%Y`

month=`date  +%m`

day=`date  +%d`



# 定义备份的目录

BACKDIR=/bak_1/mysql/$year/$month/$day



# MySQL的root密码

ROOTPASS=123456



# 建立目录

mkdir -p $BACKDIR



# 获取数据库列表

DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /`



# 备份

for dbname in $DBLIST

do

mysqlhotcopy $dbname -u root -p $ROOTPASS $BACKDIR | logger -t mysqlhotcopy

done



复制代码上面的脚本将数据库按"年/月/日"目录结构备份,将脚本存为back.sh添加到cron里每天凌晨3点执行


crontab -e


00 03 * * *   root /root/back.sh


下面的是每月3号删除上个月的备份,存为del_bak.sh,每月3号3点3分执行

03 03 03 * *   root /root/del_bak.sh


注意脚本上传后,要chmod一下权限,比如


chmod 755 /root/back.sh



#!/bin/bash


# 定义年 月 日

year=`date  +%Y`

month=`date  +%m`

#day=`date  +%d`



# 取上一个月

month=`expr $month - 1`  



# 如果是1月,则上一月为12,并且年也减1

  if [ $month -eq 0 ]; then

    month=12

    day=31

    year=`expr $year - 1`  

  fi



# 因为备份时小于10月的月份有前导零,所以这里判断如果小于10月,则加前导零

  if [ $month -lt 10 ]; then

    pre=0  

  fi



#获取要删除的目录名

BACKDIR=/bak1/mysql/$year/$pre$month



#删除了

rm -rf $BACKDIR


重新启动crond

/etc/rc.d/init.d/crond restart
Linux/Unix相关 | 评论(0) | 引用(0) | 阅读(1971)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]