Keeping backup of Database is really important for every system.
Please read this (5 Reasons why you need database backup) article about why we need database backup.
Here, I would like to make database backup everyday from Java, Spring Boot application.
Create controller class and import the following setting for scheduling.
Then, create new method with @Scheduled annotation inside the class.
Here, I use cron = "0 30 1 * * ?" so this cron task will run at 1:30 AM every day, every month.
And I use mysqldump command same as we are using with command prompt.
Use --databases option in mysqldump command for multiple databases
Use --all-databases option for all databases
Full code here and it will backup selected databases and keep sql file under "C:\home" folder. Enjoy!
<Updated 24/9/2019>
Oh I forgot to mention about 2 parameters "dbUserName" and "dbUserPassword" which I didn't declare in the source code.
These parameters are the username and password of your database.
Comments