How to set up scheduled backup tasks in Linux centos

How to set up scheduled backup tasks in Linux centos

Implementation Preparation

# Need to back up the file path: /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 with date + %Y%m%d%H%M%S

1. Write a shell script

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

# Logs are backed up to this directory. Define the variable using single quotes mypath='/tmp/logs'
# echo /tmp/logs
echo ${mypath}

# Log to be backed up mylog='/opt/apollo/logs/access_log'
# Response to /opt/apollo/logs/access_log
echo ${mylog}

# Timestamp, execute the command using ``, esc below time = `date +%Y%m%d%H%M%S`
# Response timestamp echo ${time}

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

2. Execute test.sh

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

3. Execute 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 permissions to the 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 again, the script does not report an error

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

6. Edit scheduled tasks

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

7. View scheduled tasks

# Execute test.sh once every minute
* * * * * 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 the file access_log

# Need to backup file path:
/opt/apollo/logs/access_log
# Edit file [root@localhost logs]# vi /opt/apollo/logs/access_log
# The additional content is as follows:
mmmmmmmmmmmmmmmmmmmmm

10. After 1 minute, check the backup directory again

[root@localhost logs]# cat 20190120083101_access.log
djddjsjsjsjjsjsjsj
mmmmmmmmmmmmmmmmmmmmm

11. So far, the scheduled backup task is completed.

Congratulations, you have learned how to back up!

12. Delete scheduled tasks

[root@localhost logs]# crontab -r
You have new mail in /var/spool/mail/root

13. View scheduled tasks

[root@localhost logs]# crontab -l
no crontab for root

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Alibaba Cloud Centos7 installation and configuration of SVN
  • How to add Nginx to system services in CentOS7
  • Detailed explanation of Nginx installation, SSL configuration and common commands under Centos7.x
  • Solution for not being able to use pip after installing python3.7.1 on centos6.5
  • How to configure Nginx virtual host in CentOS 7.3
  • Solution to the error when installing Docker on CentOS version
  • Three methods to modify the hostname of Centos7
  • Linux centOS installation JDK and Tomcat tutorial
  • How to build Jenkins+Maven+Git continuous integration environment on CentOS7
  • Centos7.5 configuration java environment installation tomcat explanation

<<:  Common usage of hook in react

>>:  Detailed explanation of MYSQL log and backup and restore issues

Recommend

JavaScript implements single linked list process analysis

Preface: To store multiple elements, arrays are t...

CSS3 achieves conic-gradient effect

grammar: background-image: conic-gradient(from an...

Why is there this in JS?

Table of contents 1. Demand 2. Solution 3. The fi...

Two ways to specify the character set of the html page

1. Two ways to specify the character set of the h...

Analysis of implicit bug in concurrent replication of MySQL 5.7

Preface Most of our MySQL online environments use...

The use of FrameLayout in six layouts

Preface In the last issue, we explained LinearLay...

Summarize several common ranking problems in MySQL

Preface: In some application scenarios, we often ...

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary Write a SQL...

Detailed explanation of the principles and usage of MySQL stored procedures

This article uses examples to explain the princip...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Linux kernel device driver address mapping notes

#include <asm/io.h> #define ioremap(cookie,...

Detailed explanation of how to use several timers in CocosCreator

1. setTimeOut Print abc after 3 seconds. Execute ...

Mysql get table comment field operation

I won't say much nonsense, let's just loo...

Detailed explanation of MySQL database isolation level and MVCC

Table of contents 1. Isolation Level READ UNCOMMI...

An article explains Tomcat's class loading mechanism

Table of contents - Preface - - JVM Class Loader ...