added opening of ports, created an ansible playbook for backup and integrated into Jenkens pipeline

This commit is contained in:
dima 2024-10-20 13:31:40 +03:00
parent 803c80efff
commit 67166656f3
3 changed files with 152 additions and 97 deletions

37
backup_postgresql.yml Normal file
View File

@ -0,0 +1,37 @@
---
- name: Backup PostgreSQL and set up cron job
hosts: postgres_servers
become: true
vars:
backup_dir: "/var/backups/postgresql"
tasks:
- name: Ensure backup directory exists
file:
path: "{{ backup_dir }}"
state: directory
owner: postgres
group: postgres
mode: '0755'
- name: Perform PostgreSQL database backup
become_user: postgres
command: >
pg_dump -U {{ postgres_user }}
-F c
-f "{{ backup_dir }}/db_backup_{{ postgres_db }}_{{ ansible_date_time.iso8601 }}.sql"
{{ postgres_db }}
environment:
PGPASSWORD: "{{ postgres_password }}"
- name: Create cron job for hourly PostgreSQL backup
cron:
name: "PostgreSQL hourly backup"
user: postgres
minute: "0"
hour: "*"
job: 'pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/db_backup_{{ postgres_db }}_$(date +\%F-\%H-\%M).sql'
environment:
PGPASSWORD: "{{ postgres_password }}"

View File

@ -1,11 +1,12 @@
---
- name: Install PostgreSQL on openSUSE Leap
- name: Install PostgreSQL on openSUSE Leap
hosts: postgres_servers
become: yes
vars:
postgres_user: "{{ postgres_user }}"
postgres_password: "{{ postgres_password }}"
postgres_db: "{{ postgres_db }}"
tasks:
- name: Update zypper
@ -69,19 +70,19 @@
encoding: UTF8
state: present
- name: Create table "contacts"
community.postgresql.postgresql_query:
db: "{{ postgres_db }}"
query: |
CREATE TABLE IF NOT EXISTS contacts (
имя VARCHAR(100),
телефон VARCHAR(20)
);
login_user: "{{ postgres_user }}"
login_password: "{{ postgres_password }}"
- name: Открыть порт PostgreSQL 5432 в файрволе
command: firewall-cmd --add-port=5432/tcp --permanent
become: yes
- name: Перезагрузить файрвол для применения изменений
command: firewall-cmd --reload
become: yes
handlers:
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted

View File

@ -2,9 +2,10 @@ pipeline {
agent any
parameters {
string(name: 'TARGET_IP', defaultValue: '192.168.0.72', description: 'IP адрес машины для установки PostgreSQL')
string(name: 'DB_USER', defaultValue: 'myuser', description: 'Имя пользователя для базы данных PostgreSQL')
string(name: 'DB_NAME', defaultValue: 'mydatabase', description: 'Имя базы данных PostgreSQL')
string(name: 'TARGET_IP', defaultValue: '192.168.0.72')
string(name: 'DB_USER', defaultValue: 'myuser')
string(name: 'DB_NAME', defaultValue: 'mydatabase')
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql')
}
environment {
@ -12,10 +13,9 @@ pipeline {
}
stages {
stage('Run Ansible Playbook') {
stage('Install PostgreSQL') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD'),
usernamePassword(credentialsId: 'sqlpass', usernameVariable: 'SQL_USER', passwordVariable: 'SQL_PASSWORD')]) {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
writeFile file: '/ansiblesql/hosts.ini', text: """
@ -25,14 +25,31 @@ pipeline {
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \
-e postgres_user=${params.DB_USER} \
-e postgres_password=${SQL_PASSWORD} \
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME}
"""
}
}
}
}
stage('Setup PostgreSQL Backup with Cron') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/backup_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME} \\
-e backup_dir=${params.BACKUP_DIR}
"""
}
}
}
}
}
}