Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to backup postgresql database automatically on daily basis?

I just want to know how to do backup of postgresql database on daily basis automatically?

Can someone share the complete process with me I am new to postgres and want to learn database backup, i know how to do it manually but is there is any way so that i schedule the database backup process on daily basis.?

like image 420
Sandeep Kumar Avatar asked Oct 23 '25 06:10

Sandeep Kumar


1 Answers

Your can do the following:

Windows version

Create a file backupDB.bat

@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
set BACKUP_FILE=BackupDB_%datestr%.bck
SET PGPASSWORD=<PassWord>
echo on
pg_dump -h <HostName> -p 5432 -U <UserName> -F c -b -v -f %BACKUP_FILE% <DATABASENAME>

and then use Windows Task Scheduler

Linux version

Create a file backupDB.sh

#!/bin/bash
date=$(date '+%Y-%m-%d')
PGPASSWORD="**_PASSWORD_**" pg_dump --host 127.0.0.1 --port 5432 -U **_USERNAME_** --format custom --blobs --verbose --file "DB_backup_$date.bck" **_DBNAME_**

To run this every day - lets say at 1:00 - you can use cron

crontab -e

contab entry:

0 1 * * *   ./backupDB.sh

Hope that helps

like image 94
Sebastian Hildebrandt Avatar answered Oct 24 '25 19:10

Sebastian Hildebrandt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!