A summary of MySQL database backup method under Linux

Source: Internet
Author: User
Tags ftp create database mysql database

The functionality provided through MySQL

Export

Command:

Mysqldump-u user name-P database > database. sql

Example:

The code is as follows Copy Code

Mysqldump-u root-p db1 > Db1.sql (Backup of database db1 to Db1.sql)

Prompt to enter the password, complete.

Import

You need to create an empty database first

Mysql-u root-p (Enter the password after entering MySQL)
Create Database DB1 (creating a databases named DB1)
Exit (quit MySQL)

Command:

Mysql-u user name-p database < database. sql

Example:

The code is as follows Copy Code

Mysql-u root-p DB1 < Db1.sql (import data from backup file Db1.sql to database DB1)

Prompt to enter the password, complete.

by copying files
If the database is particularly large, it can be backed up using a copy, but may cause incompatibilities between different operating systems.
Export
Go to the MySQL directory and pack the database directories that need to be backed up
Example:

The code is as follows Copy Code

Cd/var/lib/mysql (enter MySQL directory)
TAR-ZCVF db1.tar.gz DB1 (Package db1 directory, if prompted without permission, in front plus sudo, prompted to enter a password can be)

Import
first create an empty database, copy the packaged files to the MySQL directory and unzip
Example:

The code is as follows Copy Code


Mysql-u root-p (Enter the password after entering MySQL)
Create Database DB1 (creating a databases named DB1)
Exit (quit MySQL)
CP db1.tar.gz/var/lib/mysql (copy packaged files to MySQL directory)
Cd/var/lib/mysql (enter MySQL directory)
TAR-ZXVF db1.tar.gz (extract to current directory)

Post-Linux script now

The code is as follows Copy Code

#!/bin/bash
#Mysql autobackup Shell#write by tuspark.cn
#-------------------Database-related user name, password, database name to be backed up, backup directory, etc.
Dbuser=root
Dbpasswd=xxxxx
Dbserver=localhost
Dbname=xxxxx
Dbopt=--opt
backupdir=/dcbackup/
#-------------------whether to open FTP remote backup, 0 is no, 1 is.
Copytoftp=1
Ftpserver=xxxxx
Ftpuser=xxxxx
Ftppasswd=xxxxx
#-------------------The following parameters
Fileprefix=dcradiusdump
Filename= $backupdir ' Date +%f '. sql
newfile= $fileprefix-' date +%f '. tar.gz
keepdays=10
#-------------------The following log for backup
Logfile=/var/log/mysqlbackup.log
Logtmp=/var/log/mybackup.tmp
#===============================================
if [!-D $backupdir]
Then
echo "$backupdir isn't exist, then make ..." >> $logfile
Mkdir-p $backupdir
Fi
echo "start====================================>" >> $logfile
echo "Beginning backup ' Date ' +%f%T '" >> $logfile
echo "Delete $keepdays days ago Files ..." >> $logfile
Find $backupdir-name $fileprefix *-mtime + $keepdays-fls $logtmp-exec rm {};
echo "Deleted Backup file is:" >> $logfile
Cat $logtmp >> $logfile
echo "Delete old file success!" >> $logfile
If [f $backupdir $newfile]
Then
echo "$newfile backup exist, backup stop ..." >> $logfile
Else
If [-Z $dbpasswd]
Then
Mysqldump-u$dbuser-h$dbserver $dbopt $dbname > $dumpfilename
Else
Mysqldump-u$dbuser-p$dbpasswd-h$dbserver $dbopt $dbname > $dumpfilename
Fi
Tar czvf $backupdir $newfile $dumpfilename >> $logfile 2>&1
echo "$backupdir $newfile Backup success!" >> $logfile
RM-FR $dumpfilename
if [$copytoftp = 1]; Then
If [-Z $ftpserver];then
echo "ftp Server not set,copy to ftp Failed ..." >> $logfile
Exit 1
elif [-Z $ftpuser];then
echo "FTP user not set, Copy to Ftp Failed ..." >> $logfile
Exit 2
Elif [Z $ftppasswd]; Then
echo "ftp password not set, Copy to FTP Failed ..." >> $logfile
Exit 3
Else
echo "Start copy to FTP server ..." >> $logfile
Ftp-n> $logfile
Fi

After writing, we add a scheduled task can be daily or hourly backup, Linux Scheduled Task set method detailed:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.