ntdbbackup.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. Unix SMB/CIFS implementation.
  3. low level ntdb backup and restore utility
  4. Copyright (C) Andrew Tridgell 2002
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This program is meant for backup/restore of ntdb databases. Typical usage would be:
  18. tdbbackup *.ntdb
  19. when Samba shuts down cleanly, which will make a backup of all the local databases
  20. to *.bak files. Then on Samba startup you would use:
  21. tdbbackup -v *.ntdb
  22. and this will check the databases for corruption and if corruption is detected then
  23. the backup will be restored.
  24. You may also like to do a backup on a regular basis while Samba is
  25. running, perhaps using cron.
  26. The reason this program is needed is to cope with power failures
  27. while Samba is running. A power failure could lead to database
  28. corruption and Samba will then not start correctly.
  29. Note that many of the databases in Samba are transient and thus
  30. don't need to be backed up, so you can optimise the above a little
  31. by only running the backup on the critical databases.
  32. */
  33. #include "config.h"
  34. #include "ntdb.h"
  35. #include "private.h"
  36. #ifdef HAVE_GETOPT_H
  37. #include <getopt.h>
  38. #endif
  39. static int failed;
  40. static void ntdb_log(struct ntdb_context *ntdb,
  41. enum ntdb_log_level level,
  42. enum NTDB_ERROR ecode,
  43. const char *message,
  44. void *data)
  45. {
  46. fprintf(stderr, "%s:%s\n", ntdb_errorstr(ecode), message);
  47. }
  48. static char *add_suffix(const char *name, const char *suffix)
  49. {
  50. char *ret;
  51. int len = strlen(name) + strlen(suffix) + 1;
  52. ret = (char *)malloc(len);
  53. if (!ret) {
  54. fprintf(stderr,"Out of memory!\n");
  55. exit(1);
  56. }
  57. snprintf(ret, len, "%s%s", name, suffix);
  58. return ret;
  59. }
  60. static int copy_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
  61. {
  62. struct ntdb_context *ntdb_new = (struct ntdb_context *)state;
  63. enum NTDB_ERROR err;
  64. err = ntdb_store(ntdb_new, key, dbuf, NTDB_INSERT);
  65. if (err) {
  66. fprintf(stderr,"Failed to insert into %s: %s\n",
  67. ntdb_name(ntdb_new), ntdb_errorstr(err));
  68. failed = 1;
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. static int test_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
  74. {
  75. return 0;
  76. }
  77. /*
  78. carefully backup a ntdb, validating the contents and
  79. only doing the backup if its OK
  80. this function is also used for restore
  81. */
  82. static int backup_ntdb(const char *old_name, const char *new_name)
  83. {
  84. struct ntdb_context *ntdb;
  85. struct ntdb_context *ntdb_new;
  86. char *tmp_name;
  87. struct stat st;
  88. int count1, count2;
  89. enum NTDB_ERROR err;
  90. union ntdb_attribute log_attr;
  91. tmp_name = add_suffix(new_name, ".tmp");
  92. /* stat the old ntdb to find its permissions */
  93. if (stat(old_name, &st) != 0) {
  94. perror(old_name);
  95. free(tmp_name);
  96. return 1;
  97. }
  98. log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
  99. log_attr.base.next = NULL;
  100. log_attr.log.fn = ntdb_log;
  101. /* open the old ntdb */
  102. ntdb = ntdb_open(old_name, NTDB_DEFAULT, O_RDWR, 0, &log_attr);
  103. if (!ntdb) {
  104. printf("Failed to open %s\n", old_name);
  105. free(tmp_name);
  106. return 1;
  107. }
  108. unlink(tmp_name);
  109. ntdb_new = ntdb_open(tmp_name, NTDB_DEFAULT,
  110. O_RDWR|O_CREAT|O_EXCL, st.st_mode & 0777,
  111. &log_attr);
  112. if (!ntdb_new) {
  113. perror(tmp_name);
  114. free(tmp_name);
  115. return 1;
  116. }
  117. err = ntdb_transaction_start(ntdb);
  118. if (err) {
  119. fprintf(stderr, "Failed to start transaction on old ntdb: %s\n",
  120. ntdb_errorstr(err));
  121. ntdb_close(ntdb);
  122. ntdb_close(ntdb_new);
  123. unlink(tmp_name);
  124. free(tmp_name);
  125. return 1;
  126. }
  127. /* lock the backup ntdb so that nobody else can change it */
  128. err = ntdb_lockall(ntdb_new);
  129. if (err) {
  130. fprintf(stderr, "Failed to lock backup ntdb: %s\n",
  131. ntdb_errorstr(err));
  132. ntdb_close(ntdb);
  133. ntdb_close(ntdb_new);
  134. unlink(tmp_name);
  135. free(tmp_name);
  136. return 1;
  137. }
  138. failed = 0;
  139. /* traverse and copy */
  140. count1 = ntdb_traverse(ntdb, copy_fn, (void *)ntdb_new);
  141. if (count1 < 0 || failed) {
  142. fprintf(stderr,"failed to copy %s\n", old_name);
  143. ntdb_close(ntdb);
  144. ntdb_close(ntdb_new);
  145. unlink(tmp_name);
  146. free(tmp_name);
  147. return 1;
  148. }
  149. /* close the old ntdb */
  150. ntdb_close(ntdb);
  151. /* copy done, unlock the backup ntdb */
  152. ntdb_unlockall(ntdb_new);
  153. #ifdef HAVE_FDATASYNC
  154. if (fdatasync(ntdb_fd(ntdb_new)) != 0) {
  155. #else
  156. if (fsync(ntdb_fd(ntdb_new)) != 0) {
  157. #endif
  158. /* not fatal */
  159. fprintf(stderr, "failed to fsync backup file\n");
  160. }
  161. /* close the new ntdb and re-open read-only */
  162. ntdb_close(ntdb_new);
  163. /* we don't need the hash attr any more */
  164. log_attr.base.next = NULL;
  165. ntdb_new = ntdb_open(tmp_name, NTDB_DEFAULT, O_RDONLY, 0, &log_attr);
  166. if (!ntdb_new) {
  167. fprintf(stderr,"failed to reopen %s\n", tmp_name);
  168. unlink(tmp_name);
  169. perror(tmp_name);
  170. free(tmp_name);
  171. return 1;
  172. }
  173. /* traverse the new ntdb to confirm */
  174. count2 = ntdb_traverse(ntdb_new, test_fn, NULL);
  175. if (count2 != count1) {
  176. fprintf(stderr,"failed to copy %s\n", old_name);
  177. ntdb_close(ntdb_new);
  178. unlink(tmp_name);
  179. free(tmp_name);
  180. return 1;
  181. }
  182. /* close the new ntdb and rename it to .bak */
  183. ntdb_close(ntdb_new);
  184. if (rename(tmp_name, new_name) != 0) {
  185. perror(new_name);
  186. free(tmp_name);
  187. return 1;
  188. }
  189. free(tmp_name);
  190. return 0;
  191. }
  192. /*
  193. verify a ntdb and if it is corrupt then restore from *.bak
  194. */
  195. static int verify_ntdb(const char *fname, const char *bak_name)
  196. {
  197. struct ntdb_context *ntdb;
  198. int count = -1;
  199. union ntdb_attribute log_attr;
  200. log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
  201. log_attr.base.next = NULL;
  202. log_attr.log.fn = ntdb_log;
  203. /* open the ntdb */
  204. ntdb = ntdb_open(fname, NTDB_DEFAULT, O_RDONLY, 0, &log_attr);
  205. /* traverse the ntdb, then close it */
  206. if (ntdb) {
  207. count = ntdb_traverse(ntdb, test_fn, NULL);
  208. ntdb_close(ntdb);
  209. }
  210. /* count is < 0 means an error */
  211. if (count < 0) {
  212. printf("restoring %s\n", fname);
  213. return backup_ntdb(bak_name, fname);
  214. }
  215. printf("%s : %d records\n", fname, count);
  216. return 0;
  217. }
  218. /*
  219. see if one file is newer than another
  220. */
  221. static int file_newer(const char *fname1, const char *fname2)
  222. {
  223. struct stat st1, st2;
  224. if (stat(fname1, &st1) != 0) {
  225. return 0;
  226. }
  227. if (stat(fname2, &st2) != 0) {
  228. return 1;
  229. }
  230. return (st1.st_mtime > st2.st_mtime);
  231. }
  232. static void usage(void)
  233. {
  234. printf("Usage: ntdbbackup [options] <fname...>\n\n");
  235. printf(" -h this help message\n");
  236. printf(" -v verify mode (restore if corrupt)\n");
  237. printf(" -s suffix set the backup suffix\n");
  238. printf(" -v verify mode (restore if corrupt)\n");
  239. }
  240. int main(int argc, char *argv[])
  241. {
  242. int i;
  243. int ret = 0;
  244. int c;
  245. int verify = 0;
  246. const char *suffix = ".bak";
  247. while ((c = getopt(argc, argv, "vhs:")) != -1) {
  248. switch (c) {
  249. case 'h':
  250. usage();
  251. exit(0);
  252. case 'v':
  253. verify = 1;
  254. break;
  255. case 's':
  256. suffix = optarg;
  257. break;
  258. }
  259. }
  260. argc -= optind;
  261. argv += optind;
  262. if (argc < 1) {
  263. usage();
  264. exit(1);
  265. }
  266. for (i=0; i<argc; i++) {
  267. const char *fname = argv[i];
  268. char *bak_name;
  269. bak_name = add_suffix(fname, suffix);
  270. if (verify) {
  271. if (verify_ntdb(fname, bak_name) != 0) {
  272. ret = 1;
  273. }
  274. } else {
  275. if (file_newer(fname, bak_name) &&
  276. backup_ntdb(fname, bak_name) != 0) {
  277. ret = 1;
  278. }
  279. }
  280. free(bak_name);
  281. }
  282. return ret;
  283. }