Focus on cloud service provider activities
Notes on website operation and maintenance

How to back up a PostgreSQL database Using several common backup commands

Generally, we use more fixed open source CMS programs when building websites, and basically we also use PHP+MYSQL programs, so we use more MYSQL databases on the database. But a few days ago, Lao Zuo met a netizen who used PostgreSQL database. In fact, we have seen this database when using virtual hosts, but it is not used much.

If we need to back up the PostgreSQL database, we can generally export the management gadget that comes with the Web environment of our server directly for backup, but if the Web system is compiled by the operation and maintenance engineer himself, we need to use other separate commands for backup. Here we record how to back up the database through the PostgreSQL command.

First, one click fast backup of single database

su - postgres

Here we log into the database.

pg_dump >

Change the name of our database to the one we need to back up, and then back up. Here we can download the backup data locally.

psql laozuo < dbname.bak

If we need to restore the database, we can use the psql command to restore it. Is it a bit like our MYSQL restore data.

Second, remote backup database

Generally, we don't use many remote backup databases personally. We are honest that the current server operation is quite common, but Lao Zuo also records the method here.

pg_dump -h -p 1234 dbname > dbname.bak

Backup according to the command and port. Note that the database name is changed to our own.

Third, set scheduled automatic backup

This is useful to many of our friends. Generally, the project file is not moved much, and the database is generally increasing or decreasing. So we can back up the database regularly, but we need to do scheduled backup.

1. Log in to the database

su - postgres

As above, we need to log in to the database and set the scheduled task.

2. Create backup directory

mkdir -p ~/dbbackups

We need to create a backup directory.

3. Create Scheduled Task

crontab -e

Then you need to edit the file.

0 0 * * 0 pg_dump -U postgres laozuo> ~/dbbackups/

After editing and saving, we will run a check to see whether the backup is entered into the folder.

Here, after we have backed up the PostgreSQL database, we still need to download it regularly or push SCP to other servers. A friend did backup regularly before, but he still backed up to his current server. So a few days ago, he found that the server failed and the data was lost. He was tragic. So we still need to back up locally.

Domain name host preferential information push QQ group: six hundred and twenty-seven million seven hundred and seventy-five thousand four hundred and seventy-seven Get preferential promotion from merchants.
Like( one )
Do not reprint without permission: Lao Zuo's Notes » How to back up a PostgreSQL database Using several common backup commands

Scan the code to follow the official account

Get more news about webmaster circle!
Entrepreneurship, operation and new knowledge