Skip to content

Commit 5c21286

Browse files
add backup playbooks.md
1 parent f9857e7 commit 5c21286

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ https://habr.com/ru/companies/softpoint/articles/795305/ - Бэкап, бэка
1919
git add .
2020
git commit -m "add utils.md"
2121
git push -u origin main
22-
```
22+
```
23+
24+
25+
26+
# Плейбук Ansible для резервного копирования MongoDB и PostgreSQL
27+
/Users/andreyshabunov/PhpstormProjects/db-backups/backup_databases.yml

backup_mongo.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
- name: Backup MongoDB Database
3+
hosts: serverspace_host
4+
become: yes # Запуск от имени суперпользователя
5+
tasks:
6+
- name: Create backup directory
7+
file:
8+
path: /backups
9+
state: directory
10+
mode: '0755'
11+
12+
# Backup MongoDB
13+
- name: Backup MongoDB
14+
command: >
15+
mongodump --out /backups/mongodb_backup_{{ ansible_date_time.date }}_{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}
16+
register: mongo_backup
17+
ignore_errors: true
18+
19+
- debug:
20+
var: mongo_backup
21+
22+
# Optionally, if you want to transfer backups to your local machine, you can use rsync or scp:
23+
# Uncomment the lines below to transfer backups
24+
# - name: Copy MongoDB Backup to Local
25+
# fetch:
26+
# src: /backups/
27+
# dest: /path/to/local/backup/mongodb/
28+
# flat: yes

backup_postgre.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
- name: Backup PostgreSQL Database
3+
hosts: serverspace_host_postgres
4+
become: yes # Запуск от имени суперпользователя
5+
tasks:
6+
- name: Create backup directory
7+
file:
8+
path: /backups
9+
state: directory
10+
mode: '0755'
11+
12+
# Backup PostgreSQL
13+
- name: Backup PostgreSQL
14+
command: >
15+
pg_dumpall --file=/backups/postgresql_backup_{{ ansible_date_time.date }}_{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}
16+
register: postgres_backup
17+
ignore_errors: true
18+
19+
- debug:
20+
var: postgres_backup
21+
22+
# - name: Copy PostgreSQL Backup to Local
23+
# fetch:
24+
# src: /backups/
25+
# dest: /path/to/local/backup/postgresql/
26+
# flat: yes

hosts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Не работают оба пользователя одновременно, но раздельно работают корректно
2+
[servers:children]
3+
serverspace_host
4+
serverspace_host_postgres
5+
6+
7+
[serverspace_host]
8+
#78.140.240.22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
9+
#93.183.74.73 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10+
#78.140.241.18 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
11+
#78.140.243.13 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
12+
78.140.240.41 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
13+
14+
[serverspace_host_postgres]
15+
#93.183.74.73 ansible_ssh_user=postgres ansible_ssh_pass=your_postgres_password
16+
#78.140.241.18 ansible_ssh_user=postgres ansible_ssh_pass=your_postgres_password
17+
#78.140.243.13 ansible_ssh_user=postgres ansible_ssh_pass=your_postgres_password
18+
78.140.240.41 ansible_ssh_user=postgres ansible_ssh_pass=your_postgres_password

0 commit comments

Comments
 (0)