centos不同主机之间不同用户免密登陆SFTP并下载文件,且批量更改文件名(插入时间)
注意:本篇文章为原创,转载须注明出处。本篇博客地址 http://www.cnblogs.com/Duncan-wolf/articles/8546076.html
需求背景:某平台的业务日志需要增量备份(按天)
环境:
源主机:
IP:192.168.0.115 CentOS release 6.8 (Final)
用户: test1
备份路径:/home/test1/sou_dir
目标主机:
IP:192.168.0.125 CentOS release 6.8 (Final)
用户: test
备份路径:/home/test1/dest_dir
一、源主机用户test1生成公钥并拷贝到目标主机用户test中
[test1@localhost ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/test1/.ssh/id_rsa): /home/test1/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/test1/.ssh/id_rsa. Your public key has been saved in /home/test1/.ssh/id_rsa.pub. The key fingerprint is: 1c:18:88:c7:15:5f:1e:de:a3:3b:fe:ab:f0:58:f2:a3 test1@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | o o+. o | | . + + + o | | . . o o o | | . . . . | | S . | | . | | o + | | O.. | | E.=+o. | +-----------------+ [test1@localhost ~]$ ll .ssh/id_rsa.pub -rw-r--r-- 1 test1 test1 409 Feb 1 14:05 .ssh/id_rsa.pub [test1@localhost ~]$ scp .ssh/id_rsa.pub test@192.168.0.125:/home/test/.ssh/authorized_keys test@192.168.0.125's password: Permission denied, please try again. test@192.168.0.125's password: id_rsa.pub 100% 409 0.4KB/s 00:00 [test1@localhost ~]$
二、测试免密登陆是否成功
[test1@localhost ~]$ sftp test@192.168.0.125
Connecting to 192.168.0.125...
sftp> ls
des_dir
sftp> cd des_dir
sftp> pwd
Remote working directory: /home/test/des_dir
sftp> ls
PyAudio-0.2.10.tar.gz anaconda-ks.cfg
auto_deploy.sh cocos2d-0.6.3.zip
jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm log.txt
move.sh nginx-1.9.9.zip
portaudio-19-9.el6.x86_64.rpm pyaudio-0.2.3-4.el6.x86_64.rpm
pyglet-1.2.4.zip six-1.6.0.tar.gz
test.sh user.sh
zlib-1.2.3-29.el6.x86_64.rpm zlib-1.2.7-17.el7.x86_64.rpm
免秘钥配置成功!
三、编写脚本实现在文件名最后添加日期,后缀保持不变。
#!/bin/bash
#获取一天前时间
DATE=`date -d "1 days ago" +%Y%m%d`
USER=test
#下载文件目录
SRCDIR=/home/test1/src_dir
#SFTP目录(待下载文件目录)
DESDIR=/home/test/des_dir
#目标 IP
IP=192.168.0.125
#端口
PORT=22
sftp $USER@${IP} <<EOF
cd ${DESDIR}
lcd ${SRCDIR}
#需要下载的文件
mget *
bye
EOF
cd $SRCDIR
for file in `ls`
do
mv $file $(echo $file |sed "s/\(.*\)\./\1_$DATE./") #sed表达式意思为将最后一个“.”替换为“_$DATE.”格式
done
四、测试
源主机:
[test1@localhost ~]$ date Thu Feb 1 14:29:11 CST 2018 [test1@localhost ~]$ ll src_dir/ total 0 [test1@localhost ~]$
目标主机:
[test@localhost des_dir]$ ll total 9456 -rw------- 1 root root 1490 Feb 1 00:52 anaconda-ks.cfg -rw-r--r-- 1 root root 411 Feb 1 00:52 auto_deploy.sh -rw-r--r-- 1 root root 5112147 Feb 1 00:52 cocos2d-0.6.3.zip -rw-r--r-- 1 root root 190488 Feb 1 00:52 jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm -rw-r--r-- 1 root root 68 Feb 1 00:52 log.txt -rw-r--r-- 1 root root 79 Feb 1 00:52 move.sh -rw-r--r-- 1 root root 1357919 Feb 1 00:52 nginx-1.9.9.zip -rw-r--r-- 1 root root 80660 Feb 1 00:52 portaudio-19-9.el6.x86_64.rpm -rw-r--r-- 1 root root 287270 Feb 1 00:52 PyAudio-0.2.10.tar.gz -rw-r--r-- 1 root root 83376 Feb 1 00:52 pyaudio-0.2.3-4.el6.x86_64.rpm -rw-r--r-- 1 root root 2330248 Feb 1 00:52 pyglet-1.2.4.zip -rw-r--r-- 1 root root 24716 Feb 1 00:52 six-1.6.0.tar.gz -rw-r--r-- 1 root root 114 Feb 1 00:52 test.sh -rw-r--r-- 1 root root 78 Feb 1 00:52 user.sh -rw-r--r-- 1 root root 74568 Feb 1 00:52 zlib-1.2.3-29.el6.x86_64.rpm -rw-r--r-- 1 root root 91872 Feb 1 00:52 zlib-1.2.7-17.el7.x86_64.rpm [test@localhost des_dir]$ pwd /home/test/des_dir [test@localhost des_dir]$
脚本运行结果如下:
[test1@localhost ~]$ sh -x download_sftp.sh ++ date -d '1 days ago' +%Y%m%d + DATE=20180131 + USER=test + SRCDIR=/home/test1/src_dir + DESDIR=/home/test/des_dir + IP=192.168.0.125 + PORT=22 + sftp test@192.168.0.125 Connecting to 192.168.0.125... sftp> cd /home/test/des_dir sftp> lcd /home/test1/src_dir sftp> #需要下载的文件 sftp> mget * Fetching /home/test/des_dir/PyAudio-0.2.10.tar.gz to PyAudio-0.2.10.tar.gz /home/test/des_dir/PyAudio-0.2.10.tar.gz 100% 281KB 280.5KB/s 00:00 Fetching /home/test/des_dir/anaconda-ks.cfg to anaconda-ks.cfg Couldn't get handle: Permission denied Fetching /home/test/des_dir/auto_deploy.sh to auto_deploy.sh /home/test/des_dir/auto_deploy.sh 100% 411 0.4KB/s 00:00 Fetching /home/test/des_dir/cocos2d-0.6.3.zip to cocos2d-0.6.3.zip /home/test/des_dir/cocos2d-0.6.3.zip 100% 4992KB 4.9MB/s 00:00 Fetching /home/test/des_dir/jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm to jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm /home/test/des_dir/jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm 100% 186KB 186.0KB/s 00:00 Fetching /home/test/des_dir/log.txt to log.txt /home/test/des_dir/log.txt 100% 68 0.1KB/s 00:00 Fetching /home/test/des_dir/move.sh to move.sh /home/test/des_dir/move.sh 100% 79 0.1KB/s 00:00 Fetching /home/test/des_dir/nginx-1.9.9.zip to nginx-1.9.9.zip /home/test/des_dir/nginx-1.9.9.zip 100% 1326KB 1.3MB/s 00:00 Fetching /home/test/des_dir/portaudio-19-9.el6.x86_64.rpm to portaudio-19-9.el6.x86_64.rpm /home/test/des_dir/portaudio-19-9.el6.x86_64.rpm 100% 79KB 78.8KB/s 00:00 Fetching /home/test/des_dir/pyaudio-0.2.3-4.el6.x86_64.rpm to pyaudio-0.2.3-4.el6.x86_64.rpm /home/test/des_dir/pyaudio-0.2.3-4.el6.x86_64.rpm 100% 81KB 81.4KB/s 00:00 Fetching /home/test/des_dir/pyglet-1.2.4.zip to pyglet-1.2.4.zip /home/test/des_dir/pyglet-1.2.4.zip 100% 2276KB 2.2MB/s 00:00 Fetching /home/test/des_dir/six-1.6.0.tar.gz to six-1.6.0.tar.gz /home/test/des_dir/six-1.6.0.tar.gz 100% 24KB 24.1KB/s 00:00 Fetching /home/test/des_dir/test.sh to test.sh /home/test/des_dir/test.sh 100% 114 0.1KB/s 00:00 Fetching /home/test/des_dir/user.sh to user.sh /home/test/des_dir/user.sh 100% 78 0.1KB/s 00:00 Fetching /home/test/des_dir/zlib-1.2.3-29.el6.x86_64.rpm to zlib-1.2.3-29.el6.x86_64.rpm /home/test/des_dir/zlib-1.2.3-29.el6.x86_64.rpm 100% 73KB 72.8KB/s 00:00 Fetching /home/test/des_dir/zlib-1.2.7-17.el7.x86_64.rpm to zlib-1.2.7-17.el7.x86_64.rpm /home/test/des_dir/zlib-1.2.7-17.el7.x86_64.rpm 100% 90KB 89.7KB/s 00:00 sftp> bye + cd /home/test1/src_dir ++ ls + for file in '`ls`' ++ echo auto_deploy.sh ++ sed 's/\(.*\)\./\1_20180131./' + mv auto_deploy.sh auto_deploy_20180131.sh + for file in '`ls`' ++ echo cocos2d-0.6.3.zip ++ sed 's/\(.*\)\./\1_20180131./' + mv cocos2d-0.6.3.zip cocos2d-0.6.3_20180131.zip + for file in '`ls`' ++ echo jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm ++ sed 's/\(.*\)\./\1_20180131./' + mv jack-audio-connection-kit-0.121.3-1.el6.x86_64.rpm jack-audio-connection-kit-0.121.3-1.el6.x86_64_20180131.rpm + for file in '`ls`' ++ echo log.txt ++ sed 's/\(.*\)\./\1_20180131./' + mv log.txt log_20180131.txt + for file in '`ls`' ++ echo move.sh ++ sed 's/\(.*\)\./\1_20180131./' + mv move.sh move_20180131.sh + for file in '`ls`' ++ echo nginx-1.9.9.zip ++ sed 's/\(.*\)\./\1_20180131./' + mv nginx-1.9.9.zip nginx-1.9.9_20180131.zip + for file in '`ls`' ++ echo portaudio-19-9.el6.x86_64.rpm ++ sed 's/\(.*\)\./\1_20180131./' + mv portaudio-19-9.el6.x86_64.rpm portaudio-19-9.el6.x86_64_20180131.rpm + for file in '`ls`' ++ sed 's/\(.*\)\./\1_20180131./' ++ echo PyAudio-0.2.10.tar.gz + mv PyAudio-0.2.10.tar.gz PyAudio-0.2.10.tar_20180131.gz + for file in '`ls`' ++ sed 's/\(.*\)\./\1_20180131./' ++ echo pyaudio-0.2.3-4.el6.x86_64.rpm + mv pyaudio-0.2.3-4.el6.x86_64.rpm pyaudio-0.2.3-4.el6.x86_64_20180131.rpm + for file in '`ls`' ++ echo pyglet-1.2.4.zip ++ sed 's/\(.*\)\./\1_20180131./' + mv pyglet-1.2.4.zip pyglet-1.2.4_20180131.zip + for file in '`ls`' ++ sed 's/\(.*\)\./\1_20180131./' ++ echo six-1.6.0.tar.gz + mv six-1.6.0.tar.gz six-1.6.0.tar_20180131.gz + for file in '`ls`' ++ echo test.sh ++ sed 's/\(.*\)\./\1_20180131./' + mv test.sh test_20180131.sh + for file in '`ls`' ++ sed 's/\(.*\)\./\1_20180131./' ++ echo user.sh + mv user.sh user_20180131.sh + for file in '`ls`' ++ echo zlib-1.2.3-29.el6.x86_64.rpm ++ sed 's/\(.*\)\./\1_20180131./' + mv zlib-1.2.3-29.el6.x86_64.rpm zlib-1.2.3-29.el6.x86_64_20180131.rpm + for file in '`ls`' ++ echo zlib-1.2.7-17.el7.x86_64.rpm ++ sed 's/\(.*\)\./\1_20180131./' + mv zlib-1.2.7-17.el7.x86_64.rpm zlib-1.2.7-17.el7.x86_64_20180131.rpm [test1@localhost ~]$ ll .bash_history .bash_profile download_sftp.sh .mozilla/ .ssh/ .bash_logout .bashrc .gnome2/ src_dir/ .viminfo [test1@localhost ~]$ ll src_dir/ total 9452 -rw-r--r-- 1 test1 test1 411 Feb 1 14:32 auto_deploy_20180131.sh -rw-r--r-- 1 test1 test1 5112147 Feb 1 14:32 cocos2d-0.6.3_20180131.zip -rw-r--r-- 1 test1 test1 190488 Feb 1 14:32 jack-audio-connection-kit-0.121.3-1.el6.x86_64_20180131.rpm -rw-r--r-- 1 test1 test1 68 Feb 1 14:32 log_20180131.txt -rw-r--r-- 1 test1 test1 79 Feb 1 14:32 move_20180131.sh -rw-r--r-- 1 test1 test1 1357919 Feb 1 14:32 nginx-1.9.9_20180131.zip -rw-r--r-- 1 test1 test1 80660 Feb 1 14:32 portaudio-19-9.el6.x86_64_20180131.rpm -rw-r--r-- 1 test1 test1 287270 Feb 1 14:32 PyAudio-0.2.10.tar_20180131.gz -rw-r--r-- 1 test1 test1 83376 Feb 1 14:32 pyaudio-0.2.3-4.el6.x86_64_20180131.rpm -rw-r--r-- 1 test1 test1 2330248 Feb 1 14:32 pyglet-1.2.4_20180131.zip -rw-r--r-- 1 test1 test1 24716 Feb 1 14:32 six-1.6.0.tar_20180131.gz -rw-r--r-- 1 test1 test1 114 Feb 1 14:32 test_20180131.sh -rw-r--r-- 1 test1 test1 78 Feb 1 14:32 user_20180131.sh -rw-r--r-- 1 test1 test1 74568 Feb 1 14:32 zlib-1.2.3-29.el6.x86_64_20180131.rpm -rw-r--r-- 1 test1 test1 91872 Feb 1 14:32 zlib-1.2.7-17.el7.x86_64_20180131.rpm [test1@localhost ~]$
红色字体即为更改后的文件信息:PS:虚拟机时间并未与主机同步
五、配置计划任务,定期执行备份任务
浙公网安备 33010602011771号