rsync/rsync_backup.sh

57 lines
1.4 KiB
Bash
Raw Normal View History

2023-02-15 06:36:57 +08:00
#!/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/'
2023-03-10 07:43:56 +08:00
archive_dir='archives/'
2023-02-15 06:36:57 +08:00
2023-03-10 07:43:56 +08:00
mode='archive'
#mode='files'
2023-03-10 07:46:47 +08:00
delete_archive=true
#delete_archive=false
2023-03-18 10:33:25 +08:00
archive_with_date=true
#archive_with_date=false
2023-03-18 10:36:13 +08:00
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
2023-03-10 07:43:56 +08:00
if [ "$mode" == "files" ]; then
rsync -a --exclude-from=$exclude_file --password-file=$password_file $source $user@$ip::$destination
2023-11-13 19:40:10 +08:00
if [ $? -eq 0 ] && [ "$delete_archive" = true ]; then
rm -rf $source/*
fi
2023-03-10 07:43:56 +08:00
else
mkdir -p $archive_dir
2023-03-18 10:33:25 +08:00
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
2023-03-10 07:43:56 +08:00
done
2023-03-18 10:33:25 +08:00
rsync -a --exclude-from=$exclude_file --password-file=$password_file --relative $archive_dir/ $user@$ip::$destination
2023-03-10 07:43:56 +08:00
2023-03-10 07:46:47 +08:00
if [ $? -eq 0 ] && [ "$delete_archive" = true ]; then
2023-03-18 10:33:25 +08:00
rm -rf $archive_dir/*
2023-03-10 07:43:56 +08:00
fi
2023-03-18 10:33:25 +08:00
fi