xml地图|网站地图|网站标签 [设为首页] [加入收藏]

MySQL邮件服务器数据库备份脚本,linux下mysql数据

来源:http://www.ccidsi.com 作者:集成介绍 人气:76 发布时间:2019-11-02
摘要:2、实现备份脚本 #/bin/sh #  filename: mysqlbackup.sh #   crontab #   30 6 * * * /root/script/mysqlbackup.sh extmail -7 #   10 7 * * 0 /root/script/mysqlbackup.sh dspam -14 #   10 6 1 */3 * /root/script/mysqlbackup.sh --all-d

2、实现备份脚本
#/bin/sh
#  filename: mysqlbackup.sh
#   crontab
#   30 6 * * * /root/script/mysqlbackup.sh extmail -7
#   10 7 * * 0 /root/script/mysqlbackup.sh dspam -14
#   10 6 1 */3 * /root/script/mysqlbackup.sh --all-database -93
myuser='root'     #MYSQL数据库的客户名,能够利用普通有权力用帐号
mypasswd=‘yourpassword’         #MYSQL的密码
mydatabase=$1                               # 数据库名
dayover=$2                                     #备份文件保留长
nSUSEr='username for remote mache'          #长间隔分享服务器的客商名和密码
nspasswd=’password for remote mache’
command=/usr/bin/mysqldump
#把第一个参数以“-”分割并取第三段。--all-database 结果为all.
f1=`echo $1 |cut -d "-" -f3`                  
datef=`date %F`            #以YYYY-MM-DD展现日期。
 
