Difference between revisions of "Postgres Backup & Restore"
Jump to navigation
Jump to search
(One intermediate revision by the same user not shown) | |||
Line 11: | Line 11: | ||
pg_restore | pg_restore | ||
+ | ``` | ||
+ | |||
+ | https://stackoverflow.com/questions/2732474/restore-a-postgres-backup-file-using-the-command-line | ||
+ | |||
+ | ``` | ||
+ | #!/bin/bash | ||
+ | set -eu | ||
+ | if [ $# -ne 1 ]; then | ||
+ | echo "Usage: $0 <db source id" | ||
+ | echo "Example: $0 zabbix" | ||
+ | kill -INT $$ | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | db=$1 | ||
+ | |||
+ | current_time=$(date "+%Y.%m.%d-%H.%M.%S") | ||
+ | if [ $db == "zabbix" ]; then | ||
+ | . .env.db1 | ||
+ | elif [ $db == "db1" ]; then | ||
+ | . .env.az.zabbix | ||
+ | elif [ $db == "db2" ]; then | ||
+ | . .env.db2 | ||
+ | else | ||
+ | echo "Unsupported db $db" | ||
+ | echo "Options: zabbix, az-zabbix" | ||
+ | kill -INT $$ | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | pg_dump | gzip > ${current_time}.${db}.pg_dump.gz | ||
+ | echo "S: Completed backing up db $db!" | ||
+ | |||
+ | |||
+ | # psql | ||
+ | # exit 0 | ||
+ | # gunzip -c ${current_time}.az.zabbix.pg_dump.gz | psql zabbix | ||
``` | ``` |
Latest revision as of 00:39, 15 April 2022
.env
export PGPASSWORD=mypass export PGUSER=admin export PGDATABASE=mydb export PGHOST=myhostorip
nohup pg_dump -Fc $DB > $DB-$(date +%d-%m-%y_%H-%M).pgdumpFc pg_restore
https://stackoverflow.com/questions/2732474/restore-a-postgres-backup-file-using-the-command-line
#!/bin/bash set -eu if [ $# -ne 1 ]; then echo "Usage: $0 <db source id" echo "Example: $0 zabbix" kill -INT $$ exit 1 fi db=$1 current_time=$(date "+%Y.%m.%d-%H.%M.%S") if [ $db == "zabbix" ]; then . .env.db1 elif [ $db == "db1" ]; then . .env.az.zabbix elif [ $db == "db2" ]; then . .env.db2 else echo "Unsupported db $db" echo "Options: zabbix, az-zabbix" kill -INT $$ exit 1 fi pg_dump | gzip > ${current_time}.${db}.pg_dump.gz echo "S: Completed backing up db $db!" # psql # exit 0 # gunzip -c ${current_time}.az.zabbix.pg_dump.gz | psql zabbix