| 1 | #!/bin/bash |
| 2 | |
| 3 | # This script is used to export the MySQL structure of the plat/al MySQL |
| 4 | # databases. |
| 5 | |
| 6 | |
| 7 | # Developers: list 'public' tables here. |
| 8 | SHARED_TABLES="account_types \ |
| 9 | forums \ |
| 10 | geoloc_countries \ |
| 11 | geoloc_languages \ |
| 12 | log_actions \ |
| 13 | newsletter_cat \ |
| 14 | profile_binet_enum \ |
| 15 | profile_corps_enum \ |
| 16 | profile_corps_rank_enum \ |
| 17 | profile_education_degree \ |
| 18 | profile_education_degree_enum \ |
| 19 | profile_education_enum \ |
| 20 | profile_education_field_enum \ |
| 21 | profile_job_entreprise_term \ |
| 22 | profile_job_enum \ |
| 23 | profile_job_term_enum \ |
| 24 | profile_job_term_relation \ |
| 25 | profile_langskill_enum \ |
| 26 | profile_medal_enum \ |
| 27 | profile_medal_grade_enum \ |
| 28 | profile_name_enum \ |
| 29 | profile_networking_enum \ |
| 30 | profile_section_enum \ |
| 31 | profile_skill_enum \ |
| 32 | reminder_type \ |
| 33 | skins" |
| 34 | |
| 35 | usage() |
| 36 | { |
| 37 | cat << EOF |
| 38 | usage: $0 OPTIONS |
| 39 | |
| 40 | Will dump the necessary data from MySQL. |
| 41 | Note that all options should have sane defaults if you have correctly configured |
| 42 | your .my.cnf file. |
| 43 | |
| 44 | OPTIONS: |
| 45 | -h Show this message |
| 46 | -H HOST MySQL host to connect to (default: none) |
| 47 | -P PORT MySQL port to connect to (default: none) |
| 48 | -u USER User for MySQL (default: none) |
| 49 | -p PASS MySQL password to use (default: none) |
| 50 | -d SQL_DB Database to read from (default: x5dat) |
| 51 | -n Dry run, don't actually write anything |
| 52 | -o FILE Write to file instead of stdout (- for stdout as well) |
| 53 | |
| 54 | EOF |
| 55 | } |
| 56 | |
| 57 | HOST= |
| 58 | PASS= |
| 59 | PORT= |
| 60 | USER= |
| 61 | SQL_DB="x5dat" |
| 62 | FILE="-" |
| 63 | DRY_RUN=0 |
| 64 | |
| 65 | while getopts "hns:u:p:o:" OPTION |
| 66 | do |
| 67 | case $OPTION in |
| 68 | h) |
| 69 | usage |
| 70 | exit 1 |
| 71 | ;; |
| 72 | n) |
| 73 | DRY_RUN=1 |
| 74 | ;; |
| 75 | o) |
| 76 | FILE=$OPTARG |
| 77 | ;; |
| 78 | p) |
| 79 | PASS=$OPTARG |
| 80 | ;; |
| 81 | H) |
| 82 | HOST=$OPTARG |
| 83 | ;; |
| 84 | P) |
| 85 | PORT=$OPTARG |
| 86 | ;; |
| 87 | u) |
| 88 | USER=$OPTARG |
| 89 | ;; |
| 90 | d) |
| 91 | SQL_DB=$OPTARG |
| 92 | ;; |
| 93 | ?) |
| 94 | usage |
| 95 | exit |
| 96 | ;; |
| 97 | esac |
| 98 | done |
| 99 | |
| 100 | FILTER="sed -r s/AUTO_INCREMENT=[1-9]+/AUTO_INCREMENT=1/" |
| 101 | DUMPER="mysqldump --add-drop-table --default-character-set=utf8 --force" |
| 102 | |
| 103 | if [ -n "$USER" ]; then |
| 104 | DUMPER="$DUMPER --user=$USER" |
| 105 | fi |
| 106 | |
| 107 | if [ -n "$PASS" ]; then |
| 108 | DUMPER="$DUMPER --password=$PASS" |
| 109 | fi |
| 110 | |
| 111 | if [ -n "$HOST" ]; then |
| 112 | DUMPER="$DUMPER --host=$HOST" |
| 113 | fi |
| 114 | |
| 115 | if [ -n "$PORT" ]; then |
| 116 | DUMPER="$DUMPER --port=$PORT" |
| 117 | fi |
| 118 | |
| 119 | if [ "$FILE" == "-" ]; then |
| 120 | FILE="" |
| 121 | else |
| 122 | if [ $DRY_RUN -ne 1 ]; then |
| 123 | # Blank the file |
| 124 | echo "" > $FILE |
| 125 | fi |
| 126 | fi |
| 127 | |
| 128 | dump () { |
| 129 | local command=$1 |
| 130 | |
| 131 | if [ $DRY_RUN -eq 1 ]; then |
| 132 | if [ -n "$FILE" ]; then |
| 133 | echo "$command | $FILTER >> $FILE" |
| 134 | else |
| 135 | echo "$command | $FILTER" |
| 136 | fi |
| 137 | else |
| 138 | if [ -n "$FILE" ]; then |
| 139 | $command | $FILTER >> $FILE |
| 140 | else |
| 141 | $command | $FILTER |
| 142 | fi |
| 143 | fi |
| 144 | } |
| 145 | |
| 146 | # Dump structure |
| 147 | STRUCT_DUMPER="$DUMPER --no-data $SQL_DB" |
| 148 | dump "$STRUCT_DUMPER" |
| 149 | |
| 150 | SHARED_DUMPER="$DUMPER $SQL_DB $SHARED_TABLES" |
| 151 | dump "$SHARED_DUMPER" |