Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql backup and Truncate the table

How can I take the mysql database backup as csv file and truncate the table after that on regular basis. how can I achieve this using mysql only.

Any type of help will be appreciated.

like image 722
Tapsee Panu Avatar asked Dec 14 '25 04:12

Tapsee Panu


1 Answers

Instead of using csv files it's better to backup tables into .sql files, which again helps you while importing into database.

You can use bash script and cron jobs for regular backups if you are a Linux user.

Sample script :

#!/bin/bash
_user="root"
_password="YourMySQLRootPassword"

mysqldump -u${_user} -p${_password} DB_Name TableName > Table_name_`date +%Y%m%d`.sql  # here date command is executed for present date
mysql -u${_user} -p{_password} -e "truncate table DB_Name.TableName"

Before you can truncate the table you can check if the dump command was successful then execute table Truncation using "if" conditions. For quick introduction you can check it out this link https://www.youtube.com/watch?v=hwrnmQumtPw

like image 105
Jaya Avatar answered Dec 15 '25 22:12

Jaya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!