Skip to content

Instantly share code, notes, and snippets.

@mudream4869
Last active April 17, 2022 09:41
Show Gist options
  • Save mudream4869/a82508e84841d50a226932b26edcb737 to your computer and use it in GitHub Desktop.
Save mudream4869/a82508e84841d50a226932b26edcb737 to your computer and use it in GitHub Desktop.
Minecraft world backup script
#!/usr/bin/ansible-playbook
- hosts: localhost
vars:
git_repo_path: /home/minecraft/backup/season2
minecraft_worlds_paths:
- /home/minecraft/srv1.18/world
- /home/minecraft/srv1.18/world_nether
- /home/minecraft/srv1.18/world_the_end
git_key: ''
skip_git_push: false
tasks:
- name: Remove old data
ansible.builtin.file:
path: '{{ git_repo_path }}/data'
state: absent
- name: Prepare new folder
ansible.builtin.file:
path: '{{ git_repo_path }}/data'
state: directory
- name: Copy world to git repository
ansible.builtin.copy:
src: '{{ item }}'
dest: '{{ git_repo_path }}/data'
with_items: '{{ minecraft_worlds_paths }}'
- name: Git add and commit
shell: git add . && git commit -m "backup"
args: {chdir: '{{ git_repo_path }}'}
ignore_errors: true
- name: Get commit count
shell: git rev-list --count HEAD
args: {chdir: '{{ git_repo_path }}'}
register: git_commit_count
- name: Remove old commit and preserve latest 3 commit
when: git_commit_count.stdout|int >= 10
block:
# - name: Go to prev 3 commit
# shell: git checkout HEAD~~~
# args: {chdir: '{{ git_repo_path }}'}
- name: Squash old 4 commits
shell: |
git reset --soft HEAD~~~~ && \
git commit -m "remove old commit"
args: {chdir: '{{ git_repo_path }}'}
# - name: Get commit short hash
# shell: git rev-parse --short HEAD
# args: {chdir: '{{ git_repo_path }}'}
# register: git_new_root
# - name: Rebase master by squashed commit
# shell: |
# git checkout master && \
# git rebase {{ git_new_root.stdout }}
# args: {chdir: '{{ git_repo_path }}'}
- name: Remove detached commits
shell: git gc
args: {chdir: '{{ git_repo_path }}'}
- name: Git push FORCE with key
shell: git push -f origin master
args: {chdir: '{{ git_repo_path }}'}
environment:
GIT_SSH_COMMAND: |
ssh -i '{{ git_key }}' -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*
when: not skip_git_push and git_key == ''
- name: Git push FORCE with no key
shell: git push -f origin master
args: {chdir: '{{ git_repo_path }}'}
when: not skip_git_push and git_key != ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment