Steps to set up a timed backup task under Linux centos

  • 2021-01-02 22:07:21
  • OfStack

prepared


#  The file path needs to be backed up :/opt/apollo/logs/access_log
[root@localhost opt]# cd apollo/
[root@localhost apollo]# tree
.
 ├ ─ ─  logs
 │    └ ─ ─  access_log
 └ ─ ─  test.sh
#  File backup storage path :/tmp/logs
#  The backup file is timestamped date + %Y%m%d%H%M%S

1. Write shell scripts


[root@localhost tmp]# vi /opt/apollo/test.sh
#  The compiler 
# !/bin/bash

#  The logs are backed up to this directory , Define variables in single quotes 
mypath='/tmp/logs'
#  In response to /tmp/logs
echo ${mypath}

#  Logs to back up 
mylog='/opt/apollo/logs/access_log'
#  In response to /opt/apollo/logs/access_log
echo ${mylog}

#  The time stamp , Use of execution command ``,esc The following 
time=`date +%Y%m%d%H%M%S`
#  Response timestamp 
echo ${time}

#  Backup log access_log to /tmp/logs Under the path 
cp ${mylog} ${mypath}/${time}_access.log
#  In response to 
echo ${mypath} ${mypath}/${time}_access.log

2. Perform test. sh


[root@localhost apollo]# ./test.sh
-bash: ./test.sh: Permission denied

3. Implement ls-la


[root@localhost apollo]# ls -la
total 8
drwxr-xr-x  2 root root  21 Jan 20 08:00 .
drwxr-xr-x. 14 root root 4096 Jan 20 07:07 ..
-rw-r--r--  1 root root 489 Jan 20 08:00 test.sh

4. Grant execution permission to file test.sh


[root@localhost apollo]# chmod +x ./test.sh
[root@localhost apollo]# ls -la
total 8
drwxr-xr-x  2 root root  21 Jan 20 08:00 .
drwxr-xr-x. 14 root root 4096 Jan 20 07:07 ..
-rwxr-xr-x  1 root root 489 Jan 20 08:00 test.sh

5. Execute the script again without error


[root@localhost apollo]# ./test.sh
/tmp/logs
/opt/apollo/logs/access_log
20190120080932
/tmp/logs /tmp/logs/20190120080932_access.log

6. Edit timer tasks


[root@localhost logs]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

7. Look at timer tasks


#  Per minute 1 time test.sh
* * * * * sh /opt/apollo/test.sh

8. Restart crond


[root@localhost logs]# service crond reload
Redirecting to /bin/systemctl reload crond.service
You have new mail in /var/spool/mail/root

9. Write file access_log


#  The file path needs to be backed up :
/opt/apollo/logs/access_log
#  Edit the file 
[root@localhost logs]# vi /opt/apollo/logs/access_log
#  Append as follows :
mmmmmmmmmmmmmmmmmmmmm

10. After 1 minute, check the backup storage directory


[root@localhost tmp]# vi /opt/apollo/test.sh
#  The compiler 
# !/bin/bash

#  The logs are backed up to this directory , Define variables in single quotes 
mypath='/tmp/logs'
#  In response to /tmp/logs
echo ${mypath}

#  Logs to back up 
mylog='/opt/apollo/logs/access_log'
#  In response to /opt/apollo/logs/access_log
echo ${mylog}

#  The time stamp , Use of execution command ``,esc The following 
time=`date +%Y%m%d%H%M%S`
#  Response timestamp 
echo ${time}

#  Backup log access_log to /tmp/logs Under the path 
cp ${mylog} ${mypath}/${time}_access.log
#  In response to 
echo ${mypath} ${mypath}/${time}_access.log
0

11. At this point, the scheduled backup task is completed.

Congratulations, you learned to back up!

Delete the timer task


[root@localhost tmp]# vi /opt/apollo/test.sh
#  The compiler 
# !/bin/bash

#  The logs are backed up to this directory , Define variables in single quotes 
mypath='/tmp/logs'
#  In response to /tmp/logs
echo ${mypath}

#  Logs to back up 
mylog='/opt/apollo/logs/access_log'
#  In response to /opt/apollo/logs/access_log
echo ${mylog}

#  The time stamp , Use of execution command ``,esc The following 
time=`date +%Y%m%d%H%M%S`
#  Response timestamp 
echo ${time}

#  Backup log access_log to /tmp/logs Under the path 
cp ${mylog} ${mypath}/${time}_access.log
#  In response to 
echo ${mypath} ${mypath}/${time}_access.log
1

13. Check the timer task


[root@localhost tmp]# vi /opt/apollo/test.sh
#  The compiler 
# !/bin/bash

#  The logs are backed up to this directory , Define variables in single quotes 
mypath='/tmp/logs'
#  In response to /tmp/logs
echo ${mypath}

#  Logs to back up 
mylog='/opt/apollo/logs/access_log'
#  In response to /opt/apollo/logs/access_log
echo ${mylog}

#  The time stamp , Use of execution command ``,esc The following 
time=`date +%Y%m%d%H%M%S`
#  Response timestamp 
echo ${time}

#  Backup log access_log to /tmp/logs Under the path 
cp ${mylog} ${mypath}/${time}_access.log
#  In response to 
echo ${mypath} ${mypath}/${time}_access.log
2

Related articles: