分享一个从服务器备份数据的脚本。
#!/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操作步骤如下:
- 点击加号新建一个cmd
- 和linux的命令一样的,cd进入到你要将文件保存在的路径。
- 把上面的命令写成bash脚本并执行(记得修改对应的变量)