
Task:- Daily backup mysql database for JIRA and Confluence;
- Daily backup of the directory with attachments;
- Archiving all made backups;
- Delete old backups.
I will make a reservation that I needed to do this for Windows and I understand that everything is easier and faster for Linux. Also, if you are an administrator, then this topic is unlikely to be interesting to you and you will not learn anything new. But you can use the written, as a ready and proven solution.
')
First, let's define the variables:
:: Attachments JIRA Confluence set attachments="C:\Program Files\Atlassian\Application Data\JIRA\data" ::, backups set backup_folder=D:\Backups\Atlassian\JIRA\daily_backups :: mysqldump.exe set mysqldump_path="C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" :: MySQL set dbuser=root :: MySQL set dbpass=<change password> :: , (JIRA Confluence) set dbname=jiradb
Each backup should have a unique name, for its generation you can use the year, month, day and time interval.
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set dt=%%c-%%a-%%b) For /f "tokens=1-4 delims=:." %%a in ('echo %time%') do (set tm=%%a%%b%%c%%d) set backupdate=%1%dt%_%tm% set date_folder=%1%dt%
You can adjust your name as you like, in my case the format is: [db name] .YYYY-MM-DD_tt
I started the variable
date_folder so that the folder YYYY-MM-DD is created every day and the backups are added to it.
Accordingly, if there is no directory, then create it:
if exist %backup_folder%\%date_folder% GOTO NODIR mkdir %backup_folder%\%date_folder% :NODIR
Next, we perform the usual backup database using mysqldump, archive with 7zip and delete the archived file.
%mysqldump_path% --user=%dbuser% --password=%dbpass% --databases %dbname% > "%backup_folder%\%date_folder%\%dbname%.%backupdate%.sql" "zip\7za.exe" a -tzip "%backup_folder%\%date_folder%\%dbname%.%backupdate%.zip" "%backup_folder%\%date_folder%\*.sql" del "%backup_folder%\%date_folder%\*.sql"
Do not forget to archive the folder with attachments:
"zip\7za.exe" a -tzip %backup_folder%\%date_folder%\attachments.%backupdate%.zip %attachments%
And all we have to do is remove the old directories (over 14 days).
forfiles -p %backup_folder% -s -d -14 -c "cmd /c if @isdir == TRUE rd /s /q @path"
All we have to do is to feed this bat file to the Windows Task Sheduler and indicate that the task should work every day.
PS If the task does not work, check that there is a tick that the task should be performed even when the user on whose behalf the task is launched is not logged in and that this user has write permissions. Also, do not forget to specify that the task is executed in the directory in which the script resides. (For tasks, this is an optional parameter, but without this, the task was executed, but the script did not run.)
PPS If anyone found the script useful, then you can download it here:
download