rsync/rsync_backup.sh
2023-11-13 19:40:10 +08:00

57 lines
1.4 KiB
Bash

#!/bin/bash
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
password_file='/etc/rsyncd.pwd'
exclude_file='/etc/rsyncd.exclude'
user='user'
ip='ip'
source='/opt /var/www'
destination='data/'
archive_dir='archives/'
mode='archive'
#mode='files'
delete_archive=true
#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
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
mkdir -p $archive_dir
if [ "$archive_with_date" = true ]; then
today=$(date +"%Y%m%d")
archive_dir="$archive_dir/$today"
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
rsync -a --exclude-from=$exclude_file --password-file=$password_file --relative $archive_dir/ $user@$ip::$destination
if [ $? -eq 0 ] && [ "$delete_archive" = true ]; then
rm -rf $archive_dir/*
fi
fi