It provides a script to backup databases in PostgreSQL cluster structure in this repository.
- First, edit the script file.
nano script.sh
- You need to set the variables in the script file according to yourself. First, set the IP address of the backup server.
sftp_ip=10.10.10.xx
- Enter the name of the user to connect to the backup server
sftp_user=root
- Enter the port to make the SFTP connection
sftp_port=22
- backups need to be taken in the local directory first. Specify the local directory where backups will be taken.
local_backup_directory=/my/backupdir
- Where to back up files on the SFTP server. Attention! The contents of this specified directory should be reserved for backup files only. There should be no directories such as /root /etc. rm -rf is used when deleting historical files.
sftp_backup_directory=/root/backup
- Enter the IP number in the last digit of your KeepAlived IP address. For example, if your keepalived IP address is 10.10.10.10, you should enter 10.
keepalivedip=xx
- How many days the backups will be maintained on the SFTP server.
backup_time=5
- Backups are sent encrypted. That's why you need a password to be able to open backups again. Passwords are encrypted with GPG.
file_password=password
To be able to turn the backups back on, use the command below
gpg --output test.sql.gz --decrypt test.sql.gz.gpg
- ssh-key must be set for machines to access each other. This is how the backup is sent. ssh-key should be copied from all postgresql servers to SFTP server.
ssh-keygen
ssh-copy-id -f user@hostname
After all these settings, install this script file on all your postgresql cluster machines and create a cron for this file on each machine.
For example, let's say I run a cron at 1 o'clock every night. At 1 o'clock all the script files will run and the script file will check the IP address of each machine. The machine with the KeepAlived IP will run the script.
After the backup is received in the local directory and sent to the SFTP server, the backups will be deleted from the local directory.
In addition, historical backups will be kept on the SFTP server for the specified date.