linux Server Automatic Backup Script Method (mysql Attachment Backup)

  • 2021-07-18 09:40:29
  • OfStack

1. Create an 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 system mode  send notice msg to  Nailing notification 
curl -X POST -d "templateCode=003&content= File backup is successful, and the backup file is saved to 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 timed tasks

1) Enter crontab-e on the command line, then add the corresponding task, and save wq to exit.
2) Directly edit the/etc/crontab file, that is, vi/etc/crontab, and add corresponding tasks.

crontab file format:

* * * * * command

minute hour day month week command

Time-sharing day, moon and week order

For example, the backup script is executed at 1:00 every day every week and every month, 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 is running correctly, you can execute the test manually

3) Query that timed task of the current user.

Summarize


Related articles: