• Home
  • Services
    • DBA Support
    • DBA Consultancy Services
    • PostgreSQL Support
    • Website Maintenance
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum Database

    Greenplum Database

    $2,000.00 $1,500.00
    Read More
  • Company
    • FAQs
    • About Us
    • Contact
  • Events
  • Portfolio
  • Blogs
    • Blog – RayaFeeL
    • Blog – PostgreSQL Support
    • Blog – PostgreSQL Migration
    • Blog – All DB’s
    • Blog – Linux
    • Blog – Medical Coding
      • Cart

        0

    Have any question?
    (+91)8838953252
    ITsupport@rayafeel.com
    RegisterLogin
    RayaFeeL
    • Home
    • Services
      • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
      • Website Maintenance
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00
      Read More
    • Company
      • FAQs
      • About Us
      • Contact
    • Events
    • Portfolio
    • Blogs
      • Blog – RayaFeeL
      • Blog – PostgreSQL Support
      • Blog – PostgreSQL Migration
      • Blog – All DB’s
      • Blog – Linux
      • Blog – Medical Coding
        • Cart

          0

      Blog

      • Home
      • Blog
      • Blog
      • Taking PostgreSQL Basebackup script

      Taking PostgreSQL Basebackup script

      • Posted by 2ndnijam
      • Categories Blog
      • Date January 10, 2019
      • Comments 0 comment

      this script will run at every night 1 AM and will take the postgresql basebackup of local tablespace

      # crontab -e
      0 1 * * * sh /home/postgres/eds/pune.sh
      :wq
      
      #cat /home/postgres/eds/pune.sh
      # Config:
      DB="POSTGRES_DB_NAME"
      U="USERNAME"
      export PGPASSWORD="tcs@12"
      export PGPORT="5444"
      export PGHOST="10.0.4.101"
      
      BASEBACKUP_LOG=/var/log/postgresql/basebackup.log
      [ -f $BASEBACKUP_LOG ] && mv -f $BASEBACKUP_LOG $BASEBACKUP_LOG.old
      
      BACKUP_PATH=/path/to/backup/$(date +%F)
      /opt/edb/as9.6/bin/pg_basebackup -D $BACKUP_PATH -Ft -z -v 2>&1 | ts '%F %T %Z' &> $BASEBACKUP_LOG

      To create a backup of a local database where the tablespace in /opt/ts is relocated to ./backup/ts:

      # crontab -e
      0 1 * * * sh /home/postgres/eds/pune.sh
      :wq
      
      #cat /home/postgres/eds/pune.sh
      # Config:
      DB="POSTGRES_DB_NAME"
      U="USERNAME"
      export PGPASSWORD="tcs@12"
      export PGPORT="5444"
      export PGHOST="10.0.4.101"
      /opt/edb/as9.6/bin/pg_basebackup -D /backup/data -T /opt/ts=/home/backup/ts

      PostgreSQL Full backup and incremental backup script:

      cat /home/script/Postgres_backup.sh
      #!/bin/bash
      
      PGDATA=/data/
      PGHOME=/opt/PostgreSQL/9.3/bin
      HOT=/backup
      WAL_LOC=/archive/
      LOG=/home/script/script_logs
      TODAY=$(date +"%m-%d-%Y")
      
      /opt/PostgreSQL/9.3/bin/psql -X -U postgres -h localhost -c 'select now() as "Online Backup Start Time";' >> $LOG/backupinfo_$TODAY.log
      
      /opt/PostgreSQL/9.3/bin/psql -X -U postgres -h localhost -c  "select setting from pg_settings where name='data_directory'; " >> $LOG/backupinfo_$TODAY.log
      
      #psql -U postgres -c " select pg_switch_xlog(); " >> $LOG/backupinfo_$TODAY.log
      
      ## Adding Checkpoint for consistent backup
      
      echo "CHECKPOINT;
      SELECT pg_start_backup('Full Backup');
      -- Making Sure all in WAL is archived after checkpoint
      
      select pg_switch_xlog();"|/opt/PostgreSQL/9.3/bin/psql -X -U postgres -h localhost >> $LOG/backupinfo_$TODAY.log
      
      tar -cvzf $HOT/onlinebackup_$TODAY.tar.gz $PGDATA/* >> $LOG/backupinfo_$TODAY.log
      
      tar --remove-files -cvzf $HOT/arch_$TODAY.tar $WAL_LOC/* >> $LOG/backupinfo_$TODAY.log
      
      /opt/PostgreSQL/9.3/bin/psql -X -U postgres  -h localhost -c " select pg_stop_backup(); " >> $LOG/backupinfo_$TODAY.log
      
      ## Archiving all transaction in Mid of Backup
      
      /opt/PostgreSQL/9.3/bin/psql -X -U postgres -h localhost -c "select pg_switch_xlog();" >> $LOG/backupinfo_$TODAY.log
      
      /opt/PostgreSQL/9.3/bin/psql -X -U postgres -h localhost -c 'select now() as "Online Backup End Time";' >> $LOG/backupinfo_$TODAY.log
      
      # Online Backup file Retention policy will be 2 day's
      
      find /backup -name "onlinebackup_*.tar.gz" -mtime +2 -exec ls -l {} \; >> $LOG/backupinfo_$TODAY.log
      
      find /backup -name "onlinebackup_*.tar.gz" -mtime +2 -exec rm -r {} \;
      
      # Archive Backup file Retention policy will be 2 day's
      
      find /backup -name "arch_*.tar" -mtime +2 -exec ls -l {} \; >> $LOG/backupinfo_$TODAY.log
      
      find /backup -name "arch_*.tar" -mtime +2 -exec rm -r {} \;
      
      #mail -s " `hostname` :: Fullbackup Information" rdba-alerts@enterprisedb.com < $LOG/backupinfo_$TODAY.log
      exit 0

      • Share:
      2ndnijam

      Previous post

      How can I take only PostgreSQL functions & procedure backup
      January 10, 2019

      Next post

      postgreSQL restoration simple 4 steps
      January 11, 2019

      Leave A Reply Cancel reply

      You must be logged in to post a comment.

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft


      Search

      ADVERTISEMENT

      Latest Posts

      PostgreSQL Patching version 9, 10,11
      10Oct2019
      Tools for PostgreSQL
      16Sep2019
      Postgres user creation and restrict DDL & database access
      13Sep2019
      PostgreSQL SSL Setup
      07Sep2019
      How to DELETE current XLOG / WAL LOG in postgresql database ?
      19Aug2019

      Latest Courses

      PostgreSQL Database

      PostgreSQL Database

      $600.00 $500.00
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00

      Preview Course

      Free

      Recent Forum Topics

      • thought behind whiteboard activity
      • Are you going to take your first ste
      • How to start working on an application?
      • please let me know pre requirements to increase work_mem
      • how to copy some data in one table to another table in postgres

      2ndquadrant.in

      (+91) 8838953252

      ITsupport@rayafeel.com

      Company

      • About Us
      • Contact
      • Our Team
      • Blog

      COURSES

      • List Of Course
      • Become An Instructor
      • Events
      • Postgres Support Blog

      Support

      • DBA Support
      • Consultancy Services
      • Postgres Migration Blogs
      • Forum

      Recommend

      • Groups
      • Login
      • FAQs
      • SignUp

      IT Services by rayafeel.com. Powered by Rayafeel Technologies Pvt Ltd.

      • Privacy
      • Terms

      Become An Instructor?

      Join thousand of instructors and earn money hassle free!

      Get Started Now

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now