scp实现服务器数据自动备份 ----- 小彭手把手

分享一个从服务器备份数据的脚本。

#!/usr/bin/sh

# ============ code description =============
# 
# --- Author           : Yifeng Peng 
# --- Created Date     : 2022.03.04
# --- Function         : backup file automatically
#
# ------------ code details --------------
#
# 1. get the lasted file and remove this file     ---- ensure all file is completed.
# 2. loop all files in the document backuped and find files not exited in current document.  
# 3. scp files not exited.
# 
# ===========================================

# ========== user guide ==========
# 
#  1. "casename" must be modified before invoking this code when scp use "$casename".
#  2. user messages and full path of target doucument in scp command must be modified 
#  3. follow line (1234556) of scp is the password of user.
#
# ================================ 

casename=ref01

filename=$casename
rm_lasted_file=`ls -lt ./ | grep $filename | head -n 1 |awk '{print $9}'`
echo 'The lasted file is ' $rm_lasted_file 
rm -rf $rm_lasted_file
echo 'Remove file " ' $rm_lasted_file ' " first to avoid that the last file of the previous transfer was incomplete!!!!'
for fils in `cat down.txt`  # down.txt can be get by 'ls >down.txt', and remove files you do not want to backup.
do 
   if [ ! -f $fils ] ;then
   echo $fils is begin  backup ............
   scp -r -P 22 pengyf@123.1.222.23:/pengyf/modle/cesm1_2/cases/$casename/run/$fils ./ <<EOF
   1234556    
EOF
   else 
   echo $fils is exited!!! ingroed this file.
   fi 
done 

这份代码利用mobaxterm软件的本机类似cmd的功能运行成功。
mobaxterm的安装包可以点击下载

mobaxterm操作步骤如下:

  1. 点击加号新建一个cmd
  2. 和linux的命令一样的,cd进入到你要将文件保存在的路径。
  3. 把上面的命令写成bash脚本并执行(记得修改对应的变量)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值