aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/fossil/dump.c
blob: 4ad4f469dbd8efb3ef463ea673ffb7a1429ab5ee (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
/*
 * Clumsy hack to take snapshots and dumps.
 */
#include <u.h>
#include <libc.h>

void
usage(void)
{
	fprint(2, "usage: fossil/dump [-i snap-interval] [-n name] fscons /n/fossil\n");
	exits("usage");
}

char*
snapnow(void)
{
	Tm t;
	static char buf[100];

	t = *localtime(time(0)-5*60*60);	/* take dumps at 5:00 am */

	sprint(buf, "archive/%d/%02d%02d", t.year+1900, t.mon+1, t.mday);
	return buf;
}

void
main(int argc, char **argv)
{
	int onlyarchive, cons, s;
	ulong t, i;
	char *name;

	name = "main";
	s = 0;
	onlyarchive = 0;
	i = 60*60;		/* one hour */
	ARGBEGIN{
	case 'i':
		i = atoi(EARGF(usage()));
		if(i == 0){
			onlyarchive = 1;
			i = 60*60;
		}
		break;
	case 'n':
		name = EARGF(usage());
		break;
	case 's':
		s = atoi(EARGF(usage()));
		break;
	}ARGEND

	if(argc != 2)
		usage();

	if((cons = open(argv[0], OWRITE)) < 0)
		sysfatal("open %s: %r", argv[0]);

	if(chdir(argv[1]) < 0)
		sysfatal("chdir %s: %r", argv[1]);

	rfork(RFNOTEG);
	switch(fork()){
	case -1:
		sysfatal("fork: %r");
	case 0:
		break;
	default:
		exits(0);
	}

	/*
	 * pause at boot time to let clock stabilize.
	 */
	if(s)
		sleep(s*1000);

	for(;;){
		if(access(snapnow(), AEXIST) < 0)
			fprint(cons, "\nfsys %s snap -a\n", name);
		t = time(0);
		sleep((i - t%i)*1000+200);
		if(!onlyarchive)
			fprint(cons, "\nfsys %s snap\n", name);
	}
}