# Automated Backup on Windows

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

## Automated Backup on Windows

Below are 2 set of instructions to automate backing up of PostgreSQL Server in Windows Environment.

• The First method uses pg_dump.exe along with batch file to call it. This batch file will create a new file for each day it is run.
• The Second method backups uses PgPass, pg_dumpall.exe, and a batch file to backup the entire server and over write the file every time it is run.

## Using pgdump, new file for each day

• Go to Server create a Directory called Drive:\PostgresqlBack then create a sub directory called "bin" in the Drive:\PostgresqlBack
• Instead of compiling pg_dump.exe the pgAdmin III install has the pg_dump ready to go. The following files are required when using pg_dump.exe from pgAdmin III install, these files must be located in the bin folder along side pg_dump.exe
 comerr32.dll gssapi32.dll k5sprt32.dll krb_32.dll libeay32.dll libiconv2.dll libpq.dll Microsoft.VC80.CRT.manifest msvcm80.dll msvcp80.dll msvcr80.dll pg_dump.dll ssleay32.dll zlib1.dll 
• Create batch file called something, example is postgresqlBackup.bat. The file must be located in PostgresqlBack directory not the bin folder.
• Open the File then Copy/Paste the following
 @echo off for /f "tokens=1-4 delims=/ " %%i in ("%date%") do ( set dow=%%i set month=%%j set day=%%k set year=%%l ) set datestr=%month%_%day%_%year% echo datestr is %datestr% set BACKUP_FILE=<NameOfTheFile>_%datestr%.backup echo backup file name is %BACKUP_FILE% SET PGPASSWORD=<PassWord> echo on bin\pg_dump -i -h <HostName> -p 5432 -U <UserName> -F c -b -v -f %BACKUP_FILE% <DATABASENAME> 

• Change <NameOfTheFile> to something. One idea is use the name of the database. (make sure there are no spaces after the word BACKUP_FILE any spaces will cause this setting not to work.) Setting is the first part of the file name then followed by the date the file was created with the extension .backup
• Change the <PassWord > setting above to the correct password for the backup users. (make sure there is no spaces after the word PGPASSWORD any spaces will cause this setting not to work. Description of pgPassword
• Change <HostName> either to ip address or dns name of the server hosting Postgresql.
• Change <UserName> to backup user make sure this users has access to database for backup purposes
• Change <DATABASENAME> to the database name being backed up.
• Save the File
• Once you have chosen the security context the Task in going to run in, it is advised to change the directory security where the backup is run and the files are stored, as a high level user name and password are stored in plain text.

## Using .pgpass and pgdumpall, same file

To achieve an automated backup in a windows environment, I did the following.

(I called mine pgpass.conf) and put it somewhere secure. I have it in a subdirectory under the script that runs the backup.
• Lock down the .pgpass file
Using NTFS permissions, disable access to this file for everybody except the user pg is running as
(If you're running pg under the system account, then you should set it to use it's own user credentials)
• Create a script to call pg_dumpall
Mine looks like this:
SET PGPASSFILE=C:\foo\bar\PG_BACKUP\PGPASSFILE\pgpass.conf "C:\Program Files\PostgreSQL\8.2\bin\pg_dumpall.exe" -U scfcu_postgres > C:\foo\bar\PG_BACKUP\db.out current 
Important to note is the first line, that dynamically sets the PGPASSFILE environment variable. This releases after the script is finished, so that only this process for only the time it's running has automatic login. The second line is a very basic pg_dumpall command, with a username passed (-U) and the output filename (db.out) as well as the database to backup (in this case 'current')