MySQL Local Area Network Backup Using Centos7
Database Backup is an important step. For online systems, databases without backups are insecure. This blog post provides a full backup, Incremental backup, and local area network (LAN) Scheduled backup for the MySQL database using shell script files by checking online data.
Server Introduction: centos 7.0
[Local full backup]
1. single database backup script file
# Vi mysql-backup.sh
[SQL] view plaincopy
- Db_user = "root" # username and password of the current server
- Db_passwd = "root"
- Db_host = "192.168.64.133" # local server address
- Db_name = "whp" # Name of the backup database
- # Thedirectoryforstoryyourbackupfile.
- Backup_dir = "/backup" # path of the file to be backed up
- # Dateformatforbackupfile (dd-mm-yyyy)
- Time = "$ (date +" % Y-% m-% d-% H-% M-% S ")"
- # Mysql, mysqldumpandsomeotherbin 'spath
- MYSQL = "/application/mysql/bin/mysql"
- MYSQLDUMP = "/application/mysql/bin/mysqldump"
- GZIP = "/bin/gzip"
- $ MYSQLDUMP-u $ db_user-h $ db_host-p $ db_passwd $ db_name | $ GZIP-9> "$ backup_dir/$ db_name" _ "prop"
2. retain full backup of 7-day historical data
[SQL] view plaincopy
- Db_user = "root"
- Db_passwd = "root"
- Db_host = "localhost"
- # Thedirectoryforstoryyourbackupfile.
- Backup_dir = "/application/backup /"
- # Dateformatforbackupfile (dd-mm-yyyy)
- Time = "$ (date +" % H-% M-% S-% m-% d-% y ")"
- # Mysql, mysqldumpandsomeotherbin 'spath
- MYSQL = "/application/mysql/bin/mysql"
- MYSQLDUMP = "/application/mysql/bin/mysqldump"
- MKDIR = "/bin/mkdir"
- RM = "/bin/rm"
- MV = "/bin/mv"
- GZIP = "/bin/gzip"
- # Checkthedirectoryforstorebackupiswriteable
- Test! -W $ backup_dir & echo "Error: $ backup_dirisun-writeable." & exit
- # Thedirectoryforstorythenewestbackup
- Test! -D "$ backup_dir/backup.0/" & $ MKDIR "$ backup_dir/backup.0 /"
- Echo "StarttoBackup ...";
- # Getalldatabases
- # Don 'tbackupinformation _ schema and performance_schema
- All_db = "$ ($ MYSQL-u $ db_user-h $ db_host-p $ db_passwd-Bse 'showdatabases ')"
- All_db =$ {all_db // information_schema /};
- All_db =$ {all_db // performance_schema /};
- Fordbin $ all_db
- Do
- $ MYSQLDUMP-u $ db_user-h $ db_host-p $ db_passwd $ db | $ GZIP-9> "$ backup_dir/backup.0/snapshot"
- Done
- # Deletetheoldestbackup
- Test-d "$ backup_dir/backup.7/" & $ RM-rf "$ backup_dir/backup.7"
- # Rotatebackupdirectory
- Forintin6543210
- Do
- If (test-d "$ backup_dir"/backup. "$ int ")
- Then
- Next_int = 'expr $ int + 1'
- $ MV "$ backup_dir"/backup. "$ int" "$ backup_dir"/backup. "$ next_int"
- Fi
- Done
- Echo "BackUpSuccess! "
- Exit0;
3. Modify shell script permissions and execute
# Chmod 700 mysql-backup.sh // only allow the Administrator to run this script
#./Mysql-backup.sh // execute the script, test once
[Incremental log file backup]
1. Create a script file:
# Vi incre-backup.sh
[SQL] view plaincopy
- # Execute mysqladmin to refresh the log file
- /Application/mysql/bin/mysqladmin-uroot-prootflush-logs
- # DATADIR =/var/lib/mysql
- DATADIR =/application/data # obtain the data file path
- BAKDIR =/backup # obtain the path of the target data file to be backed up
- ### If you have made special settings for mysqlbinlog, modify this field or modify the row that applies this variable: The machine name is used by default, and the machine name is used by mysql by default.
- # HOSTNAME = 'uname-N'
- Cd $ DATADIR # transfer to/application/data to query mysql-bin.index files
- # FILELIST = 'cat $ HOSTNAME-bin.index'
- FILELIST = 'catmysql-bin. Index'
- # COUNTERnumber
- COUNTER = 0
- Forfilein $ FILELIST
- Do
- COUNTER = 'expr $ COUNTER + 1'
- Done
- NextNum = 0
- Forfilein $ FILELIST
- Do
- Base = 'basename $ file'
- NextNum = 'expr $ NextNum + 1'
- If [$ NextNum-eq $ COUNTER]
- Then
- Echo "skiplastest"
- Else
- Dest = $ BAKDIR/$ base
- If (test-e $ dest)
- Then
- Echo "skipexist $ base"
- Else
- Echo "copying $ base"
- Cp $ base $ BAKDIR
- Fi
- Fi
- Done
- Echo "backupmysqlbinlogok"
2. Modify the File Permission and execute
# Chmod 700 incre-backup.sh // only allow the Administrator to run this script
#./Mysql-backup.sh // execute the script, test once
Automatic Backup]
Automatic Backup is based on the above script. the linux crontab command is used to automatically execute shell files at regular intervals:
# Crontab-e
Add:
00 01 ***/mysql-backup.sh // run at every day
Remote LAN backup]
Local Area Network Backup uses the NFS server to achieve data sharing over the local area network. It is achieved by mounting the NFS server, just as we usually share files and configure network connections, share your files. The implementation method is as follows:
1. NFS Server Installation
A) software packages required to install the NFS server:
# Yuminstall-y nfs-utils
B) edit the exports file
# Vim/etc/exports
#/Home/nfs/192.168.248.0/24 (rw, sync)
Hosts With the same network number as 192.168.248.0/24 can mount the/home/NFS/directory on the nfs server to their own file systems.
Rw indicates read/write; sync indicates synchronous write.
C) Start the nfs service.
# Systemctlstart rpcbind. service
# Systemctlstart nfs-server.service
D) Confirm that the NFS server is successfully started:
# Rpcinfo-p
E) If the instance is not successfully started, restart the rpcbind and nfs-server services:
# Systemctl restart rpcbind. service
# Systemctl restart nfs-server.service
Client installation
A) install nfs and start the rpcbind service.
# Yuminstall-y nfs-utils
B) Start rpcbind first
# Systemctlenable rpcbind. service
C) then start the rpcbind service:
Systemctlstart rpcbind. service
D) check whether the NFS server has a shared directory:
# Showmount-e 192.168.64.138
E) mount the server directory on the slave machine to a directory on the client (here, mount the local backup file path above to the server ):
# Mount-tnfs-o nolock, nfsvers = 1, vers = 3 192.168.64.138:/home/nfs/backup
Problem: mount. nfs: Stale NFS file handle
Solution: remove the mounting document from the client and remount the file:
# Umount-
[Backup recovery]
A. Restore the gzip database to Mysq
# Gunzip <dbname.gz | mysql-u Username-p dbname
B. Restore the database using binary files:
#/Mysql/bin/mysqlbinlog -- database = fox -- start-date = "5:00:00" -- stop-date = "9:00:00"/mysql/data/mysql-bin.000001 |/mysql/bin /mysql-u root-p123456-f
Database: Specifies a specific database.
Start-date: start Time
Stop-date: End Time
/Mysql/data/mysql-bin.000001: specify a binary file
[Summary]
The basic backup method has been completed, but we need to adopt different backup policies according to the specific application scenarios, so as to ensure security without consuming too much memory.