Friday, August 25, 2023

::: Backup & Restore MariaDB MySQL using bat Script in windows :::.

1. run script fullbackup Full_backup_mariadb.bat
2. run script transaksi log back_log_mariadb_windows.bat
3. result
 
 
4. script full backup
 
rem script create by Teguh Triharto
@echo off

rem Set the necessary variables
set MYSQL_PATH=C:\Program Files\MariaDB\bin
set BACKUP_DIR=C:\datamysql\backup
set DB_USER=root
set DB_PASSWORD=root
set DB_NAME=teguhth
set MAX_BACKUP_AGE_DAYS=30

rem Get current date and time for backup file naming
set TIMESTAMP=%date:~4,2%%date:~7,2%%date:~10,4%_%time:~0,2%%time:~3,2%%time:~6,2%
set TIMESTAMP=%TIMESTAMP: =0%

rem Create the backup directory if it doesn't exist
if not exist "%BACKUP_DIR%" mkdir "%BACKUP_DIR%"

rem Perform the database backup
"%MYSQL_PATH%\mysqldump.exe" --user=%DB_USER% --password=%DB_PASSWORD% -CfQq --max-allowed-packet=1G --hex-blob --order-by-primary --single-transaction --routines=true --triggers=true --no-data=false --databases %DB_NAME% > "%BACKUP_DIR%\full_%DB_NAME%_%TIMESTAMP%.sql"

rem Check if the backup was successful
if %ERRORLEVEL% neq 0 (
    echo Database backup failed!
) else (
    echo Database backup completed successfully!
)


rem Delete backups older than MAX_BACKUP_AGE_DAYS
forfiles /p "%BACKUP_DIR%" /s /m *.sql /d -%MAX_BACKUP_AGE_DAYS% /c "cmd /c del @path"
 
5. script transaction log

rem script create by Teguh Triharto 
@echo off

rem Set the necessary variables
set MYSQL_PATH=C:\Program Files\MariaDB\bin
set BACKUP_DIR=C:\datamysql\backup
set DB_USER=root
set DB_PASSWORD=root
set DB_NAME=teguhth
set MAX_BACKUP_AGE_DAYS=30

rem Get current date and time for backup file naming
set TIMESTAMP=%date:~4,2%%date:~7,2%%date:~10,4%_%time:~0,2%%time:~3,2%%time:~6,2%
set TIMESTAMP=%TIMESTAMP: =0%

rem Create the backup directory if it doesn't exist
if not exist "%BACKUP_DIR%" mkdir "%BACKUP_DIR%"

rem Perform the database backup transaksi log
"%MYSQL_PATH%\mysqldump.exe" --user=%DB_USER% --password=%DB_PASSWORD%  --databases %DB_NAME% > "%BACKUP_DIR%\log_%DB_NAME%_%TIMESTAMP%.sql"


rem Check if the copy was successful
if %ERRORLEVEL% neq 0 (
    echo Error log backup failed!
) else (
    echo Error log backup completed successfully!
)

rem Delete backups older than MAX_BACKUP_AGE_DAYS
forfiles /p "%BACKUP_DIR%" /s /m *.log /d -%MAX_BACKUP_AGE_DAYS% /c "cmd /c del @path"

No comments:

Post a Comment

Popular Posts