Taylohtio/_Scripts/MantisBackup/mantis_backup_sftp.cmd

82 lines
2.2 KiB
Batchfile

@echo off
:: Need to install:
:: - MySQL client tools (mysqldump.exe is used in this script). If version is chenged - change path to exe.
:: - WinSCP (winscp.com) should be installed and added to PATH env variable
:: - 7-Zip console verion (7za.exe is used in this script). Put exe in the same folder as this cmd has or change path.
:: Create needed variables and folders (and delete old files)
SET DRIVELETTER=D
del /F /Q %DRIVELETTER%:\_backups_mantis\backups\*
:: Russian format
::SET TIMESTAMP=%date:~6,4%%date:~3,2%%date:~0,2%%time:~0,2%%time:~3,2%
:: Format on SQL server
SET TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%
:: Replace spaces by 0
SET TIMESTAMP=%TIMESTAMP: =0%
SET BACKUPS_DIR=%DRIVELETTER%:\_backups_mantis\backups\mantis_%TIMESTAMP%
SET SITE_DIR=%BACKUPS_DIR%\site
SET DB_DIR=%BACKUPS_DIR%\db
SET ARCHIVE_DIR=%DRIVELETTER%:\_backups_mantis\archive
mkdir %SITE_DIR%
mkdir %DB_DIR%
mkdir %ARCHIVE_DIR%
:: Create DB backup
:: Destination file
SET DSTFILE=%DB_DIR%\backup-%TIMESTAMP%.sql
:: Connection parameters
SET SQLSERVER=mobimus.palvelin.pro
SET SQLUSER=
SET SQLPASS=""
SET DB=mobimus_mantisbt
:: Do the job - make DB dump
"D:\_backups_mantis\mysqldump.exe" -c -e --default-character-set=utf8 --hex-blob -r %DSTFILE% -h %SQLSERVER% -u %SQLUSER% -p%SQLPASS% %DB%
:: Create Mantis & attachments backup
:: Connection parameters
SET FTPSERVER=mobimus.palvelin.pro
SET FTPUSER=
SET FTPPASS=""
:: Get host key finhertip using WinSCP
SET FTPHOSTKEY=""
:: Backup all Mantis files (with attachments)
SET SRCDIR=public_html/mantisbt
:: Do the job - download files from ftp
winscp.com /ini=nul /command ^
"open sftp://%FTPUSER%:%FTPPASS%@%FTPSERVER% -hostkey="%FTPHOSTKEY%"" ^
"cd %SRCDIR%" ^
"get * %SITE_DIR%\" ^
"close" ^
"exit"
:: Compress backup files
:: Destination file
SET DSTARCH=%BACKUPS_DIR%.zip
:: Do the job
%DRIVELETTER%:\_backups_mantis\7za.exe a -tzip -y %DSTARCH% %BACKUPS_DIR%
:: Remove backup folder from local drive
FOR /D %%p IN ("%DRIVELETTER%:\_backups_mantis\backups\*.*") DO rmdir "%%p" /s /q
:: Move files to archive folder and remove files older than 30 days
copy %DRIVELETTER%:\_backups_mantis\backups\* %ARCHIVE_DIR%
ForFiles /p %ARCHIVE_DIR% /s /d -30 /c "cmd /c del @file"
exit