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,87 +1,88 @@
|
|||||||
---
|
---
|
||||||
- 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
|
|
||||||
command: zypper refresh
|
- name: Update zypper
|
||||||
register: zypper_refresh
|
command: zypper refresh
|
||||||
changed_when: "'Refreshing' in zypper_refresh.stdout"
|
register: zypper_refresh
|
||||||
|
changed_when: "'Refreshing' in zypper_refresh.stdout"
|
||||||
- name: Update System
|
|
||||||
zypper:
|
- name: Update System
|
||||||
name: '*'
|
zypper:
|
||||||
state: latest
|
name: '*'
|
||||||
when: zypper_refresh.changed
|
state: latest
|
||||||
|
when: zypper_refresh.changed
|
||||||
- name: Update PostgreSQL package
|
|
||||||
zypper:
|
- name: Update PostgreSQL package
|
||||||
name:
|
zypper:
|
||||||
- postgresql-server
|
name:
|
||||||
- postgresql-contrib
|
- postgresql-server
|
||||||
state: present
|
- postgresql-contrib
|
||||||
|
state: present
|
||||||
- name: PostgreSQL initdb
|
|
||||||
command: sudo -u postgres initdb -D /var/lib/pgsql/data
|
- name: PostgreSQL initdb
|
||||||
args:
|
command: sudo -u postgres initdb -D /var/lib/pgsql/data
|
||||||
creates: /var/lib/pgsql/data/PG_VERSION
|
args:
|
||||||
|
creates: /var/lib/pgsql/data/PG_VERSION
|
||||||
- name: Systemctl start and enable PostgreSQL
|
|
||||||
service:
|
- name: Systemctl start and enable PostgreSQL
|
||||||
name: postgresql
|
service:
|
||||||
state: started
|
name: postgresql
|
||||||
enabled: yes
|
state: started
|
||||||
|
enabled: yes
|
||||||
- name: python3-psycopg2 install
|
|
||||||
zypper:
|
- name: python3-psycopg2 install
|
||||||
name: python3-psycopg2
|
zypper:
|
||||||
state: present
|
name: python3-psycopg2
|
||||||
|
state: present
|
||||||
- name: Change listen_addresses in postgresql.conf
|
|
||||||
lineinfile:
|
- name: Change listen_addresses in postgresql.conf
|
||||||
path: /var/lib/pgsql/data/postgresql.conf
|
lineinfile:
|
||||||
regexp: '^#?listen_addresses\s*='
|
path: /var/lib/pgsql/data/postgresql.conf
|
||||||
line: "listen_addresses = '*'"
|
regexp: '^#?listen_addresses\s*='
|
||||||
notify: Restart PostgreSQL
|
line: "listen_addresses = '*'"
|
||||||
|
notify: Restart PostgreSQL
|
||||||
- name: Change pg_hba.conf
|
|
||||||
lineinfile:
|
- name: Change pg_hba.conf
|
||||||
path: /var/lib/pgsql/data/pg_hba.conf
|
lineinfile:
|
||||||
regexp: '^host\s+all\s+all\s+0\.0\.0\.0/0\s+md5'
|
path: /var/lib/pgsql/data/pg_hba.conf
|
||||||
line: "host all all 0.0.0.0/0 md5"
|
regexp: '^host\s+all\s+all\s+0\.0\.0\.0/0\s+md5'
|
||||||
notify: Restart PostgreSQL
|
line: "host all all 0.0.0.0/0 md5"
|
||||||
|
notify: Restart PostgreSQL
|
||||||
- name: Create User PostgreSQL
|
|
||||||
community.postgresql.postgresql_user:
|
- name: Create User PostgreSQL
|
||||||
name: "{{ postgres_user }}"
|
community.postgresql.postgresql_user:
|
||||||
password: "{{ postgres_password }}"
|
name: "{{ postgres_user }}"
|
||||||
state: present
|
password: "{{ postgres_password }}"
|
||||||
|
state: present
|
||||||
- name: Create Base PostgreSQL
|
|
||||||
community.postgresql.postgresql_db:
|
- name: Create Base PostgreSQL
|
||||||
name: "{{ postgres_db }}"
|
community.postgresql.postgresql_db:
|
||||||
owner: "{{ postgres_user }}"
|
name: "{{ postgres_db }}"
|
||||||
encoding: UTF8
|
owner: "{{ postgres_user }}"
|
||||||
state: present
|
encoding: UTF8
|
||||||
|
state: present
|
||||||
- name: Create table "contacts"
|
|
||||||
community.postgresql.postgresql_query:
|
|
||||||
db: "{{ postgres_db }}"
|
- name: Открыть порт PostgreSQL 5432 в файрволе
|
||||||
query: |
|
command: firewall-cmd --add-port=5432/tcp --permanent
|
||||||
CREATE TABLE IF NOT EXISTS contacts (
|
become: yes
|
||||||
имя VARCHAR(100),
|
|
||||||
телефон VARCHAR(20)
|
|
||||||
);
|
- name: Перезагрузить файрвол для применения изменений
|
||||||
login_user: "{{ postgres_user }}"
|
command: firewall-cmd --reload
|
||||||
login_password: "{{ postgres_password }}"
|
become: yes
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
- name: Restart PostgreSQL
|
- name: Restart PostgreSQL
|
||||||
service:
|
service:
|
||||||
name: postgresql
|
name: postgresql
|
||||||
state: restarted
|
state: restarted
|
||||||
|
|
@ -2,37 +2,54 @@ 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 {
|
||||||
ANSIBLE_HOST_KEY_CHECKING = 'False'
|
ANSIBLE_HOST_KEY_CHECKING = 'False'
|
||||||
}
|
}
|
||||||
|
|
||||||
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: """
|
||||||
[postgres_servers]
|
[postgres_servers]
|
||||||
${params.TARGET_IP} ansible_user=${SSH_USER} ansible_ssh_pass=${POSTGRES_PASSWORD} ansible_connection=ssh
|
${params.TARGET_IP} ansible_user=${SSH_USER} ansible_ssh_pass=${POSTGRES_PASSWORD} ansible_connection=ssh
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
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