How Do I Backup My PostgreSQL Database with Cron?

后端 未结 8 1253
孤街浪徒
孤街浪徒 2021-01-30 09:31

I can run commands like vacuumdb, pg_dump, and psql just fine in a script if I preface them like so:

/usr/bin/sudo -u postgres /usr/bin/pg_dump -Fc mydatabase &g         


        
8条回答
  •  情深已故
    2021-01-30 10:12

    Another version to get the list of databases:
    psql -lqt | grep -vE '^ +(template[0-9]+|postgres)? *\|' | cut -d'|' -f1| sed -e 's/ //g' -e '/^$/d'

    As my psql -lqt output is:

     abcdefghij         | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     postgres           | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0          | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                        |          |          |             |             | postgres=CTc/postgres
     template1          | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                        |          |          |             |             | postgres=CTc/postgres
     abc                | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
    

提交回复
热议问题