Ubuntu Data Backup Automation


  • name: Ubuntu Data Backup Automation
    hosts: from_server
    become: yes
    vars:
    source_dir: “/path/to/source” # Change to the actual folder to back up
    dest_dir: “/path/to/destination” # Change to the actual backup location on TO server
    to_server: “to_server_user@to_server_ip”
    ssh_key: “~/.ssh/id_rsa” # Change to the correct SSH key tasks:
    • name: Install rsync if not present
      apt:
      name: rsync
      state: present
    • name: Ensure SSH key exists for passwordless authentication
      ansible.builtin.copy:
      src: “{{ ssh_key }}.pub”
      dest: “/home/{{ ansible_user }}/.ssh/authorized_keys”
      remote_src: yes
    • name: Create cron job for periodic backup
      ansible.builtin.cron:
      name: “Automated Data Backup”
      minute: “0”
      hour: “*/6” # Runs every 6 hours
      job: “rsync -az –delete -e ‘ssh -i {{ ssh_key }}’ {{ source_dir }}/ {{ to_server }}:{{ dest_dir }}/”

コメント

コメントを残す