dump all mysql tables into separate files automagically?

后端 未结 10 959
北恋
北恋 2020-11-29 20:21

I\'d like to get dumps of each mysql table into separate files. The manual indicates that the syntax for this is

mysqldump [options] db_name [tbl_name ...]
         


        
10条回答
  •  长情又很酷
    2020-11-29 20:38

    You can accomplish this by:

    1. Get the list of databases in mysql
    2. dump each database with mysqldump
    # Optional variables for a backup script
    MYSQL_USER="root"
    MYSQL_PASS="something"
    BACKUP_DIR=/srv/backup/$(date +%Y-%m-%dT%H_%M_%S);
    test -d "$BACKUP_DIR" || mkdir -p "$BACKUP_DIR"
    # Get the database list, exclude information_schema
    for db in $(mysql -B -s -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' | grep -v information_schema)
    do
      # dump each database in a separate file
      mysqldump -u $MYSQL_USER --password=$MYSQL_PASS "$db" | gzip > "$BACKUP_DIR/$db.sql.gz"
    done
    

提交回复
热议问题