dump.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Clumsy hack to take snapshots and dumps.
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. void
  15. usage(void)
  16. {
  17. fprint(2, "usage: fossil/dump [-i snap-interval] [-n name] fscons /n/fossil\n");
  18. exits("usage");
  19. }
  20. char*
  21. snapnow(void)
  22. {
  23. Tm t;
  24. static char buf[100];
  25. t = *localtime(time(0)-5*60*60); /* take dumps at 5:00 am */
  26. sprint(buf, "archive/%d/%02d%02d", t.year+1900, t.mon+1, t.mday);
  27. return buf;
  28. }
  29. void
  30. main(int argc, char **argv)
  31. {
  32. int onlyarchive, cons, s;
  33. uint32_t t, i;
  34. char *name;
  35. name = "main";
  36. s = 0;
  37. onlyarchive = 0;
  38. i = 60*60; /* one hour */
  39. ARGBEGIN{
  40. case 'i':
  41. i = atoi(EARGF(usage()));
  42. if(i == 0){
  43. onlyarchive = 1;
  44. i = 60*60;
  45. }
  46. break;
  47. case 'n':
  48. name = EARGF(usage());
  49. break;
  50. case 's':
  51. s = atoi(EARGF(usage()));
  52. break;
  53. }ARGEND
  54. if(argc != 2)
  55. usage();
  56. if((cons = open(argv[0], OWRITE)) < 0)
  57. sysfatal("open %s: %r", argv[0]);
  58. if(chdir(argv[1]) < 0)
  59. sysfatal("chdir %s: %r", argv[1]);
  60. rfork(RFNOTEG);
  61. switch(fork()){
  62. case -1:
  63. sysfatal("fork: %r");
  64. case 0:
  65. break;
  66. default:
  67. exits(0);
  68. }
  69. /*
  70. * pause at boot time to let clock stabilize.
  71. */
  72. if(s)
  73. sleep(s*1000);
  74. for(;;){
  75. if(access(snapnow(), AEXIST) < 0)
  76. fprint(cons, "\nfsys %s snap -a\n", name);
  77. t = time(0);
  78. sleep((i - t%i)*1000+200);
  79. if(!onlyarchive)
  80. fprint(cons, "\nfsys %s snap\n", name);
  81. }
  82. }