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