mysql表备份脚本

今天看了老男孩库表份脚本的视频,自己整理了一下

#!/bin/bash
HOSTNAME=root
MYPASS=abcd1234
SOCK=/var/lib/mysql/mysql.sock
DATE=$(date +%F)
MYCMD="mysql -u$HOSTNAME -p$MYPASS -S $SOCK"
MYDUMP="mysqldump -u$HOSTNAME -p$MYPASS -S $SOCK"
for date in $($MYCMD -e "show databases" | sed '1,2d')
do
   mkdir /backup/${date} -p
     for table in $($MYCMD -e "show tables from $date" | sed '1d')
     do
       $MYDUMP $date $table|gzip >/backup/${date}/${date}_${table}_$DATE.sql.gz
     done
done

运行结果如下

[root@www backup]# tree /backup
/backup
|-- abc
|   |-- abc_ab_2013-12-19.sql.gz
|   `-- abc_adb_2013-12-19.sql.gz
|-- cactidb
|   |-- cactidb_cdef_2013-12-19.sql.gz
|   |-- cactidb_cdef_items_2013-12-19.sql.gz
|   |-- cactidb_colors_2013-12-19.sql.gz
|   |-- cactidb_data_input_2013-12-19.sql.gz
|   |-- cactidb_data_input_data_2013-12-19.sql.gz
|   |-- cactidb_data_input_fields_2013-12-19.sql.gz
|   |-- cactidb_data_local_2013-12-19.sql.gz
|   |-- cactidb_data_template_2013-12-19.sql.gz
|   |-- cactidb_data_template_data_2013-12-19.sql.gz
|   |-- cactidb_data_template_data_rra_2013-12-19.sql.gz
|   |-- cactidb_data_template_rrd_2013-12-19.sql.gz
|   |-- cactidb_graph_local_2013-12-19.sql.gz
|   |-- cactidb_graph_template_input_2013-12-19.sql.gz
|   |-- cactidb_graph_template_input_defs_2013-12-19.sql.gz
|   |-- cactidb_graph_templates_2013-12-19.sql.gz
|   |-- cactidb_graph_templates_gprint_2013-12-19.sql.gz
|   |-- cactidb_graph_templates_graph_2013-12-19.sql.gz
|   |-- cactidb_graph_templates_item_2013-12-19.sql.gz
|   |-- cactidb_graph_tree_2013-12-19.sql.gz
|   |-- cactidb_graph_tree_items_2013-12-19.sql.gz
|   |-- cactidb_host_2013-12-19.sql.gz
|   |-- cactidb_host_graph_2013-12-19.sql.gz
|   |-- cactidb_host_snmp_cache_2013-12-19.sql.gz
|   |-- cactidb_host_snmp_query_2013-12-19.sql.gz
|   |-- cactidb_host_template_2013-12-19.sql.gz
|   |-- cactidb_host_template_graph_2013-12-19.sql.gz
|   |-- cactidb_host_template_snmp_query_2013-12-19.sql.gz
|   |-- cactidb_poller_2013-12-19.sql.gz
|   |-- cactidb_poller_command_2013-12-19.sql.gz
|   |-- cactidb_poller_item_2013-12-19.sql.gz
|   |-- cactidb_poller_output_2013-12-19.sql.gz
|   |-- cactidb_poller_reindex_2013-12-19.sql.gz
|   |-- cactidb_poller_time_2013-12-19.sql.gz
|   |-- cactidb_rra_2013-12-19.sql.gz
|   |-- cactidb_rra_cf_2013-12-19.sql.gz
|   |-- cactidb_settings_2013-12-19.sql.gz
|   |-- cactidb_settings_graphs_2013-12-19.sql.gz
|   |-- cactidb_settings_tree_2013-12-19.sql.gz
|   |-- cactidb_snmp_query_2013-12-19.sql.gz
|   |-- cactidb_snmp_query_graph_2013-12-19.sql.gz
|   |-- cactidb_snmp_query_graph_rrd_2013-12-19.sql.gz
|   |-- cactidb_snmp_query_graph_rrd_sv_2013-12-19.sql.gz
|   |-- cactidb_snmp_query_graph_sv_2013-12-19.sql.gz
|   |-- cactidb_user_auth_2013-12-19.sql.gz
|   |-- cactidb_user_auth_perms_2013-12-19.sql.gz
|   |-- cactidb_user_auth_realm_2013-12-19.sql.gz
|   |-- cactidb_user_log_2013-12-19.sql.gz
|   `-- cactidb_version_2013-12-19.sql.gz
|-- mysql
|   |-- mysql_columns_priv_2013-12-19.sql.gz
|   |-- mysql_db_2013-12-19.sql.gz
|   |-- mysql_func_2013-12-19.sql.gz
|   |-- mysql_help_category_2013-12-19.sql.gz
|   |-- mysql_help_keyword_2013-12-19.sql.gz
|   |-- mysql_help_relation_2013-12-19.sql.gz
|   |-- mysql_help_topic_2013-12-19.sql.gz
|   |-- mysql_host_2013-12-19.sql.gz
|   |-- mysql_proc_2013-12-19.sql.gz
|   |-- mysql_procs_priv_2013-12-19.sql.gz
|   |-- mysql_tables_priv_2013-12-19.sql.gz
|   |-- mysql_time_zone_2013-12-19.sql.gz
|   |-- mysql_time_zone_leap_second_2013-12-19.sql.gz
|   |-- mysql_time_zone_name_2013-12-19.sql.gz
|   |-- mysql_time_zone_transition_2013-12-19.sql.gz
|   |-- mysql_time_zone_transition_type_2013-12-19.sql.gz
|   `-- mysql_user_2013-12-19.sql.gz
|-- stu
|   |-- stu_aab_2013-12-19.sql.gz
|   |-- stu_ab_2013-12-19.sql.gz
|   |-- stu_caab_2013-12-19.sql.gz
|   `-- stu_cadab_2013-12-19.sql.gz
|-- test
|   |-- test_ab_2013-12-19.sql.gz
|   |-- test_ac_2013-12-19.sql.gz
|   `-- test_gc_2013-12-19.sql.gz
`-- zy


你可能感兴趣的:(mysql,databases,tables,老男孩)