kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

linux rsync 备份静态文件脚本

mysqlbak.sh

1
2
3
php rsync.php u8 2023-01-01

php rsync.php aps 2023-01-01

rsync.php

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
137
138
139
<?php

function sDir(&$fpa, &$dones, $logs, $ptime, $dir, $pre = 0)
{
	if (!is_dir($dir))
		return;

	// 类型 大小 时间 是否gzip 文件名或目录名
	$path = $dir;
	if (!isset($dones[$path])) {

		$tp = is_link($path) ? 'dir_link' : 'dir';
		$s = filesize($path);
		$t = filectime($path);
		$gz = 'no';

		echo "同步 {$tp}: {$path}\n";
		exec("echo 同步 {$tp}: {$path} >> {$logs}");

		exec("rsync -avd --max-size 0 {$path}/. root@192.168.1.2:/home/bak/rsync_178/{$path} >> {$logs} 2>&1");
		fputcsv($fpa, array(date('Y-m-d_H:i:s'), $tp, $s, $t, $gz, $path), "\t");
		$dones[$path] = array('', $tp, $s, $t, $gz, $path);
	}
	if (is_link($path))
		return;

	$handle = opendir($dir);
	while ($file = readdir($handle)) {
		if ($file == '.' || $file == '..')
			continue;

		$path = $dir . '/' . $file;

		$tp = '';
		$s = filesize($path);
		$t = filemtime($path);
		$gz = 'no';

		if (is_dir($path)) {

			sDir($fpa, $dones, $logs, $ptime, $path, $pre + 8);

		} elseif (is_link($path)) {

			$tp = 'link';
			echo "同步 {$tp}: {$path}\n";
			exec("echo 同步 {$tp}: {$path} >> {$logs}");

			exec("rsync -akv {$path} root@192.168.1.2:/home/bak/rsync_178/{$path} >> {$logs} 2>&1");
			fputcsv($fpa, array(date('Y-m-d_H:i:s'), $tp, $s, $t, $gz, $path), "\t");
			$dones[$path] = array('', $tp, $s, $t, $gz, $path);

		} elseif (isset($dones[$path])) {

			if ($s >= 100 and $t < $ptime and $dones[$path][1] == 'file') {
				$suf = substr($path, -3);
				$retval1 = $retval2 = '';
				if ($suf != '.gz') {

					$spath = str_replace(' ', '\\ ', str_replace(')', '\\)', str_replace('(', '\\(', $path)));

					exec("gzip {$spath}", $output, $retval1);
					$tp = 'gz';
					$gz = 'yes' . $retval1 . $retval2;
					fputcsv($fpa, array(date('Y-m-d_H:i:s'), $tp, $s, $t, $gz, $path), "\t");

					echo "{$dones[$path][0]} 已经同步, gzip压缩: {$path}\n";
				}
			}

		//    if ($tp == '')
		//        echo "{$dones[$path][0]} 已经同步: {$path}\n";

		} else {

			$spath = str_replace(' ', '\\ ', str_replace(')', '\\)', str_replace('(', '\\(', $path)));

			$tp = 'file';
			echo "同步 {$tp}: {$path}\n";
			exec("echo 同步 {$tp}: {$spath} >> {$logs}");

			$suf = substr($path, -3);
			$addsuf = ($suf != '.gz') ? '.gz' : '';
			$retval1 = $retval2 = '';

			if ($s < 100) {
				exec("rsync -av {$spath} root@192.168.1.2:'/home/bak/rsync_178/{$spath}' >> {$logs} 2>&1");
			} elseif ($t < $ptime) {
				$gz = 'yes';
				if ($suf != '.gz')
					exec("gzip {$spath}", $output, $retval1);
				exec("rsync -av {$spath}{$addsuf} root@192.168.1.2:'/home/bak/rsync_178/{$spath}{$addsuf}' >> {$logs} 2>&1");
			} else {
				$gz = 'yes';
				if ($suf != '.gz')
					exec("gzip {$spath}", $output, $retval1);
				exec("rsync -av {$spath}{$addsuf} root@192.168.1.2:'/home/bak/rsync_178/{$spath}{$addsuf}' >> {$logs} 2>&1");
				if ($suf != '.gz')
					exec("gunzip {$spath}{$addsuf}", $output, $retval2);
			}
			$gz .= $retval1 . $retval2;
			fputcsv($fpa, array(date('Y-m-d_H:i:s'), $tp, $s, $t, $gz, $path), "\t");
			$dones[$path] = array('', $tp, $s, $t, $gz, $path);
		}
	}
	closedir($handle);
}

if ($argc != 3) {
	echo "run: {$argv[0]} dir 2023-01-01\n";
	exit(-1);
}

$dir = $argv[1];
$ptime = strtotime($argv[2]);


$files = "rsync_{$dir}.files";
$logs = "rsync_{$dir}.log";


// 读取已经同步的列表
$dones = array();
if (file_exists($files)) {
	$fp = fopen($files, 'r');
	while ($line = fgetcsv($fp, 0, "\t")) {
		if ($line[1] == 'gz')
			continue;
		$file = $line[5];
		$dones[$file] = $line;
		$dones[$file . '.gz'] = $line;
	}
	fclose($fp);
}


$fpa = fopen($files, 'a');
sDir($fpa, $dones, $logs, $ptime, $dir);
fclose($fpa);

tools, command

« linux rsync命令 PHP 实现文件夹遍历 »