Compare commits

...
5 Commits
Author SHA1 Message Date
3err0 0136f3a6c8 typo 2024-02-28 13:58:12 +08:00
3err0 3b5493bf52 ansible 2024-02-27 18:52:12 +08:00
3err0 dc5f6bd932 delete files 2023-11-13 19:40:10 +08:00
3err0 4dae05ba2c check if rsync installed 2023-03-18 10:36:13 +08:00
3err0 418460f5cc archive with date option 2023-03-18 10:33:25 +08:00
2 changed files with 90 additions and 7 deletions
+61
View File
@@ -0,0 +1,61 @@
- name: Deploy rsync_backup script
hosts: all
become: yes
vars:
script_url: "https://code.3err0.ru/3err0/rsync/raw/branch/main/rsync_backup.sh"
script_path: "/usr/local/bin/rsync_backup"
exclude_url: "https://code.3err0.ru/3err0/rsync/raw/branch/main/etc/rsyncd.exclude"
backup_user: "backup"
dest_ip: "ip"
files_source: "'/opt'"
tasks:
- name: Download script
ansible.builtin.get_url:
url: "{{ script_url }}"
dest: "{{ script_path }}"
mode: '0755'
- name: Create rsyncd.pwd file
ansible.builtin.copy:
dest: /etc/rsyncd.pwd
content: password
mode: '0600'
- name: Create rsyncd.exclude file
ansible.builtin.get_url:
url: "{{ exclude_url }}"
dest: /etc/rsyncd.exclude
mode: '0644'
- name: Update user in script
ansible.builtin.lineinfile:
path: "{{ script_path }}"
regexp: "^user='[^']*'"
line: "user='{{ backup_user }}'"
state: present
- name: Update ip in script
ansible.builtin.lineinfile:
path: "{{ script_path }}"
regexp: "^ip='[^']*'"
line: "ip='{{ dest_ip }}'"
state: present
- name: Update source in script
ansible.builtin.lineinfile:
path: "{{ script_path }}"
regexp: "^source='[^']*'"
line: "source={{ files_source }}"
state: present
- name: Schedule script in cron for daily execution at 1 AM
ansible.builtin.cron:
name: "Backup with rsync"
job: "{{ script_path }}"
minute: "0"
hour: "1"
day: "*"
month: "*"
weekday: "*"
+29 -7
View File
@@ -16,20 +16,42 @@ mode='archive'
delete_archive=true delete_archive=true
#delete_archive=false #delete_archive=false
archive_with_date=true
#archive_with_date=false
if ! which rsync >/dev/null 2>&1; then
echo "rsync is not installed. Installing..."
sudo apt-get update
sudo apt-get install -y rsync
fi
if [ "$mode" == "files" ]; then if [ "$mode" == "files" ]; then
rsync -a --exclude-from=$exclude_file --password-file=$password_file $source $user@$ip::$destination rsync -a --exclude-from=$exclude_file --password-file=$password_file $source $user@$ip::$destination
if [ $? -eq 0 ] && [ "$delete_archive" = true ]; then
rm -rf $source/*
fi
else else
mkdir -p $archive_dir mkdir -p $archive_dir
for dir in $source if [ "$archive_with_date" = true ]; then
do today=$(date +"%Y%m%d")
archive_name=$(date +"%Y%m%d")-$(echo $dir | tr '/' '_').tar.gz archive_dir="$archive_dir/$today"
tar -czf $archive_dir/$archive_name $dir mkdir -p $archive_dir
fi
for dir in $source; do
if [ "$archive_with_date" = true ]; then
archive_name=$(echo $dir | tr '/' '_').tar.gz
tar -czf $archive_dir/$archive_name $dir
else
archive_name=$(basename $dir).tar.gz
tar -czf $archive_dir/$(date +"%Y%m%d")-$archive_name $dir
fi
done done
rsync -a --exclude-from=$exclude_file --password-file=$password_file $archive_dir/* $user@$ip::$destination rsync -a --exclude-from=$exclude_file --password-file=$password_file --relative $archive_dir/ $user@$ip::$destination
if [ $? -eq 0 ] && [ "$delete_archive" = true ]; then if [ $? -eq 0 ] && [ "$delete_archive" = true ]; then
rm -f $archive_dir/* rm -rf $archive_dir/*
fi fi
fi fi