Table of Contents |
---|
Linux
MySQL
To setup set up a cron job for daily database backup follow the steps mentioned below
- Create a backup.sh file using command - vi backup.sh
On the vim, editor screen enter the command for database backup and to delete files older than 30 days
Code Block mysqldump -u<user> -p<password> --single-transaction --skip-lock-tables --routines database name > /backup directory path/OPENSPECIMEN_`date +\%d-\%m-\%Y`.SQL find /home/daily-database-backup/ -mtime +30 -exec rm {} \;
Create a cron job with the command - : crontab -e
This will open a crontab (cron configuration file) and the format of the job should be as follows.
minute hour day-of-month month day-of-week <path to the backup.sh file>
This is an example file to take database backup nightly everyday every day at 11:59
Code Block 59 23 * * * /home/backup.sh
...
It will take the backup of database daily and also delete files older then than 30 days
scriptdatabase-backup.bat
Code Block |
---|
@echo off |
...
echo Exporting OpenSpecimen database dump... |
...
set backupFilename=OPENSPECIMEN_%DATE:~7,2%-%DATE:~4,2%-%DATE:~10,4%.SQL |
...
mysqldump -uroot -py4txhySc --routines --result-file="path of database backup directory\%backupFilename%" <name of the databse> |
...
set backupfldr=path of database backup directory |
...
set retaindays=30 |
...
Forfiles -p %backupfldr% -s -m *.SQL -d -%retaindays% -c "cmd /c del /q @path" |
...
echo Done! |
...