1
/* vi: set et sw=4 ts=4 cino=t0,(0: */
2
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4
* This file is part of libaccounts-glib
6
* Copyright (C) 2011 Nokia Corporation.
8
* Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* version 2.1 as published by the Free Software Foundation.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
37
"Backups the accounts from ~/.accounts/accounts.db into\n"
38
"~/.accounts/accounts.db.bak\n\n",
43
write_backup (sqlite3 *src, const gchar *filename)
45
sqlite3_backup *backup;
50
ret = sqlite3_open (filename, &dest);
51
if (ret != SQLITE_OK) return FALSE;
53
backup = sqlite3_backup_init (dest, "main", src, "main");
56
g_warning ("Couldn't start backup");
64
ret = sqlite3_backup_step (backup, -1);
65
if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED)
69
while ((ret == SQLITE_BUSY || ret == SQLITE_LOCKED) && n_retries < 5);
71
sqlite3_backup_finish (backup);
74
return ret == SQLITE_OK;
80
gchar *filename, *filename_bak;
84
gboolean success = FALSE;;
86
filename = g_build_filename (g_get_home_dir (),
90
filename_bak = g_strdup_printf ("%s.bak", filename);
92
g_debug ("Opening %s", filename);
97
ret = sqlite3_open (filename, &db);
98
if (ret == SQLITE_BUSY)
102
while (ret == SQLITE_BUSY && n_retries < 5);
104
if (G_UNLIKELY (ret != SQLITE_OK))
106
g_warning ("Couldn't open accounts DB: %s", sqlite3_errmsg (db));
113
ret = sqlite3_wal_checkpoint (db, NULL);
114
if (ret == SQLITE_BUSY)
118
while (ret == SQLITE_BUSY && n_retries < 5);
120
if (G_UNLIKELY (ret != SQLITE_OK))
121
g_warning ("Checkpoint failed: %s", sqlite3_errmsg (db));
123
success = write_backup (db, filename_bak);
129
g_free (filename_bak);
135
main (int argc, char **argv)
139
g_set_prgname (g_path_get_basename (argv[0]));
149
return success ? EXIT_SUCCESS : EXIT_FAILURE;