shell脚本实现scp远程文件拷贝 zip压缩与tar打包

实现思路:

验证当前使用系统的用户,root才可以执行脚本
备份操作打印日志需要时间戳信息
要备份的文件需要先在本地打包,zip压缩和tar压缩
mkdir备份路径,每次保留最新版本
脚本执行失败,退出:exit 1
KaTeX parse error: Expected 'EOF', got '#' at position 19: …= 0表示上一条指令执行失败 #̲!/bin/bash user…user" != “root” ];then
echo “you are not root,su root:”
su root
else
echo “check root successfully”
fi

today=date +%Y-%m-%d-%H:%M:%S
srcdir_sql=/root
srcdir_pic=/root/pic
localsrcdir=/root/backupsrcdir # not exist
remoteip=x.x.x.x
remotedir=/root/backupdesdir # not exist
backuplog=$srcdir_sql/mybackup.log # will touch a mybackup.log in srcdir_sql
echo t o d a y " ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ " > > today"*****************************************">> today"">>backuplog
echo t o d a y " − i n f o − − − − − − − − − − b a c k u p i n i t s u c c e s s " > > today"-info----------back up init success">> today"infobackupinitsuccess">>backuplog

tar local srcfile:sql and pic

mkdir localsrcdir

if [ ! -d $localsrcdir ];then
mkdir l o c a l s r c d i r i f [ " localsrcdir if [ " localsrcdirif["?" != 0 ];then
echo t o d a y " − e r r o r − − − − − − − − − − f a i l t o m k d i r l o c a l s r c d i r " > > today"-error----------fail to mkdir localsrcdir">> today"errorfailtomkdirlocalsrcdir">>backuplog
exit 1
else
echo t o d a y " − i n f o − − − − − − − − − − s u c c e s s t o m k d i r l o c a l s r c d i r " > > today"-info----------success to mkdir localsrcdir">> today"infosuccesstomkdirlocalsrcdir">>backuplog
fi
else
rm -rf $localsrcdir
mkdir l o c a l s r c d i r i f [ " localsrcdir if [ " localsrcdirif["?" != 0 ];then
echo t o d a y " − e r r o r − − − − − − − − − − c a n n o t c r e a t e l o c a l s r c d i r , p l e a s e t r y a g a i n " > > today"-error----------can not create localsrcdir,please try again">> today"errorcannotcreatelocalsrcdir,pleasetryagain">>backuplog
exit 1
else
echo t o d a y " − i n f o − − − − − − − − − − r m a n d c r e a t e l o c a l s r c d i r s u c c e s s " > > today"-info----------rm and create localsrcdir success">> today"informandcreatelocalsrcdirsuccess">>backuplog
fi
fi
echo t o d a y " − i n f o − − − − − − − − − − s t a r t g z i p p i c f i l e " > > today"-info----------start gzip pic file">> today"infostartgzippicfile">>backuplog

zip pic file

piczip=piczip.zip
if [ ! -d $piczip ];then
zip -r piczip.zip s r c d i r p i c i f [ " srcdir_pic if [ " srcdirpicif["?" != 0 ];then
echo t o d a y " − e r r o r − − − − − − − − − − f a i l t o z i p p i c " > > today"-error----------fail to zip pic">> today"errorfailtozippic">>backuplog
exit 1
else
echo t o d a y " − i n f o − − − − − − − − − − s u c c e s s t o z i p p i c " > > today"-info----------success to zip pic">> today"infosuccesstozippic">>backuplog
fi
else
rm -rf $piczip
zip -r piczip.zip $srcdir_pic
fi

cp $srcdir_sql/shop.sql $localsrcdir
cp $piczip $localsrcdir

tar localsrcdir

localsrcdir_tar=backup_sqlandpic.tar.gz
if [ ! -d $localsrcdir_tar ];then
tar -zcPf backup_sqlandpic.tar.gz l o c a l s r c d i r i f [ " localsrcdir if [ " localsrcdirif["?" != 0 ];then
echo t o d a y " − e r r o r − − − − − − − − − − f a i l t o t a r l o c a l s r c d i r " > > today"-error----------fail to tar localsrcdir">> today"errorfailtotarlocalsrcdir">>backuplog
exit 1
else
echo t o d a y " − i n f o − − − − − − − − − − s u c c e s s t o t a r l o c a l s r c d i r " > > today"-info----------success to tar localsrcdir">> today"infosuccesstotarlocalsrcdir">>backuplog
fi
else
rm -rf $localsrcdir_tar
tar -zcPf backup_sqlandpic.tar.gz $localsrcdir
fi

if [ ! -d $remotedir ];then
mkdir r e m o t e d i r i f [ " remotedir if [ " remotedirif["?" != 0 ];then
echo t o d a y " − e r r o r − − − − − − − − − − f a i l t o m k d i r r e m o t e d i r " > > today"-error----------fail to mkdir remotedir">> today"errorfailtomkdirremotedir">>backuplog
exit 1
else
echo t o d a y " − i n f o − − − − − − − − − − s u c c e s s t o m k d i r r e m o t e d i r " > > today"-info----------success to mkdir remotedir">> today"infosuccesstomkdirremotedir">>backuplog
fi
else
rm -rf $remotedir
mkdir $remotedir
fi

send tar backup to remote

scp $localsrcdir_tar r e m o t e i p : remoteip: remoteip:remotedes
passwd=“0000000000”
/usr/bin/expect<<-EOF
set timeout 3
spawn ssh root@x.x.x.x
expect{
“*password:”{send “KaTeX parse error: Can't use function '\r' in math mode at position 7: passwd\̲r̲"} } interact e…?” != 0 ]
then
echo $today"-error----------scp backup file error" >> $backuplog
exit 1
fi
echo $today"-info----------scp bakup file successfully" >> $backuplog
echo “scp bakup file successfully”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值