#日子减去相应的天数后的日期,接收date -d "-7 days"
dateover=`date -d "$2 days" %Y-%m-%d`
filename="mysql.$f1.$datef.tar.gz"                        #备份输入压缩后的公文名
overfilename="mysql.$f1.$dateover.tar.gz"          #产生超期的文本名
#为了保证数据能健康输出压缩,接受先地方备份成功后运动到长途的情势。
tempfolder=/tmp
tempfilename="$tempfolder/mysql.$f1.temp.sql" 
TAR=tar
remoteshare=//windows share server ip/mail$
dest_dir=/mnt/ip0.16
log=/var/log/backup.log
recipient=postmaster@yourdomain.com
#SENDM=`mail -s "Mail Server Backup Log..." $recipient < $log`
#判别是不是有存在2个参数。
if [ $# -ne 2 ]; then
        echo "|-------------------------------------------------------------------|"
        echo "|usage:mysqlbackup.sh <--all-database | dbname> <-days>.       |"
        echo "|       --all-database: all database to backup.                                |"
        echo "|       dbname: database name to backup.                                    |"
        echo "|       -days: delete file for over date.                                           |"
        echo "|-------------------------------------------------------------------|"
        echo " usage error." > $log
    mail -s "MySQL backup faild for Mail Server.." $recipient
# $SENDM
        exit 1
fi
 
看清数据库是还是不是留存。
mysqldbfolder="/var/lib/mysql"
        if [ $1 != "--all-database" ]; then
                if [ ! -d "$mysqldbfolder/$1" ]; then
                        echo "Database no exit!" >$log
                          mail -s "MYSQL Backup faild,database no exit!" $recipient
                            #$SENDM
                        exit 1
                fi
        fi
#
#  If the function of the second parameter is equal to 0,then write “sucess” to the log file.
#

for db_back in $(cat $db)  

    #

    # this is a free shell script under gnu gpl version 2.0 or above

    # copyright (c) 2006 sam tang

    # feedback/comment/suggestions :

1、制定备份战术 

for db_back in $(cat $db)  

    # mysql_backup.sh: backup mysql databases and keep newest 5 days backup.

皇家88平台 1

#将备份好的上传到FTP服务器  

    #!/bin/sh

皇家88平台 2

  

    # m h  dom mon dow   command

writelog()
{
        if [ $2 -eq 0 ]; then
         echo $1 " ...... backup Sucess." >> $log
        else
         echo $1 " ..... backup Faild." >> $log
     mail -s "$1 Backup faild on Mail Server." $recipient
#         $SENDM
        exit 1
        fi;
}
# MySQL DUMP TO temp File /tmp/tempfilename
# tar tempfile to mysql.$f1.date.tar.gz
echo "Start Backuping...." > $log
echo "$datef "`date %T` >> $log
echo "------------------------" >> $log
$command -u$myuser -p$mypasswd $mydatabase > $tempfilename
writelog "mysql $1 ...." $?
cd tempfolder
$TAR czf $tempfolder/$filename $tempfilename
writelog "Tar...." $?
#
#认清/mnt/0.16那么些目录是或不是早就被mount了。
count=`mount |grep "$dest_dir" |wc -l`
if [ $count -eq 0 ]; then
        mount -o username=$nsuser,password=$nspasswd $remoteshare $dest_dir
fi
writelog "Mount...." $?
#
# Move Mysql Backup File to Network that is $remoteshare or /mnt/ip0.16
#
mv  $tempfolder/$filename $dest_dir/$filename
writelog "move $filename to $dest_dir " $?
#  Delete over  days ago of $2
# 删除过期的文书。
        if [ -f "$dest_dir/$overfilename" ]; then
                rm -rf $dest_dir/$overfilename
                writelog "$overfilename was deleted...." $?
        fi;
umount $dest_dir
writelog "umount $dest_dir" $?
#把每一日步的周转结果邮件出去。
mail -s "MySQL backup Complete for Mail Server.." $recipient<$log
#$SENDM
3、运维crontab -e做日程,或把前的的crontab例处的保存为文件放在cron.d中的day或weeks文件夹中。

             echo "Backup MySQL succeed" >>$eMailFile  

    #

正文书档案接纳mysqldump 对数据库进行备份,mysqldump 是使用SQL级其他备份机制,它将数据表导成 SQL脚本文件,在不相同的 MySQL 版本之间晋级时相对相比确切,那也是最常用的备份方法,mysqldump 比直接拷贝要慢些。

剧本放在 /home/user/mysql教程_backup.sh

echo "-------------------------" >> $eMailFile  

    28 16 * * * /home/user/mysql_backup.sh

     for db_name in $(cat $db)  

    #

    # your mysql login information

    # db_user is mysql username

    # db_passwd is mysql password

    # db_host is mysql host

    # -----------------------------

    db_user="root"

    db_passwd="password"

    db_host="localhost"

    # the directory for story your backup file.

    backup_dir="/home/mybackup"

    # date format for backup file (dd-mm-yyyy)

    time="$(date "%d-%m-%y")"

    # mysql, mysqldump and some other bin's path

    mysql="/usr/local/mysql/bin/mysql"

    mysqldump="/usr/local/mysql/bin/mysqldump"

    mkdir="/bin/mkdir"

    rm="/bin/rm"

    mv="/bin/mv"

    gzip="/bin/gzip"

    # check the directory for store backup is writeable

    test ! -w $backup_dir && echo "error: $backup_dir is un-writeable." && exit 0

    # the directory for story the newest backup

    test ! -d "$backup_dir/backup.0/" && $mkdir "$backup_dir/backup.0/"

    # get all databases

    all_db="$($mysql -u $db_user -h $db_host -p$db_passwd -bse 'show databases')"

    for db in $all_db

    do

    $mysqldump -u $db_user -h $db_host -p$db_passwd $db | $gzip -9 > "$backup_dir/backup.0/$time.$db.gz"

    done

    # delete the oldest backup

    test -d "$backup_dir/backup.5/" && $rm -rf "$backup_dir/backup.5"

    # rotate backup directory

    for int in 4 3 2 1 0

    do

    if(test -d "$backup_dir"/backup."$int")

    then

    next_int=`expr $int 1`

    $mv "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"

    fi

    done

    exit 0;

    备注:

    mysql是以mysql客商地点运转的,对/home /mybackup不可写也会退步

    chmod 777 /home/mybackup难题化解了

看贰个在lix下准时备份mysql数据库教程

添加backupmysqleveryday.sh(vi /data/shell/backupmysqleveryday.sh)

  #!/bin/sh

  #this shell is user for backup mysql data everyday

  #author:www.ieliwb.com

  #path-config

  base_mysql_path=/data/webserver/mysql/

  mysql_dump_path=/data/mysqlbackup/

  mnt_back_path=/mnt/web/mysqlbackup/

  ftp_back_path=mysql_data_backup/

  #mysql-config

  mysql_user=”root”

  mysql_pass=”*******”

  #ftp-config

  ftp_host=”ip”

  ftp_user=”********”

  ftp_pass=”********”

  backup_name=`date %y%m%d%h%m%s`

  cd ${mysql_dump_path}

  #mysqldump

  ${base_mysql_path}bin/mysqldump -u${mysql_user} -p${mysql_pass} -all-database > ${backup_name}.sql

  #pack

  tar zcf ${backup_name}.tar.gz ${backup_name}.sql

  rm -f ${backup_name}.sql

  #backup to mnt

  cp ${backup_name}.tar.gz ${mnt_back_path}${backup_name}.tar.gz

  #ftp to other host

  ftp -n<

  open ${ftp_host}

  user ${ftp_user} ${ftp_pass}

  bin

  prompt off

  cd ${ftp_back_path}

  passive

  put ${backup_name}.tar.gz

  close

  bye

  !

  2.增添准期安顿(vi /etc/crontab)

  01 0 * * * root /data/shell/backupmysqleveryday.sh

  每天0:01点执行

 

/home/user/mysql教程_backup.sh crontab # crontab -l #皇家88平台 , m h dom mon dow command 28 16 * * * /home/user/mysql_backup.sh 脚本如下 #!/bin/sh # mysql_backup.sh: backup...

             mail -s "MySQL Backup fail" $email < $eMailFile  

    # crontab -l

    ftp -nv $Host <<EOF  

    # last updated: 20 march 2006

MYSQL_BIN="/usr/local/mysql/bin"  

    crontab

             cd /zeng_dir  

    脚本如下

[email protected]  

    user wolf "wolf#123"  

    echo "-----------------------" >> $eMailFile  

DATE     #时间

 

else  

pass     #FTP的密码

StartTime=$(date "-d 1 day ago" "%Y-%m-%d %H:%M:%S")  

#将备份好的上传到FTP服务器  

        else  

    sleep 10  

    echo "-------------------------" >> $eMailFile  

eMailFile="$WAN_DIR/mail.log"  

EOF  

cd /zeng_dir  

DATA_DIR   #mysql数据目录

    for dbname in $(cat $db)  

echo "-----------------------" >> $eMailFile  

    zeng  

echo "       " > $eMailFile  

 

EOF  

 

    echo "尚未展开完全备份,将来开展全体备份"  

then  

pass=chenqibin  

cd $WAN_DIR  

function zeng() {  

gzdumpfile #减掉后的后缀名

  

shell进行全体和增量备份mysql数据库

backfile=`ls -l /wan_dir | wc -l`  

    quit  

#一同备份后删除本地增量备份文件,只保留这段日子二个礼拜的增量备份文件  

        then  

    echo "完全备份目录存在" >>$backup_log  

     echo "$StartTime start backup binlog" >> $eMailFile  

 

done  

WAN_DIR="/wan_dir"  

        if [ $? = 0 ]  

  

  

DATE=`date "%Y%m%d"`  

     do  

             echo "Backup MySQL fail" >>$eMailFile  

本文由68399皇家赌场发布于集成介绍,转载请注明出处:MySQL邮件服务器数据库备份脚本,linux下mysql数据

关键词: 68399皇家赌场

最火资讯