added opening of ports, created an ansible playbook for backup and integrated into Jenkens pipeline
This commit is contained in:
parent
803c80efff
commit
67166656f3
37
backup_postgresql.yml
Normal file
37
backup_postgresql.yml
Normal 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 }}"
|
||||||
|
|
@ -1,11 +1,12 @@
|
|||||||
---
|
---
|
||||||
- name: Install PostgreSQL on openSUSE Leap
|
- name: Install PostgreSQL on openSUSE Leap
|
||||||
hosts: postgres_servers
|
hosts: postgres_servers
|
||||||
become: yes
|
become: yes
|
||||||
vars:
|
vars:
|
||||||
postgres_user: "{{ postgres_user }}"
|
postgres_user: "{{ postgres_user }}"
|
||||||
postgres_password: "{{ postgres_password }}"
|
postgres_password: "{{ postgres_password }}"
|
||||||
postgres_db: "{{ postgres_db }}"
|
postgres_db: "{{ postgres_db }}"
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
|
|
||||||
- name: Update zypper
|
- name: Update zypper
|
||||||
@ -69,19 +70,19 @@
|
|||||||
encoding: UTF8
|
encoding: UTF8
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
- name: Create table "contacts"
|
|
||||||
community.postgresql.postgresql_query:
|
- name: Открыть порт PostgreSQL 5432 в файрволе
|
||||||
db: "{{ postgres_db }}"
|
command: firewall-cmd --add-port=5432/tcp --permanent
|
||||||
query: |
|
become: yes
|
||||||
CREATE TABLE IF NOT EXISTS contacts (
|
|
||||||
имя VARCHAR(100),
|
|
||||||
телефон VARCHAR(20)
|
- name: Перезагрузить файрвол для применения изменений
|
||||||
);
|
command: firewall-cmd --reload
|
||||||
login_user: "{{ postgres_user }}"
|
become: yes
|
||||||
login_password: "{{ postgres_password }}"
|
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
- name: Restart PostgreSQL
|
- name: Restart PostgreSQL
|
||||||
service:
|
service:
|
||||||
name: postgresql
|
name: postgresql
|
||||||
state: restarted
|
state: restarted
|
||||||
|
|
@ -2,9 +2,10 @@ pipeline {
|
|||||||
agent any
|
agent any
|
||||||
|
|
||||||
parameters {
|
parameters {
|
||||||
string(name: 'TARGET_IP', defaultValue: '192.168.0.72', description: 'IP адрес машины для установки PostgreSQL')
|
string(name: 'TARGET_IP', defaultValue: '192.168.0.72')
|
||||||
string(name: 'DB_USER', defaultValue: 'myuser', description: 'Имя пользователя для базы данных PostgreSQL')
|
string(name: 'DB_USER', defaultValue: 'myuser')
|
||||||
string(name: 'DB_NAME', defaultValue: 'mydatabase', description: 'Имя базы данных PostgreSQL')
|
string(name: 'DB_NAME', defaultValue: 'mydatabase')
|
||||||
|
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql')
|
||||||
}
|
}
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
@ -12,10 +13,9 @@ pipeline {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Run Ansible Playbook') {
|
stage('Install PostgreSQL') {
|
||||||
steps {
|
steps {
|
||||||
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD'),
|
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
|
||||||
usernamePassword(credentialsId: 'sqlpass', usernameVariable: 'SQL_USER', passwordVariable: 'SQL_PASSWORD')]) {
|
|
||||||
script {
|
script {
|
||||||
|
|
||||||
writeFile file: '/ansiblesql/hosts.ini', text: """
|
writeFile file: '/ansiblesql/hosts.ini', text: """
|
||||||
@ -25,14 +25,31 @@ pipeline {
|
|||||||
|
|
||||||
|
|
||||||
sh """
|
sh """
|
||||||
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \
|
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\
|
||||||
-e postgres_user=${params.DB_USER} \
|
-e postgres_user=${params.DB_USER} \\
|
||||||
-e postgres_password=${SQL_PASSWORD} \
|
-e postgres_password=${POSTGRES_PASSWORD} \\
|
||||||
-e postgres_db=${params.DB_NAME}
|
-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}
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user