database-import.sh 864 B

1234567891011121314151617181920212223242526272829
  1. #!/bin/bash
  2. DATE=$(date +%m-%d-%y-%s)
  3. #database config
  4. DB_USER="your_database_username"
  5. DB_PW="your_database_password"
  6. DB_CNF="your_database"
  7. TABLE_TARGET="the_database_table"
  8. TABLE_ROW_TARGET="the_database_row"
  9. KEY_TARGET="the_key_you_want_to_target"
  10. SCHEMA_LOCATION="/your/path/to/import.sql"
  11. BACKUP_DIRECTORY="/your/path/to/store/backups/"
  12. #do not edit below this line
  13. #backup
  14. echo " Backing up the database..."
  15. mysqldump -u"$DB_USER" -p"$DB_PW" --host=localhost "$DB_CNF" > "$BACKUP_DIRECTORY"/"$DB_CNF"-"$DATE".sql
  16. #purge the affected rows
  17. echo " Purging the $KEY_TARGET rows..."
  18. echo "DELETE FROM $TABLE_TARGET WHERE $TABLE_ROW_TARGET = '$KEY_TARGET' | mysql -u$DB_USER -p$DB_PW --host=localhost $DB_CNF"
  19. #import the new rows
  20. echo " Importing $SCHEMA_LOCATION"
  21. mysql -u "$DB_USER" -p"$DB_PW" "$DB_CNF" < "$SCHEMA_LOCATION"
  22. echo " IMPORT COMPLETE!"