实现思路:
验证当前使用系统的用户,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"−info−−−−−−−−−−backupinitsuccess">>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"−error−−−−−−−−−−failtomkdirlocalsrcdir">>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"−info−−−−−−−−−−successtomkdirlocalsrcdir">>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"−error−−−−−−−−−−cannotcreatelocalsrcdir,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"−info−−−−−−−−−−rmandcreatelocalsrcdirsuccess">>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"−info−−−−−−−−−−startgzippicfile">>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"−error−−−−−−−−−−failtozippic">>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"−info−−−−−−−−−−successtozippic">>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"−error−−−−−−−−−−failtotarlocalsrcdir">>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"−info−−−−−−−−−−successtotarlocalsrcdir">>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"−error−−−−−−−−−−failtomkdirremotedir">>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"−info−−−−−−−−−−successtomkdirremotedir">>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”