-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotbackup.php
More file actions
56 lines (52 loc) · 1.75 KB
/
dotbackup.php
File metadata and controls
56 lines (52 loc) · 1.75 KB
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
<?php
include("vendor/autoload.php");
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
Sentry\init(['dsn' => getenv("SENTRY_DSN") ]);
$dotenv->required('BASE_DIR')->notempty();
$dotenv->required('DOTENV_DIR')->notempty();
$dotenv->required('BACKUP_SUBDIR');
chdir($_ENV["DOTENV_DIR"]);
exec('git pull');
if (substr($_ENV["BACKUP_SUBDIR"], -1)!="/" && strlen($_ENV["BACKUP_SUBDIR"])!=0) {
$_ENV["BACKUP_SUBDIR"].="/";
}
if (substr($_ENV["BASE_DIR"], -1)!="/") {
$_ENV["BASE_DIR"].="/";
}
if (substr($_ENV["DOTENV_DIR"], -1)!="/") {
$_ENV["DOTENV_DIR"].="/";
}
$files = explode(',', $_ENV["BACKUP_FILES"] ?? ".env");
$verbose = getenv("VERBOSE");
if ($verbose === "false") {
$verbose = false;
}
if ($verbose) {
echo "Backing up following files:\n\t".implode("\n\t", $files)."\n";
}
$it = new RecursiveDirectoryIterator($_ENV["BASE_DIR"]);
foreach (new RecursiveIteratorIterator($it) as $file) {
if (substr($file, 0, strlen($_ENV["DOTENV_DIR"]))!= $_ENV["DOTENV_DIR"]) {
if (in_array(basename($file), $files)) {
$source = $file;
$file = substr($file, strlen($_ENV["BASE_DIR"]));
if ($verbose) {
echo "found ".basename($file). " in ".dirname($file)."\n";
}
$backup_dir = $_ENV["DOTENV_DIR"].dirname($file)."/".$_ENV["BACKUP_SUBDIR"];
if (!file_exists($backup_dir)) {
mkdir($backup_dir, 0755, true);
}
$dest = $backup_dir.basename($file);
if ($verbose) {
echo "\tbacking up ".$source."\n\tto ".$dest. "\n";
}
copy($source, $dest);
}
}
}
chdir($_ENV["DOTENV_DIR"]);
exec('git add .');
exec('git commit -m "'.date("y-m-d").'"');
exec('git push');