文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

linux下mysql如何自动备份shell脚本

2022-06-04 21:48

关注

Linux 服务器上的程序每天都在更新 MySQL 数据库,于是就想起写一个 shell 脚本,结合 crontab,定时备份数据库。其实非常简单,主要就是使用 MySQL 自带的 mysqldump 命令。


 #!/bin/bash 
# Shell script to backup MySql database  
# To backup Nysql databases file to /backup dir and later pick up by your  
# script. You can skip few databases from backup too. 
# For more info please see (Installation info): 
# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/01/mysql-backup-script.html  
# Last updated: Aug - 2005 
# -------------------------------------------------------------------- 
# This is a free shell script under GNU GPL version 2.0 or above 
# Copyright (C) 2004, 2005 nixCraft project 
# Feedback/comment/suggestions : http://cyberciti.biz/fb/ 
# ------------------------------------------------------------------------- 
# This script is part of nixCraft shell script collection (NSSC) 
# Visit http://bash.cyberciti.biz/ for more information. 
# ------------------------------------------------------------------------- 
MyUSER="SET-MYSQL-USER-NAME"   # USERNAME 
MyPASS="SET-PASSWORD"    # PASSWORD  
MyHOST="localhost"     # Hostname 
# Linux bin paths, change this if it can not be autodetected via which command 
MYSQL="$(which mysql)" 
MYSQLDUMP="$(which mysqldump)" 
CHOWN="$(which chown)" 
CHMOD="$(which chmod)" 
GZIP="$(which gzip)" 
# Backup Dest directory, change this if you have someother location 
DEST="/backup" 
# Main directory where backup will be stored 
MBD="$DEST/mysql" 
# Get hostname 
HOST="$(hostname)" 
# Get data in dd-mm-yyyy format 
NOW="$(date +"%d-%m-%Y")" 
# File to store current backup file 
FILE="" 
# Store list of databases  
DBS="" 
# DO NOT BACKUP these databases 
IGGY="test" 
[ ! -d $MBD ] && mkdir -p $MBD || : 
# Only root can access it! 
$CHOWN 0.0 -R $DEST 
$CHMOD 0600 $DEST 
# Get all database list first 
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')" 
for db in $DBS 
do 
  skipdb=-1 
  if [ "$IGGY" != "" ]; 
  then 
  for i in $IGGY 
  do 
    [ "$db" == "$i" ] && skipdb=1 || : 
  done 
  fi 
  if [ "$skipdb" == "-1" ] ; then 
  FILE="$MBD/$db.$HOST.$NOW.gz" 
  # do all inone job in pipe, 
  # connect to mysql using mysqldump for select mysql database 
  # and pipe it out to gz file in backup dir :) 
    $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE 
  fi 
done

保存后将以上脚本加入crontab调度。如:每天早上四点半备份:30 4 * * * /data/backup-db.sh

如果你使用mysql5.1,可能会提示mysqldump 错误:


mysqldump: Couldn't execute 'show create table `general_log`': Table 'mysql.general_log' doesn't exist


mysqldump: Couldn't execute 'show create table `slow_log`': Table 'mysql.slow_log' doesn't exist

原因是mysql库中没有show_log表和general_log表,需要手动创建:


CREATE TABLE IF NOT EXISTS general_log ( 
 event_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
 user_host mediumtext NOT NULL, 
 thread_id int(11) NOT NULL, 
 server_id int(10) unsigned NOT NULL, 
 command_type varchar(64) NOT NULL, 
 argument mediumtext NOT NULL 
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log'; 
CREATE TABLE IF NOT EXISTS slow_log ( 
 start_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
 user_host mediumtext NOT NULL, 
 query_time time NOT NULL, 
 lock_time time NOT NULL, 
 rows_sent int(11) NOT NULL, 
 rows_examined int(11) NOT NULL, 
 db varchar(512) NOT NULL, 
 last_insert_id int(11) NOT NULL, 
 insert_id int(11) NOT NULL, 
 server_id int(10) unsigned NOT NULL, 
 sql_text mediumtext NOT NULL 
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';

方法二:

注意:


DumpFile=db$(date +%y%m%d)如果设置为这样一定要将此脚本放备份目录下才行。
DumpFile="$BackupPath"db$(date +%y%m%d) 如果设置为这样,日志中会有这样的提示tar: Removing leading `/' from member names 是因为备份的目录使用的是绝对路径,不过这样不影响数据,可以根据自己习惯而定。
-------------------------------------------------------------------start
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2004-09
#Setting
#设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置,以及备份方式
#默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy
#默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz
DBName=mysql
DBUser=root
DBPasswd=
BackupPath=/root/
LogFile=/root/db.log
DBPath=/var/lib/mysql/
#BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#Setting End

NewFile="$BackupPath"db$(date +%y%m%d).tgz
DumpFile="$BackupPath"db$(date +%y%m%d)
OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
if [ -f $OldFile ]
then
rm -f $OldFile >> $LogFile 2>&1
echo "[$OldFile]Delete Old File Success!" >> $LogFile
else
echo "[$OldFile]No Old Backup File!" >> $LogFile
fi
if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile
else
case $BackupMethod in
mysqldump)
if [ -z $DBPasswd ]
then
mysqldump -u $DBUser --opt $DBName > $DumpFile
else
mysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
mysqlhotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1
else
mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
*)
/etc/init.d/mysqld stop >/dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
/etc/init.d/mysqld start >/dev/null 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
;;
esac
fi
echo "-------------------------------------------" >> $LogFile
---------------------------------------------------------------------------------------------end

以上内容就是本文给大家介绍的linux下mysql如何自动备份shell脚本,希望大家喜欢。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