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

Vue page monitoring user preview time function implementation code

A recent business involves such a requirement tha...

A detailed tutorial on using Docker to build a complete development environment

Introduction to DNMP DNMP (Docker + Nginx + MySQL...

Using MySQL database in docker to achieve LAN access

1. Get the mysql image docker pull mysql:5.6 Note...

Solution to the problem of installing MySQL compressed version zip

There was a problem when installing the compresse...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

Detailed explanation of MySQL index selection and optimization

Table of contents Index Model B+Tree Index select...

Vue realizes picture switching effect

This article example shares the specific code of ...

Introduction to commonly used MySQL commands in Linux environment

Enter the mysql command: mysql -u+(user name) -p+...

MySQL reports an error: Can't find file: './mysql/plugin.frm' solution

Find the problem Recently, I found a problem at w...

The difference between html Frame, Iframe and Frameset

10.4.1 The difference between Frameset and Frame ...

Using Nginx to implement grayscale release

Grayscale release refers to a release method that...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

About Vue's 4 auxiliary functions of Vuex

Table of contents 1. Auxiliary functions 2. Examp...