Skip to content

Instantly share code, notes, and snippets.

@nezarjhons
Forked from marcanuy/MYDB.cnf
Created December 6, 2021 04:57
Show Gist options
  • Save nezarjhons/db9ed45949e55fe480cf1c6c98fa099b to your computer and use it in GitHub Desktop.
Save nezarjhons/db9ed45949e55fe480cf1c6c98fa099b to your computer and use it in GitHub Desktop.
Database maintenance from console.
#!/bin/bash
# Backup database from console
DATABASE=
DEFAULTS_FILE=$DATABASE.cnf
BACKUP_DIR=
LOGS_DIR=
DB_OUT_FILENAME=$DEFAULTS_FILE-`date +\%Y\%m\%d`.sql.gz
mysqldump --defaults-extra-file=$DEFAULTS_FILE $DATABASE 2>> $LOGS_DIR/$DATABASE.log | gzip - > $BACKUP_DIR/$DB_OUT_FILENAME
[client]
host=
user=
password=
#!/bin/bash
# Truncate database tables from console
DATABASE=
DEFAULTS_FILE=$DATABASE.cnf
mysql --defaults-extra-file="$DEFAULTS_FILE" -Nse 'show tables' $DATABASE | while read table; do mysql --defaults-extra-file="$DEFAULTS_FILE" -e "truncate table $table" $DATABASE; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment