Linux script for defining variables

The existing two pieces of code are basically the same, but the variables are changed, and the others are unchanged, but the results are different during execution

Code 1:

vi back.sh
#backup import file,such as /etc/rc.local /var/spool/cron/root
IP=$(ifconfig eth1|sed -nr '2s#.*addr:(.*)  B.*#\1#gp')
Path=/backup
if [ $(date +%w) -eq 0 ]
then
    Time=$(date +%F-%w -d "-1 day")
else
    Time=$(date +%F "-1 day")
fi
mkdir $Path/$IP -p
cd / &&\
tar zcfh $Path/$IP/backup_$Time.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs &&\
md5sum $Path/$IP/backup_$Time.tar.gz >$Path/$IP/flag_$Time.log &&\
rsync -azv $Path/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.password &&\
find $Path/ -type f \( -name "*.log" -o -name "*.tar.gz" \) -mtime +7 |xargs rm –f
"back.sh" 15L, 628C written

Code two:

vi back.sh
#backup import file,such as /etc/rc.local /var/spool/cron/root
IP=$(ifconfig eth1|sed -nr '2s#.*addr:(.*)  B.*#\1#gp')
Path=/backup
if [ $(date +%w) -eq 0 ]
then
    Time=$(date +%F-%w -d "-1 day")
else
    Time=$(date +%F "-1 day")
fi
mkdir $Path/$IP -p
cd / &&\
tar zcfh /backup/$IP/backup_$Time.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs &&\
md5sum $Path/$IP/backup_$Time.tar.gz >$Path/$IP/flag_$Time.log &&\
rsync -azv $Path/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.password &&\
find $Path/ -type f \( -name "*.log" -o -name "*.tar.gz" \) -mtime +7 |xargs rm –f
"back.sh" 15L, 628C written

The above code only modifies the packing situation. The result of tar zcf /backup is different from that of defining a variable tar $Path /. The first execution result is:

Code 1 execution result:

[root@nfs01 backup]# ls
172.16.1.31 backup_2017-12-23-6.tar.gz flag_2017-12-23-6.log

Code 2 execution result:

[root@nfs01 backup]# ls
172.16.1.31

The principle of code one and code two are the same, but why are the results of execution different? I think it is caused by the beginning of the directory of environment variables, which invalidates the previous environment variables and packages them twice:

Test script

[root@nfs01 scripts]# sh -x back.sh 
++ sed -nr '2s#.*addr:(.*)  B.*#\1#gp'
++ ifconfig eth1
+ IP=172.16.1.31
+ Path=/backup
++ date +%w
+ '[' 4 -eq 0 ']'
++ date +%F -d '-1 day'
+ Time=2017-12-20
+ mkdir /backup/172.16.1.31 -p
+ cd /
+ tar zcfh /backup/172.16.1.31/backup_2017-12-20.tar.gz var/spool/cron/root etc/rc.local etc/sysconfig/iptables var/www/html app/logs
+ md5sum /backup/172.16.1.31/backup_2017-12-20.tar.gz
+ rsync -azv /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.password
sending incremental file list
172.16.1.31/backup_2017-12-20.tar.gz
172.16.1.31/flag_2017-12-20.log

sent 1128 bytes  received 65 bytes  2386.00 bytes/sec
total size is 2960  speedup is 2.48
+ xargs rm -f
+ find /backup/ -type f '(' -name '*.log' -o -name '*.tar.gz' ')' -mtime +7

There is no problem with the test script, but I don't know what the reason is. I hope God can answer it. Thank you very much!!!!

Tags: Linux iptables rsync

Posted on Fri, 01 May 2020 22:38:25 -0700 by tigomark