[MariaDB/MySQL] How to backup the data weekly

[MariaDB/MySQL] How to backup the data weekly

Goal

  • Creat MariaDB backup shell script
  • Register in Cron


Intro

  • I wanted to dump the data which was in MariaDB for stability


Practice

1. Dump shell script

  • Script
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#####################################################################
# Date          Name            Desc
#####################################################################
# 2019-07-29    SeongbaeChang   Initialize
# 2018-07-17    SeongbaeChang   Initialize
#####################################################################
#!/bin/bash

# Set current datetime
TIMESTAMP=`date +"%y.%m.%d.%H%M"`
BACKUP_FILENAME=backup_mariadb_agriculture_${TIMESTAMP}.sql
ROOT_HOME="/root"
MY_USER_HOME="/home/dorbae"

echo "[`date +"%Y-%m-%d %H:%M:%S"`] Start Backup..."
echo "[`date +"%Y-%m-%d %H:%M:%S"`] Backup File Name = ${BACKUP_FILENAME}"
# Backup MariaDB data as SQL format
mysqldump -uagric -pagriculture agriculture > ${ROOT_HOME}/${BACKUP_FILENAME}

echo "[`date +"%Y-%m-%d %H:%M:%S"`] Moving file ${ROOT_HOME} to ${MY_USER_HOME}"
# Move to another user home directory
mv ${ROOT_HOME}/${BACKUP_FILENAME} ${MY_USER_HOME}/${BACKUP_FILENAME}
# Change the owner managing the backup file
chown dorbae:dorbae ${MY_USER_HOME}/${BACKUP_FILENAME}

echo "[`date +"%Y-%m-%d %H:%M:%S"`] Finished Backup."


2. Register in Cron

2.1. Edit the cron table

# crontab -e


2.2. Register the schedule

  • Cron pattern
[MINUTE] [HOUR] [DAY] [MONTH] [DAY OF WEEK] [COMMAND]


  • I tried to execute backup script at 7 O’clock every day

screenshot001


3. Result

screenshot002



References

댓글남기기

-->