Applicable scene: the need to do some configuration script parameters specify a backup directory, tar compressed storage, via scp backup files are stored in another server. May be performed automatically, no execution cycle is set.
#!/bin/bash
source_dir='/data/docj/'
tar_file='attachment'
target_dir='/data/backup/attachment/'
tmp_dir='/data/tmp/'
current_time=$(date +'%Y-%m-%d_%H%M%S')
login_user='root'
login_ip='192.168.142.154'
echo "----$0-$current_time-文件备份打包开始----"
cd $source_dir
tar -zcf $tmp_dir${current_time}.tar.gz $tar_file
echo "----$0-$current_time-文件发送开始----"
expect -c "
spawn scp $tmp_dir${current_time}.tar.gz $login_user@$login_ip:$target_dir
expect {
\"*assword*\"
{
set timeout 600;
send \"123456\r\";
}
\"*yes/no*\"
{
send \"yes\r\"; exp_continue;
}
}
expect eof"
rm -f $tmp_dir*
echo "----$0-$current_time-文件备份执行结束----"