aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/DB/Dump.php
blob: e2a8a2f8a27213b4be4ee19c304276195a55d0bf (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace Studip\Cli\Commands\DB;

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 = 'db:dump';

    protected function configure(): void
    {
        $this->setDescription('Dump the given database schema');
        $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'));
        $today = date('Ymd');
        $prefix = \Config::get()->STUDIP_INSTALLATION_ID ? \Config::get()->STUDIP_INSTALLATION_ID : 'studip';

        if (!is_writeable($dump_dir)) {
            $io->error('Directory: ' . $dump_dir . ' is not writeable!');
            return Command::FAILURE;
        }
        $dump_db_dir = $dump_dir . '/db-' . $today;
        if (!\is_dir($dump_db_dir)) {
            \mkdir($dump_db_dir);
        }
        $command = $this->getBaseDumpCommand();
        $output = [];
        $result_code = 0;

        foreach (\DBManager::get()->query('SHOW TABLES') as $tables) {
            $table = $tables[0];

            $dump_table = $dump_db_dir . '/' . $table . '-' . $today . '.sql';

            $io->writeln('<info>Dumping database table ' . $table . '</info>');

            $cmd = $command . ' ' . $table . ' > ' . $dump_table;
            $this->runCommand($cmd, $output, $result_code);

            if ($result_code > 0) {
                $io->error($this->parseOutput($cmd, $output));
                return Command::FAILURE;
            }
        }
        $dump_db = $dump_dir . '/' . $prefix . '-DB-' . $today . '.tar.gz';

        $io->writeln('<info>Packing database to ' . $dump_db . '</info>');

        $cmd = "cd $dump_db_dir && tar -czf $dump_db *";
        $this->runCommand($cmd, $output, $result_code);

        if ($result_code > 0) {
            $io->error($this->parseOutput($cmd, $output));
            return Command::FAILURE;
        }

        $cmd = "rm -rf $dump_db_dir";
        $this->runCommand($cmd, $output, $result_code);

        if ($result_code > 0) {
            $io->error($this->parseOutput($cmd, $output));
            return Command::FAILURE;
        }

        return Command::SUCCESS;
    }

    /**
     * Get the base dump command arguments for MySQL as a string.
     *
     * @return string
     */
    private function getBaseDumpCommand(): string
    {
        $command =
            'mysqldump ' .
            $this->getConnectionString() .
            ' --skip-add-locks --skip-comments --skip-set-charset --tz-utc';

        if (!\DBManager::get()->isMariaDB()) {
            $command .= ' --column-statistics=0 --set-gtid-purged=OFF';
        }
        return $command . ' ' . $GLOBALS['DB_STUDIP_DATABASE'];
    }

    /**
     * Generate a basic connection string (--socket, --host, --port, --user, --password) for the database.
     *
     * @return string
     */
    private function getConnectionString(): string
    {
        return ' --user="' .
            $GLOBALS['DB_STUDIP_USER'] .
            '"  --password="' .
            $GLOBALS['DB_STUDIP_PASSWORD'] .
            '"  --host="' .
            $GLOBALS['DB_STUDIP_HOST'] .
            '"';
    }

    /**
     * Execute dump command
     * @param string $cmd
     * @param array $output
     * @param int $result_code
     */
    private function runCommand(string $cmd, array &$output, int &$result_code)
    {
        exec($cmd . ' 2>&1', $output, $result_code);
    }

    /**
     * Parse output of exec()
     * @param string $cmd
     * @param array $output
     * @return string
     */
    private function parseOutput(string &$cmd, array &$output): string
    {
        $result = join('\n', array_merge([$cmd], $output));
        $cmd = '';
        $output = [];
        return $result;
    }
}