|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: The alias way to backup mysql database from command line |
| 4 | +description: "Tired of searching for the right command every time you need to back up a MySql database via mysqldump. Well make an alias out of it, and it should work for a while." |
| 5 | +author: ama |
| 6 | +permalink: /ama/the-alias-way-to-backup-a-mysql-database-from-command-line |
| 7 | +published: true |
| 8 | +categories: [database] |
| 9 | +tags: [mysql, bash, linux] |
| 10 | +--- |
| 11 | + |
| 12 | +As I have told you [before](http://www.codingpedia.org/ama/a-developers-guide-to-using-aliases/), I am really hooked on bash aliases[^2]. |
| 13 | + This blog emphasizes this point and shows how to make a MySql database backup via `mysqldump` with only three words, even with three letters, if you may like. |
| 14 | + |
| 15 | +[^1]: <http://tldp.org/LDP/abs/html/aliases.html> |
| 16 | +[^2]: <http://www.codingpedia.org/ama/a-developers-guide-to-using-aliases/> |
| 17 | + |
| 18 | +So, without further ado, let's see the alias: |
| 19 | + |
| 20 | +``` |
| 21 | +
|
| 22 | +$ alias mysql-backup-db_name='mysqldump db_name -u db_user -p -h 127.0.0.1 --port 3306 --single-transaction > path_to_back_up_directory/db_name_$(date "+%Y-%m-%d_%H:%M").sql' |
| 23 | +$ mysql-backup-db_name |
| 24 | +``` |
| 25 | + |
| 26 | +or, same result with three letters, if you'd like: |
| 27 | + |
| 28 | +``` |
| 29 | +
|
| 30 | +$ alias mbd='mysqldump db_name -u db_user -p -h 127.0.0.1 --port 3306 --single-transaction > path_to_back_up_directory/db_name_$(date "+%Y-%m-%d_%H:%M").sql' |
| 31 | +$ mbd |
| 32 | +``` |
| 33 | + |
| 34 | +<!--more--> |
| 35 | + |
| 36 | +The option `-u`, will ask you for the password. The port number is not mandatory (as it defaults to **3306**), but if you are using other port, you need to specify it. |
| 37 | + |
| 38 | +> I personally prefer the longer alias-approach, as the name is more clear, **there is auto complete**. Besides that I can always `alias-grep` it[^1] - (`alias alias-grep='alias | grep'`), |
| 39 | + if I need to see how it looks like - using the alias in this case as sort of documentation... |
| 40 | + |
| 41 | +Below there is a concrete example, where I backup the MySQL keycloak database: |
| 42 | + |
| 43 | +``` |
| 44 | +$ mysql-backup-keycloak-prod |
| 45 | + aka |
| 46 | +$ mysqldump keycloak -u keycloak -p -h 127.0.0.1 --port 3306 --single-transaction > ~/backup/db/keycloak_db_$(date "+%Y-%m-%d_%H:%M").sql |
| 47 | +``` |
| 48 | + |
| 49 | +That will generate a _.sql_ file in the specified path: |
| 50 | + |
| 51 | +``` |
| 52 | +$ ls -lrt ~/backup/db |
| 53 | +-rw-rw-r-- 1 ama ama 199219 Mar 10 07:04 keycloak_db_2017-03-10_07:04.sql |
| 54 | +-rw-rw-r-- 1 ama ama 198489 Apr 3 06:28 keycloak_db_2017-04-03_06:27.sql |
| 55 | +``` |
| 56 | + |
| 57 | +We can take things even further, and back-up a remote database from the local machine, after we build a ssh-tunnel[^3] with an alias of course: |
| 58 | + |
| 59 | +``` |
| 60 | +alias mysql-tunnel-linode='ssh -L 127.0.0.1:3305:127.0.0.1:3306 ama@w.x.y.z -N' |
| 61 | +alias mysql-backup-keycloak-prod='mysqldump keycloak -u keycloak -p -h 127.0.0.1 --port 3305 --single-transaction > ~/backup/db/keycloak_db_prod_$(date "+%Y-%m-%d_%H:%M").sql' |
| 62 | +``` |
| 63 | + |
| 64 | +Same back-up command as before, only the port differs now and is pointing to the tunnel port - 3305. |
| 65 | + |
| 66 | +With this I think I've made my point about the power and versatility of bash aliases. |
| 67 | + |
| 68 | + |
| 69 | +[^3]: <https://en.wikipedia.org/wiki/Tunneling_protocol> |
| 70 | + |
| 71 | +## References |
0 commit comments