aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/backup-restore/backup.c
blob: dab7bbd35a5f9e435c08511e0471bf3b526dfd79 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>

#include <glib/gi18n.h>
#include <libgnome/gnome-util.h>

#define EVOLUTION "evolution-" BASE_VERSION
#define EVOLUTION_DIR "~/.evolution/"
#define EVOLUTION_DIR_BACKUP "~/.evolution-old/"
#define GCONF_DUMP_FILE "backup-restore-gconf.xml"
#define GCONF_DUMP_PATH EVOLUTION_DIR GCONF_DUMP_FILE
#define GCONF_DIR "/apps/evolution"
#define ARCHIVE_NAME "evolution-backup.tar.gz"

static gboolean backup_op = FALSE;
static gboolean restore_op = FALSE;
static gboolean check_op = FALSE;
static gboolean restart_arg = FALSE;

#define d(x) x

/* #define s(x) system (x) */
#define s(x) G_STMT_START { g_message (x); system (x); } G_STMT_END

static void
backup (const char *filename) 
{
    char *command;
    
    /* FIXME Will the versioned setting always work? */
    s (EVOLUTION " --force-shutdown");

    s ("gconftool-2 --dump " GCONF_DIR " > " GCONF_DUMP_PATH);

    /* FIXME stay on this file system ,other options?" */
    /* FIXME compression type?" */
    /* FIXME date/time stamp?" */
    /* FIXME archive location?" */
    command = g_strdup_printf ("cd ~ && tar zpcf %s .evolution", filename);
    s (command);
    g_free (command);

    if (restart_arg)
        s (EVOLUTION);
}

static void
restore (const char *filename) 
{
    char *command;
    
    /* FIXME Will the versioned setting always work? */
    s (EVOLUTION " --force-shutdown");

    s ("mv " EVOLUTION_DIR " " EVOLUTION_DIR_BACKUP);

    command = g_strdup_printf ("cd ~ && tar zxf %s", filename);
    s (command);
    g_free (command);

    s ("gconftool-2 --load " GCONF_DUMP_PATH);
    s ("rm -rf " GCONF_DUMP_PATH);
    s ("rm -rf " EVOLUTION_DIR_BACKUP);

    if (restart_arg)
        s (EVOLUTION);
}

static void
check (const char *filename) 
{
    char *command;
    int result;

    command = g_strdup_printf ("tar ztf %s | grep -e \"^\\.evolution/$\"", filename);
    result = system (command);
    g_free (command);

    g_message ("First result %d", result);
    if (result)
        exit (result);

    command = g_strdup_printf ("tar ztf %s | grep -e \"^\\.evolution/%s$\"", filename, GCONF_DUMP_FILE);
    result = system (command);
    g_free (command);

    g_message ("Second result %d", result);

    exit (result);
}

int
main (int argc, char **argv)
{
    GValue popt_context_value = { 0, };
    GnomeProgram *program;
    poptContext popt_context;
    const char **args;

    struct poptOption options[] = {
        { "backup", '\0', POPT_ARG_NONE, &backup_op, 0, 
          N_("Backup Evolution directory"), NULL },
        { "restore", '\0', POPT_ARG_NONE, &restore_op, 0, 
          N_("Restore Evolution directory"), NULL },
        { "check", '\0', POPT_ARG_NONE, &check_op, 0, 
          N_("Check Evolution archive"), NULL },
        { "restart", '\0', POPT_ARG_NONE, &restart_arg, 0, 
          N_("Restart Evolution"), NULL },
        { NULL, '\0', 0, NULL, 0, NULL, NULL }
    };

    bindtextdomain (GETTEXT_PACKAGE, EVOLUTION_LOCALEDIR);
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    textdomain (GETTEXT_PACKAGE);

    program = gnome_program_init (PACKAGE, VERSION, LIBGNOME_MODULE, argc, argv, 
                      GNOME_PROGRAM_STANDARD_PROPERTIES,
                      GNOME_PARAM_POPT_TABLE, options,
                      NULL);

    g_value_init (&popt_context_value, G_TYPE_POINTER);
    g_object_get_property (G_OBJECT (program), GNOME_PARAM_POPT_CONTEXT, &popt_context_value);
    popt_context = g_value_get_pointer (&popt_context_value);
    args = poptGetArgs (popt_context);

    if (args != NULL) {
        const char **p;
        
        for (p = args; *p != NULL; p++) {
            if (backup_op) {
                d(g_message ("Backing up to %s", (char *) *p));
                backup ((char *) *p);
            } else if (restore_op) {
                d(g_message ("Restoring from %s", (char *) *p));
                restore ((char *) *p);
            } else if (check_op) {
                d(g_message ("Checking %s", (char *) *p));
                check ((char *) *p);            
            }
        }
    }

    g_value_unset (&popt_context_value);
    
    return 0;
}