The following Linux shell script is used to automatically back up the MySQL database on a daily basis, which can be performed on a daily basis via Linux crontab. In the script you can set up a list of database tables that need to be backed up, and the backup files will be compressed by gzip. It should be noted that this script only applies to environments where data consistency requirements are not high.
| code is as follows |
copy code |
| #!/bin/ Bash mysql_pwd= "password" mysql_dump= "/usr/local/mysql/bin/mysqldump" cur_year=$ (date + "%Y") Cur_ month=$ (date + "%m") cur_day=$ (date + "%d") dump_path= "/usr/backup/mysql/$cur _year-$cur _month/$cur _day" arr_tables= ( table_1 "table_2" "Table_3" ) If [!-d "$dump _path"]; then mkdir-p "$dump _path " Fi for cur_table in ${arr_tables[*]}; Do $mysql _dump-uroot-p$mysql_pwd--opt mydb $cur _table | gzip > $dump _path/$cur _table.sql.gz Done |