-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.yml
114 lines (97 loc) · 3.41 KB
/
backup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
- name: Backup dot files and configs to GitHub
hosts: localhost
gather_facts: true
vars:
cur_home: "{{ ansible_play_hosts[0] }}"
git_name: "{{ ansible_play_hosts[1] }}"
git_email: "{{ ansible_play_hosts[2] }}"
backup_repo: "{{ ansible_play_hosts[3] }}"
test: "{{ ansible_env.HOME }}"
required_args:
- git_name
- git_email
- backup_repo
tasks:
#- name: Check if required args are set
# assert:
# that: item is defined
# fail_msg: "{{ item }} is not set. Please provide a value for {{ item }}"
# with_items:
# - git_name
# - git_email
# - backup_repo
- name: Debug HOME environment variable
debug:
var: cur_home
- name: Read GitHub Token from ~/.github_token file
slurp:
src: "{{ cur_home }}/.github_token"
register: github_token_file
- name: Set GitHub Token
set_fact:
github_token: "{{ github_token_file.content | b64decode | regex_replace('\\s+$', '') }}"
- name: Ensure /tmp/backups directory exists
file:
path: /tmp/backups
state: directory
- name: Check if /tmp/backups directory is already added to Git config
shell: git config --get-all safe.directory | grep -q '/tmp/backups'
register: safe_directory_exists
ignore_errors: yes
- name: Add exception for /tmp/backups directory in Git config if not already added
command: git config --global --add safe.directory /tmp/backups
when: not safe_directory_exists.rc == 0
- name: Clone the GitHub repository
git:
repo: "https://{{ github_token }}@github.com/{{ backup_repo }}.git"
dest: /tmp/backups
clone: yes
update: yes
force: yes
- name: Backup dot files and configs
find:
hidden: true
paths:
- "{{ ansible_env.HOME }}"
patterns:
- '.*'
- '*.conf'
- '*.cfg'
- '*.ini'
file_type: file
register: backup_files
- name: Copy backup files to local directory
copy:
src: "{{ item.path }}"
dest: /tmp/backups/{{ item.path | basename }}
with_items: "{{ backup_files.files }}"
- name: Check if .backer_add file exists
stat:
path: "{{ cur_home}}/.backer_add"
register: backer_add_file
- name: Set .backer_add variable
set_fact:
backer_add: "{{ '--files-from=' + cur_home + '/.backer_add' if backer_add_file.stat.exists else '' }}"
- name: Sync dot files and configs
command: rsync -av --delete --copy-links --exclude=.git/ {{ backer_add }} --include='.*' --include='*.conf' --include='*.cfg' --include='*.ini' "{{ ansible_env.HOME }}/" /tmp/backups/
- name: Check for changes
command: git status --porcelain
args:
chdir: /tmp/backups
register: git_status
- name: Set Git user name and email for the repository
command: git config user.name "{{ git_name }}"
args:
chdir: /tmp/backups
- name: Set Git user email for the repository
command: git config user.email "{{ git_email }}"
args:
chdir: /tmp/backups
- name: Commit and push backup files to GitHub if changes
shell: |
cd /tmp/backups
git add .
[ "$(git status --porcelain | wc -l)" -eq 0 ] || (git commit -m "Backup $(date --iso-8601=seconds)" && git push)
register: mygit
failed_when: mygit.rc != 0