Skip to content

Simple bash script for postgresql databases backup through cron

License

Notifications You must be signed in to change notification settings

itarient/cron.pg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cron.pg

Simple bash script for postgresql databases backup through a cron daemon.

Installation

  1. Login under postgres system user and create some folder in postgre' home directory
root:~# su -l postgres`
postgres:~# mkdir cron.pg.d
  1. copy backup.sh into cron.pg.d

postgres:~# cp backup.sh cron.pg.d/

  1. make your jobs
postgres:~# vim my_job1.sh
postgres:~# vim my_jon2.sh
...

Use the following variables

  • JOB_DIR - destination storage for your backups. backup.sh will form the following file structure inside it:
JOB_DIR
|- dbname1
|  |- dbname1-YYYY-mm-d1.gz
|  |- dbname1-YYYY-mm-d1.log
|  |- dbname1-YYYY-mm-d2.gz
|  |- dbname1-YYYY-mm-d2.log
|  ...
|- dbname2
|  |- dbname2-YYYY-mm-d1.gz
|  |- dbname2-YYYY-mm-d1.log
|  |- dbname2-YYYY-mm-d2.gz
|  |- dbname2-YYYY-mm-d2.log
|  ...
...
  • JOB_DBLIST - space-separated database names to be backuped
  • JOB_KEEP - keep this number of backup files

See an examples.

  1. add your jobs to the crontab
postgres:~# crontab -e
0 4     * * 6   cd /var/lib/postgresql/cron.pg.d && ./job_weekly_keep4.sh
0 4     * * *   cd /var/lib/postgresql/cron.pg.d && ./job_daily_keep20.sh
  1. Check your authorization rules in your pg_hba.conf. You need local peer authorization rule if you don't want specify login and passowrds inside your jobs. If you use auxiliary user to make backups, change backup.sh, and add -U parameter to the pg_dump command line. Also use ~/.pgpass file for storing user password. This is MUST be done under different non-privileged system user, NOT postgres, and NOT root!!!*
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
  1. !!! CHECK YOUR JOBS ARE MADE !!!

  2. !!! !!! REGULARLY CHECK YOUR BACKUPS ARE RESTORABLE !!! !!!

About

Simple bash script for postgresql databases backup through cron

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages