首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使所有数据库备份与db名称相同

如何使所有数据库备份与db名称相同
EN

Stack Overflow用户
提问于 2019-07-25 15:53:49
回答 2查看 136关注 0票数 0

我在centos7上使用MySQL。我有50个数据库,像database1,database2...,database50。

如何设置cronjob,以便每天转储与数据库名称相同的所有数据库,如database1.sql、database2.sql ...使用单个命令或脚本的数据库50.sql。

请提供一些适当的解决方案,将不胜感激。

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2019-07-27 08:08:50

  1. 将当前日期转换为从某个起始日期开始的整数天数。
  2. 取该日期的模50。这会给你0 ..49.
  3. 添加1并连接。现在您有了database1 .. database50。将其放入外壳变量db
  4. mysqldump ... $db >$db.sql

票数 0
EN

Stack Overflow用户

发布于 2019-07-25 16:25:35

我正在使用这个脚本

代码语言:javascript
复制
#! /bin/bash

# MySQL database backup (databases in separate files) with daily, weekly and monthly rotation

# Sebastian Flippence (http://seb.flippence.net) originally based on code from: Ameir Abdeldayem (http://www.ameir.net)
# You are free to modify and distribute this code,
# so long as you keep the authors name and URL in it.

# Modified by IVO GELOV

# How many backups do you want to keep?
MAX_DAYS=5

# Date format that is appended to filename
DATE=`date +'%Y-%m-%d'`
DATSTR=`date '+%Y%m%d' -d "-$MAX_DAYS days"`

# MySQL server's name
SERVER=""

# Directory to backup to
BACKDIR="/var/db_arhiv/mysql"

#----------------------MySQL Settings--------------------#

# MySQL server's hostname or IP address
HOST="localhost"

# MySQL username
USER="user"

# MySQL password
PASS="password"

# List all of the MySQL databases that you want to backup, 
# each separated by a space. Or set the option below to backup all database
DBS="db1 db2"

# Set to 'y' if you want to backup all your databases. This will override
# the database selection above.
DUMPALL="y"


# Custom path to system commands (enable these if you want use a different 
# location for PHP and MySQL or if you are having problems running this script)
MYSQL="/usr/local/mysql/bin/mysql"
MYSQLDUMP="/usr/local/mysql/bin/mysqldump" 


function checkMysqlUp() {
    $MYSQL -N -h $HOST --user=$USER --password=$PASS -e status > /dev/null
}
trap checkMysqlUp 0

function error() {
  local PARENT_LINENO="$1"
  local MESSAGE="$2"
  local CODE="${3:-1}"
  if [[ -n "$MESSAGE" ]] ; then
    echo "Error on or near line ${PARENT_LINENO}: ${MESSAGE}; exiting with status ${CODE}"
  else
    echo "Error on or near line ${PARENT_LINENO}; exiting with status ${CODE}"
  fi
  exit "${CODE}"
}
trap 'error ${LINENO}' ERR

# Check backup directory exists
# if not, create it
if  [ ! -e "$BACKDIR/$DATE" ]; then
    mkdir -p "$BACKDIR/$DATE"
    echo "Created backup directory (${BACKDIR}/${DATE})"
fi

if  [ $DUMPALL = "y" ]; then
    echo "Creating list of databases on: ${HOST}..."

    $MYSQL -N -h $HOST --user=$USER --password=$PASS -e "show databases;" > ${BACKDIR}/dbs_on_${SERVER}.txt

    # redefine list of databases to be backed up
    DBS=`sed -e ':a;N;$!ba;s/\n/ /g' -e 's/Database //g' ${BACKDIR}/dbs_on_${SERVER}.txt`
fi

echo "Backing up MySQL databases..."

#cd ${LATEST}
for database in $DBS; do
  if [ ${database} = "information_schema" ] || [ ${database} = "performance_schema" ] || [ ${database} = "pinba" ]
  then
    continue
  fi
    echo "${database}..."
    $MYSQLDUMP --host=$HOST --user=$USER --password=$PASS --default-character-set=utf8 --routines --triggers --lock-tables --disable-keys --force --single-transaction --allow-keywords --dump-date $database > ${BACKDIR}/${DATE}/${SERVER}$database.sql
done

if  [ $DUMPALL = "y" ]; then
    rm -f ${BACKDIR}/dbs_on_${SERVER}.txt
fi

# dump privileges
$MYSQL -N -h $HOST --user=$USER --password=$PASS --skip-column-names -A -e "SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user" | $MYSQL -N -h $HOST --user=$USER --password=$PASS --skip-column-names -A > ${BACKDIR}/${DATE}/${SERVER}_grants.sql

# delete older files
for x in `find ${BACKDIR}/20* -type d`
do
    xd=`basename "${x//-/}"`
    if [[ $xd < $DATSTR ]]
    then
        rm -rf "$x"
    fi
done

echo "MySQL backup is complete"
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57196935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档