How to automatically backup the script for Linux servers (mysql, attachment backup)

How to automatically backup the script for Linux servers (mysql, attachment backup)

1. Create the backup.sh script file

#!/bin/sh
SOURCE_FOLDER=/data
DB_FOLDER=/data/db
BACKUP_FOLDER=/data/backup
TM=`date +%Y%m%d%H%M%S`
echo start to dump mysql database and backup files at $TM
# delete old sql backup files
cd $DB_FOLDER
rm -rf $DB_FOLDER/*.sql
#dump mysql data
/usr/bin/mysqldump -h127.0.0.1 -uroot -pcz_pims@2018 --single-transaction --master-data=2 --databases ms > ms-$TM.sql
echo dump db pims_hf as pims_hf-$TM.sql
/usr/bin/mysqldump -h127.0.0.1 -uroot -ppass --single-transaction --master-data=2 --databases ms_workflow > pims_hf_workflow-$TM.sql
echo dump db ms_workflow as pims_hf_workflow-$TM.sql
/usr/bin/mysqldump -h127.0.0.1 -uroot -ppass --single-transaction --master-data=2 --databases files > files-$TM.sql
echo dump db filesrv as files-$TM.sql
# delete backup files which 30 days before than today
find $BACKUP_FOLDER/* -type f -mtime +30 -name "*.zip" -exec rm {} \;
# compress
cd $SOURCE_FOLDER
zip -q -r $BACKUP_FOLDER/db-$TM.zip db
zip -q -r $BACKUP_FOLDER/files-new.zip files
echo zip $SOURCE_FOLDER files and backup to $BACKUP_FOLDER end
# Call the system method send notice msg to Dingding notification curl -X POST -d "templateCode=003&content=The file backup is successful. The backup file is saved in this directory: $BACKUP_FOLDER" http://127.0.0.1:8080/admin/do/notify/dingding
echo -e end backup files at $TM '\n\n\n'

2. Create a scheduled task

1) Enter crontab -e in the command line, add the corresponding tasks, save and exit.
2) Edit the /etc/crontab file directly, that is, vi /etc/crontab, and add the corresponding tasks.

crontab file format:

* * * * * command

minute hour day month week command

Minutes Hours Day Month Weekday Command

For example: the backup script is executed at 1:00 every week, every month, and every day, and the log is placed in the backup.log file

0 1 * * * /bin/sh /home/script/backup.sh >> /home/script/backup.log 2>&1

If the test script runs normally, you can execute the test manually

3) Query the current user's scheduled tasks.

Summarize

The above is the method of automatic backup script of Linux server (mysql, attachment backup) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to write a MySQL backup script
  • A brief analysis of MySQL backup and recovery
  • Detailed explanation of mysql backup and recovery
  • MySQL scheduled backup solution (using Linux crontab)
  • Brief analysis of mysql scheduled backup tasks
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • How to implement scheduled automatic backup of MySQL under CentOS7
  • Mysql backup multiple database code examples
  • How to elegantly back up MySQL account information

<<:  Vue component to realize carousel animation

>>:  Solve the 1251 error when establishing a connection between mysql and navicat

Recommend

The complete version of the common Linux tool vi/vim

Why learn vim Linux has a large number of configu...

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

Docker container from entry to obsession (recommended)

1. What is Docker? Everyone knows about virtual m...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

HTML head tag meta to achieve refresh redirection

Copy code The code is as follows: <html> &l...

How to encapsulate query components based on element-ui step by step

Table of contents Function Basic query functions ...

Detailed explanation of the role of brackets in AngularJS

1. The role of brackets 1.1 Square brackets [ ] W...

Detailed explanation of the process of building and running Docker containers

Simply pull the image, create a container and run...

How to install MySQL 8.0.17 and configure remote access

1. Preparation before installation Check the data...

How to use CocosCreator for sound processing in game development

Table of contents 1. Basics of audio playback in ...

Detailed introduction to Mysql date query

Query the current date SELECT CURRENT_DATE(); SEL...

How to solve the problem of clicking tomcat9.exe crashing

A reader contacted me and asked why there were pr...