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

Analysis of several reasons why Iframe should be used less

The following graph shows how time-consuming it is...

HTML design pattern daily study notes

HTML Design Pattern Study Notes This week I mainl...

Small problem with the spacing between label and input in Google Browser

Code first, then text Copy code The code is as fol...

vue.js Router nested routes

Preface: Sometimes in a route, the main part is t...

Common rule priority issues of Nginx location

Table of contents 1. Location / Matching 2. Locat...

Example of converting JS one-dimensional array into three-dimensional array

Today I saw a friend asking a question in the Q&a...

Summary of common functions of PostgreSQL regular expressions

Summary of common functions of PostgreSQL regular...

Convert psd cut image to div+css format

PSD to div css web page cutting example Step 1: F...

Sample code for nginx to achieve dynamic and static separation

1. Simple configuration of nginx's dynamic an...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Solution to leaving gaps between BootStrap grids

Table of contents [See an example]: [The original...

HTML basics HTML structure

What is an HTML file? HTML stands for Hyper Text M...