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

Solution to forgetting the administrator password of mysql database

1. Enter the command mysqld --skip-grant-tables (...

Getting Started with Nginx Reverse Proxy

Table of contents Overview The role of reverse pr...

How to deploy LNMP architecture in docker

Environmental requirements: IP hostname 192.168.1...

jQuery implements employee management registration page

This article example shares the specific code of ...

Zabbix3.4 method to monitor mongodb database status

Mongodb has a db.serverStatus() command, which ca...

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

Vue3.0 handwritten carousel effect

This article shares the specific code of Vue3.0 h...

mysql replace part of the field content and mysql replace function replace()

[mysql] replace usage (replace part of the conten...

JS cross-domain XML--with AS URLLoader

Recently, I received a requirement for function ex...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...

JS removeAttribute() method to delete an attribute of an element

In JavaScript, use the removeAttribute() method o...