This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
computing:linux:mysql_backup [2016/10/29 14:41] gcooper |
computing:linux:mysql_backup [2018/10/01 14:33] (current) gcooper |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== MySQL and PostgreSQL Backup Script ====== | ====== MySQL and PostgreSQL Backup Script ====== | ||
- | You can modify this script to only backup MySQL or only PostgreSQL. | + | ===== Reset Forgotten |
+ | |||
+ | If necessary, as '' | ||
+ | |||
+ | < | ||
+ | sudo su postgres && psql | ||
+ | |||
+ | ALTER USER postgres with password ' | ||
+ | |||
+ | \q | ||
+ | |||
+ | exit | ||
+ | </ | ||
+ | |||
+ | ===== PostgreSQL Password ===== | ||
+ | |||
+ | For scripts that run as '' | ||
+ | |||
+ | < | ||
+ | touch ~/.pgpass && chmod 0600 ~/.pgpass && vim ~/.pgpass | ||
+ | </ | ||
+ | |||
+ | and add: | ||
+ | |||
+ | < | ||
+ | # | ||
+ | localhost: | ||
+ | </ | ||
===== db_backup.sh ===== | ===== db_backup.sh ===== | ||
- | :!: You must edit the script, at least for the MySQL root password. | + | :!: You must edit the script, at least for the MySQL 'root' |
+ | |||
+ | You can easily modify this script to only backup MySQL or only PostgreSQL. | ||
< | < | ||
Line 18: | Line 47: | ||
# Mysql password | # Mysql password | ||
- | MYSQLPW=" | + | MYSQLPW=" |
# Mysql username | # Mysql username | ||
MYSQLUSER=" | MYSQLUSER=" | ||
+ | |||
+ | # PostgreSQL backup username | ||
+ | PGSQLUSER=" | ||
# Location of the backup logfile. | # Location of the backup logfile. | ||
Line 36: | Line 67: | ||
# | # | ||
- | # PostgreSQL | + | # Postgresql |
# | # | ||
- | if pgrep postmaster | + | if pgrep postgres |
echo "Begin Backing up PostgreSQL Databases... | echo "Begin Backing up PostgreSQL Databases... | ||
Line 45: | Line 76: | ||
# Dump all databases at once | # Dump all databases at once | ||
- | / | + | / |
echo " | echo " | ||
echo "" | echo "" | ||
# Individual databases | # Individual databases | ||
- | databases=`psql -h localhost -U postgres | + | LIST_PG_DB_QUERY=" |
+ | |||
+ | databases=`psql -h localhost -U $PGSQLUSER | ||
for i in $databases; do | for i in $databases; do | ||
/ | / | ||
Line 94: | Line 127: | ||
exit | exit | ||
</ | </ | ||
+ | |||
+ | ====== Restore A MySQL Backup ====== | ||
+ | |||
+ | One-liner to restore a gzipped MySQL backup: | ||
+ | |||
+ | < | ||
+ | gunzip < mysql-database-name.sql.gz | mysql -p -h localhost mysql-database-name | ||
+ | </ | ||
+ | |||
+ | :!: You will need to enter the database password of the database on ' |