Swipe left or right to navigate to next or previous post
Backup a copy of original file or data made in case the original is lost or damaged. Backup and recovery are interconnected terms. The main purpose of the backup is to recover the data in case the original file and content is lost or damaged.
The Cron daemon is a built-in Linux utility that reads the crontab from commands and scripts to run the process automatically at scheduled time. Cron read the crontab for commands and scripts for predefined commands and scripts.
Want to learn more about cron jobs and see all the possible examples. Read Ultimate Guide on Crontab with examples
Mysqldump is a command-line utility to create dump of the MySQL database along with structure, its data to restore that in case of database corruption, error and system crash.
Want to know more this database dump? Read ultimate guide on MysqlDump
Mysqldump does not allow user to enter user and password in the same command. There is two-way to overcome this password.
Want to know more this database dump without using password? Read Database dump without using password
sudo nano ~/.my.cnf
[mysqldump] user=mysqluser password=mysql_user_secret_password
sudo chmod 600 ~/.my.cnf
The password in the .my.cnf is used by the mysqldump. So, we don't need to provide the password using the mysqldump command
######################################################################## # # This python script is used for mysql database backup using mysqldump. # ######################################################################## import os import time import datetime import pipes # MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup. DB_HOST = 'localhost' DB_USER = 'db_user' DB_USER_PASSWORD = 'db_password' DB_NAME = 'db_name' BACKUP_PATH = '/var/database_backup/dumps' # Getting current DateTime to create the separate backup folder like "20210705-123433". DATE = time.strftime('%Y_%m') TODAYBACKUPPATH = BACKUP_PATH + '/' + DATE DATETIME = time.strftime('%Y%m_%d-%H%M%S') DUMP_FILE_NAME = DATETIME + '-'+ DB_NAME print ("Back started for "+DATETIME) # Checking if backup folder already exists or not. If not exists will create it. try: os.stat(TODAYBACKUPPATH) except: os.mkdir(TODAYBACKUPPATH) # Starting actual database backup process. dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + DB_NAME + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + DUMP_FILE_NAME + ".sql" os.system(dumpcmd) print ("Your backups have been created in '" + TODAYBACKUPPATH + "' directory")
#!/bin/bash /usr/bin/python3 /var/database_backup/database_dump.py
If you are using any virtual environment, you don't need to activate the virtual environment and then invoke the command. You can just give a full path from root to python executable of virtual environment.
#!/bin/bash /full-path-to-virtual-env/bin/python3 /var/database_backup/database_dump.py
The system takes all the necessary dependencies from the virtual environment.
Want to learn more on crontab configuration? Read Ultimate guide to crontab
crontab -e
# run every 12 hours 0 */12 * * * /var/database_backup/script_runner.sh >> /var/database_backup/logs/database_dump.log 2>&1 # run every 30 minutes */30 * * * * /var/database_backup/script_runner.sh >> /var/database_backup/logs/database_dump.log 2>&1
The updated cron jobs are automatically recognized by the system.