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: