There have been several online databases recently, and the hard disk is relatively tight, but it is not convenient to resize and transfer to other machines. However, data is important and needs to be backed up frequently.
The earliest practice was to back up the complete database locally and push the backup database and binlog together to a remote backup host. However, due to the recent tight space, the backup database needs to occupy a large amount of space. Is there any other solution?
After checking that mysqldump has the remote backup function, you can directly back up the remote data to a local device, which greatly saves the disk space of the remote database.
So I tried to write a backup script and run it regularly with crontab. Currently, it seems that the results are not bad.
You can directly use the online environment with a slight modification. If you have any questions, contact O (always _ blank) O ~
Note: This method is applicable to the Intranet. For Internet, you need to consider network factors such as the data size and bandwidth.
The script content is as follows:
- #ByCuiyuanrong
- # Remote backup of mysql Data and binlog to local!
-
- Mysql_user ="Admin"
- Mysql_password ="Admin_password"
- Remote_ips = 'cat/data/shell_files/ips.txt'
- Mysql_port ="3306"
- TIME='Date '+ % Y % m % d''
-
- Source/etc/profile
- Rm-f/data/shell_files/db. list
- # Loop each IP Address
- ForRemote_ipIn$ Remote_ips
- Do
- # Read the database name of each remote mysql Server
- Echo"=============$ Remote_ip 'date' + % Y _ % m _ % d-% H: % M: % s' ============">/Data/shell_files/history_mysql_backup.log
- /App/mysql/bin/mysql-- Host = "$ remote_ip" -- port = "$ mysql_port" -- user = "$ mysql_user" -- password = "$ mysql_password" <EOF>/data/shell_files/db. list
- Show databases;
- Exit
- EOF
- # Use sed to filter out databases without backup
- Sed-I'/Database \ | information_schema \ | performance_schema \ | test/d'/Data/shell_files/db. list
- # Create a backup directory
- Mkdir-p/data/mysql_backup/$TIME/$ Remote_ip/data/
- Mkdir-p/data/mysql_backup/$TIME/$ Remote_ip/binlog/
- # Backing up each database in a loop
- ForDBIn'Cat/data/shell_files/db. list'
- Do
- Echo"Begin dump $ DB 'date' + % Y _ % m _ % d-% H: % M: % S ''">/Data/shell_files/history_mysql_backup.log
- /App/mysql/bin/mysqldump-- Host = "$ remote_ip" -- port = "$ mysql_port" -- user = "$ mysql_user" -- password = "$ mysql_password" -- opt -- flush-logs -- master-data = 2 -- single-transaction -- default-character-set = utf8 $ DB | gzip-f>/data/mysql_backup/$ TIME/$ remote_ip/data/mongodb. SQL .gz
- Echo"End dump $ DB">/Data/shell_files/history_mysql_backup.log
- Done
- # Backing up binlog log files
- Echo"Begin rsync binlogs $ remote_ip">/Data/shell_files/history_mysql_backup.log
- Rsync-e'Ssh-p 22-o StrictHostKeyChecking = no'-Avz-- Progress -- delete root @ $ remote_ip:/data/mysql/binlog/data/mysql_backup/$ TIME/$ remote_ip/binlog/
- Echo"End rsync binlogs $ remote_ip">/Data/shell_files/history_mysql_backup.log
- Sleep 60
- Done