aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Base/Dump.php
blob: 39be0a27f5f57f46fd04c94b03ed13ec2b8d436f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace Studip\Cli\Commands\Base;

use Config;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class Dump extends Command
{
    protected static $defaultName = 'base:dump';

    protected function configure(): void
    {
        $this->setDescription('Dumping Stud.IP directory');
        $this->addArgument('path', InputArgument::REQUIRED, 'path where the backup should be saved');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $dump_dir = realpath($input->getArgument('path'));
        $prefix = Config::get()->STUDIP_INSTALLATION_ID ? Config::get()->STUDIP_INSTALLATION_ID : 'studip';
        $today = date('Ymd');

        $base_path = realpath($GLOBALS['STUDIP_BASE_PATH']);

        if (!$base_path) {
            $io->error('Stud.IP directory not found!');
            return Command::FAILURE;
        }

        $dumb_studip = $dump_dir . '/' . $prefix . '-BASE-' . $today . '.tar.gz';

        $io->info('Dumping Stud.IP directory to' . $base_path);

        $cmd = "cd $base_path && tar -czf $dumb_studip ." . ' 2>&1';

        exec($cmd, $output, $ok);

        if ($ok > 0) {
            $io->error(join("\n", array_merge([$cmd], $output)));
            return Command::FAILURE;
        }
        return Command::SUCCESS;
    }
}