FTP通过shell脚本自动上传文件

1.通过ftp命令,只能传文件,不能上传目录,可以mkdir -p /server/scripts    创建一个指定存放脚本的目录

[root@shnne scripts]#vim ftp_upload.sh
#!/bin/bash
FTILE_NAME=$1
ftp -n <<- EOF
open 192.168.1.101
user shnne shnne123
lcd /data    #指定要上传的本地路径
cd /data    #指定上传的ftp端路径
bin
put $FTILE_NAME
bye
EOF

[root@shnne scripts]#vim auto_ftp.sh
#!/bin/bash
for file in `ls /data`    #指定要上传的目录
do
/bin/sh /server/scripts/ftp_upload.sh $file
done
#注:因为在上传data里的文件时候,可能再次执行的时候会覆盖,建议可以用mv /data/* /backup 移动到另一个备份目录下,或者成功以后删除也行。

2.通过lftp命令来上传所有文件,可以上传整个目录的所有文件

[root@shnne ~]# vim ftp_upload2.sh
#!/bin/sh
/usr/bin/lftp <<END
open ftp://shnne:shnne123@192.168.1.101
lcd /data
mirror -R .
END

以上任务都需要做计划任务来执行的。

#!/bin/sh
#The shell command is to package backup files and upload them to offsite backup servers

backupdir=/backup
dstfile="/etc/hosts /boot/grub"
bakname=bak_$(date +%F)

#check the directory is exist
if [ ! -d "$backupdir" ]
then
 /bin/mkdir -p $backupdir
fi

#backup file or directory
/bin/tar zcf $backupdir/${bakname}.tar.gz $dstfile

#Transmission backup
/usr/bin/lftp <<END
open ftp://shnne:shnne123@192.168.1.101
cd bak
lcd $backupdir
mirror -R .
END

#Deletion of more than 180 days
/usr/bin/find $backupdir -type f -name "*.gz" -mtime +180|xargs rm -f


分享到:
关键词:WinServer

网友留言(0 条)

发表评论