Windows PowerShell script for archiving RedMine Turnkey Hyper-V over ssh

Initial data

Task

Get a backup of the RedMine database and files smaller than the whole Hyper-V image.

You need to know a little about PowerShell and PuTTY (although I recommend WinSCP – visually edit files and walk around the directory).

After debugging and adjusting for your additional needs (for example, exclude large Git directories), shove it into the standard scheduler Windows (do not forget about specifying the working directory).

Implementation

After the number of the item, the System in which we are performing the action.

one. Windows. Install OpenSSH.Client, execute PowerShell

Add-WindowsCapability -Online -Name OpenSSH.Client*

2. Windows. Generate shh key, execute PowerShell

cd c:Users[UserName].ssh
ssh-keygen -t rsa -b 4096 -C "your_mail@example.com"

[UserName] – the name of the current user.

Catalog c: Users [UserName] .ssh usually hidden and / or systemic. If there is no catalog, create it first.

your_mail@example.com – e-mail of the current user.

3. Redmine… Content from c: Users [UserName] .ssh id_rsa.pub append file (add new line) root / .ssh / authorized_keys

Catalog root / .ssh / authorized_keys usually hidden and / or systemic.

4. Windows. Establishing the first connection, execute PowerShell

ssh root@192.168.1.155

root – RedMine user

192.168.1.155 – IP Hyper-V where RedMine is deployed

PowerShell will ask you to accept the connection, dial yes and press Enter, if everything is done correctly, you will no longer have to enter the password when connecting via ssh.

5. Windows. Create a file StartBackUp.ps1 with content in the user’s working directory (RedMine data will be backed up to this directory)

cls
#Текущая дата
$DateStr=(Get-Date).ToString("yyyy_MM_dd_hh_mm_ss")

#Создаем (удаляя предыдущий) скрипт арихивации
Write-Host "=====================$DateStr======================"
$FileName = "BackUpScript.ps1"
if (Test-Path $FileName) 
{
  Remove-Item $FileName
}

#Формируем имя временного каталога RedMine
$BackUpPath="/var//www//"+$DateStr
Write-Host "Create: $FileName"

#Создать временный каталог для архива
"ssh root@192.168.1.155 'mkdir "+$BackUpPath+"'">>$FileName

#Архив файлов
"ssh root@192.168.1.155 'tar -cvf "+$BackUpPath+"/files.tar.gz /var/www/redmine/files'">>$FileName

#Архив Git
"ssh root@192.168.1.155 'tar -cvf "+$BackUpPath+"/git.tar.gz /srv/repos/git'">>$FileName

#Архив базы RedMine
"ssh root@192.168.1.155 '/usr/bin/mysqldump -u redmine -p9323XXXXXXX13ad redmine_production | gzip > "+$BackUpPath+"/redmineDB.gz'">>$FileName

#Запуск скрипта архивации
Write-Host "Run: $FileName"
.BackUpScript.ps1

#Копировние временного каталога RedMine в текущий рабочий каталог Windows
Write-Host "Copy dir from $BackUpPath to "(Get-Location)
scp -r root@192.168.1.155:$BackUpPath (Get-Location)

#Создаем (удаляя предыдущий) скрипт удаления временного каталога RedMine
$FileName = "DelBackUpScript.ps1"
if (Test-Path $FileName) 
{
  Remove-Item $FileName
}
#Запуск скрипта удаления
"ssh root@192.168.1.155 'rm -r "+ $BackUpPath+"'">>$FileName
Write-Host "Run: $FileName"
.DelBackUpScript.ps1

The data for the line of the RedMine database archive should be taken from /var/www/redmine/config/database.yml

6. Windows. Launch PowerShell, go to the directory with StartBackUp.ps1 and launch it for execution.

Similar Posts

Leave a Reply