1 This patches creates two new command line options as follows:
5 The backup-dir-dels and suffix-dels options give the ability to store
6 backup of removed files on the receiver in different directories or with
7 different suffix than the backup of files that have been changed but that
8 are still on the source drive. Both commands can be combined.
10 The default behaviour if one or both of the options are not specified
11 is the previous behaviour, both backups use the same directory or
16 To use this patch, run these commands for a successful build:
18 patch -p1 <patches/backup-dir-dels.diff
19 ./configure (optional if already run)
22 based-on: a01e3b490eb36ccf9e704840e1b6683dab867550
23 diff --git a/backup.c b/backup.c
26 @@ -29,29 +29,36 @@ extern int preserve_specials;
27 extern int preserve_links;
28 extern int safe_symlinks;
29 extern int backup_dir_len;
30 +extern int backup_dir_dels_len;
31 extern unsigned int backup_dir_remainder;
32 +extern unsigned int backup_dir_dels_remainder;
33 extern char backup_dir_buf[MAXPATHLEN];
34 +extern char backup_dir_dels_buf[MAXPATHLEN];
35 extern char *backup_suffix;
36 +extern char *backup_suffix_dels;
37 extern char *backup_dir;
38 +extern char *backup_dir_dels;
40 +static BOOL deleting;
42 /* Create a backup path from the given fname, putting the result into
43 * backup_dir_buf. Any new directories (compared to the prior backup
44 * path) are ensured to exist as directories, replacing anything else
45 * that may be in the way (e.g. a symlink). */
46 -static BOOL copy_valid_path(const char *fname)
47 +static BOOL copy_valid_path(const char *fname, char *buf, int prefix_len, unsigned int remainder, const char *suffix)
53 - char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
54 + char *b, *rel = buf + prefix_len, *name = rel;
56 for (f = fname, b = rel; *f && *f == *b; f++, b++) {
61 - if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
62 + if (stringjoin(rel, remainder, fname, suffix, NULL) >= remainder) {
63 rprintf(FERROR, "backup filename too long\n");
66 @@ -62,16 +69,16 @@ static BOOL copy_valid_path(const char *fname)
70 - if (do_lstat(backup_dir_buf, &sx.st) < 0) {
71 + if (do_lstat(buf, &sx.st) < 0) {
74 - rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
75 + rsyserr(FERROR, errno, "backup lstat %s failed", buf);
79 if (!S_ISDIR(sx.st.st_mode)) {
80 flags = get_del_for_flag(sx.st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
81 - if (delete_item(backup_dir_buf, sx.st.st_mode, flags) == 0)
82 + if (delete_item(buf, sx.st.st_mode, flags) == 0)
86 @@ -85,8 +92,8 @@ static BOOL copy_valid_path(const char *fname)
87 for ( ; b; name = b + 1, b = strchr(name, '/')) {
90 - if (mkdir_defmode(backup_dir_buf) < 0) {
91 - rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf);
92 + if (mkdir_defmode(buf) < 0) {
93 + rsyserr(FERROR, errno, "backup mkdir %s failed", buf);
97 @@ -114,7 +121,7 @@ static BOOL copy_valid_path(const char *fname)
101 - set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
102 + set_file_attrs(buf, file, NULL, NULL, 0);
106 @@ -134,15 +141,20 @@ static BOOL copy_valid_path(const char *fname)
107 /* Make a complete pathname for backup file and verify any new path elements. */
108 char *get_backup_name(const char *fname)
110 + char *buf = deleting ? backup_dir_dels_buf : backup_dir_buf;
111 + char *suffix = deleting ? backup_suffix_dels : backup_suffix;
114 + int prefix_len = deleting ? backup_dir_dels_len : backup_dir_len;
115 + unsigned int remainder = deleting ? backup_dir_dels_remainder : backup_dir_remainder;
116 /* copy fname into backup_dir_buf while validating the dirs. */
117 - if (copy_valid_path(fname))
118 - return backup_dir_buf;
119 + if (copy_valid_path(fname, buf, prefix_len, remainder, suffix))
123 if (stringjoin(backup_dir_buf, MAXPATHLEN,
124 - fname, backup_suffix, NULL) < MAXPATHLEN)
125 - return backup_dir_buf;
126 + fname, suffix, NULL) < MAXPATHLEN)
130 rprintf(FERROR, "backup filename too long\n");
131 @@ -317,3 +329,13 @@ int make_backup(const char *fname, BOOL prefer_rename)
132 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
136 +/* backup switch routine called only when backing-up removed file */
137 +int safe_delete(const char *fname)
141 + ret = make_backup(fname, True);
145 diff --git a/delete.c b/delete.c
148 @@ -28,6 +28,9 @@ extern int max_delete;
149 extern char *backup_dir;
150 extern char *backup_suffix;
151 extern int backup_suffix_len;
152 +extern char *backup_dir_dels;
153 +extern char *backup_suffix_dels;
154 +extern int backup_suffix_dels_len;
155 extern uid_t our_uid;
156 extern struct stats stats;
158 @@ -35,10 +38,14 @@ int ignore_perishable = 0;
159 int non_perishable_cnt = 0;
160 int skipped_deletes = 0;
162 +/* Function now compares both backup_suffix and backup_suffix_dels. */
163 static inline int is_backup_file(char *fn)
165 int k = strlen(fn) - backup_suffix_len;
166 - return k > 0 && strcmp(fn+k, backup_suffix) == 0;
167 + if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
169 + k += backup_suffix_len - backup_suffix_dels_len;
170 + return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
173 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
174 @@ -170,9 +177,9 @@ enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
176 ok = do_rmdir(fbuf) == 0;
178 - if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir || !is_backup_file(fbuf))) {
179 + if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir_dels || !is_backup_file(fbuf))) {
180 what = "make_backup";
181 - ok = make_backup(fbuf, True);
182 + ok = safe_delete(fbuf);
185 ok = robust_unlink(fbuf) == 0;
186 diff --git a/options.c b/options.c
189 @@ -151,10 +151,14 @@ int no_detach
192 int backup_dir_len = 0;
193 +int backup_dir_dels_len = 0;
194 int backup_suffix_len;
195 +int backup_suffix_dels_len;
196 unsigned int backup_dir_remainder;
197 +unsigned int backup_dir_dels_remainder;
199 char *backup_suffix = NULL;
200 +char *backup_suffix_dels = NULL;
202 char *partial_dir = NULL;
203 char *basis_dir[MAX_BASIS_DIRS+1];
204 @@ -166,7 +170,9 @@ char *stdout_format = NULL;
205 char *password_file = NULL;
206 char *rsync_path = RSYNC_PATH;
207 char *backup_dir = NULL;
208 +char *backup_dir_dels = NULL;
209 char backup_dir_buf[MAXPATHLEN];
210 +char backup_dir_dels_buf[MAXPATHLEN];
211 char *sockopts = NULL;
212 char *usermap = NULL;
213 char *groupmap = NULL;
214 @@ -670,6 +676,8 @@ void usage(enum logcode F)
215 rprintf(F," -b, --backup make backups (see --suffix & --backup-dir)\n");
216 rprintf(F," --backup-dir=DIR make backups into hierarchy based in DIR\n");
217 rprintf(F," --suffix=SUFFIX set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
218 + rprintf(F," --backup-dir-dels=DIR backup removed files into hierarchy based in DIR\n");
219 + rprintf(F," --suffix-dels=SUFFIX set removed-files suffix (def. --suffix w/o b-d-d)\n");
220 rprintf(F," -u, --update skip files that are newer on the receiver\n");
221 rprintf(F," --inplace update destination files in-place (SEE MAN PAGE)\n");
222 rprintf(F," --append append data onto shorter files\n");
223 @@ -969,7 +977,9 @@ static struct poptOption long_options[] = {
224 {"backup", 'b', POPT_ARG_VAL, &make_backups, 1, 0, 0 },
225 {"no-backup", 0, POPT_ARG_VAL, &make_backups, 0, 0, 0 },
226 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
227 + {"backup-dir-dels", 0, POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
228 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
229 + {"suffix-dels", 0, POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
230 {"list-only", 0, POPT_ARG_VAL, &list_only, 2, 0, 0 },
231 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
232 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
233 @@ -2006,6 +2016,8 @@ int parse_arguments(int *argc_p, const char ***argv_p)
234 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
236 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
237 + if (backup_dir_dels)
238 + backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, SP_DEFAULT);
240 if (daemon_filter_list.head && !am_sender) {
241 filter_rule_list *elp = &daemon_filter_list;
242 @@ -2027,6 +2039,14 @@ int parse_arguments(int *argc_p, const char ***argv_p)
243 if (check_filter(elp, FLOG, dir, 1) < 0)
244 goto options_rejected;
246 + /* Clean backup_dir_dels same as for backup_dir */
247 + if (backup_dir_dels) {
248 + if (!*backup_dir_dels)
249 + goto options_rejected;
250 + clean_fname(backup_dir_dels, 1);
251 + if (check_filter(elp, FLOG, backup_dir_dels, 1) < 0)
252 + goto options_rejected;
257 @@ -2038,6 +2058,20 @@ int parse_arguments(int *argc_p, const char ***argv_p)
261 + /* --suffix-dels defaults to --suffix, or empty for a client given an
262 + * explicit --backup-dir-dels (just as --suffix defaults to empty when
263 + * a --backup-dir is given). The second case does not apply to the
264 + * server for consistency with server_options, which sends --suffix-dels
265 + * to the server iff it differs from --suffix. */
266 + if (!backup_suffix_dels)
267 + backup_suffix_dels = backup_dir_dels && !am_server ? "" : backup_suffix;
268 + backup_suffix_dels_len = strlen(backup_suffix_dels);
269 + if (strchr(backup_suffix_dels, '/') != NULL) {
270 + snprintf(err_buf, sizeof err_buf,
271 + "--suffix-dels cannot contain slashes: %s\n",
272 + backup_suffix_dels);
276 while (*backup_dir == '.' && backup_dir[1] == '/')
278 @@ -2071,6 +2105,34 @@ int parse_arguments(int *argc_p, const char ***argv_p)
279 "P *%s", backup_suffix);
280 parse_filter_str(&filter_list, backup_dir_buf, rule_template(0), 0);
282 + if (backup_dir_dels) {
283 + backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
284 + backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
285 + if (backup_dir_dels_remainder < 32) {
286 + snprintf(err_buf, sizeof err_buf,
287 + "the --backup-dir-dels path is WAY too long.\n");
290 + if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
291 + backup_dir_dels_buf[backup_dir_dels_len++] = '/';
292 + backup_dir_dels_buf[backup_dir_dels_len] = '\0';
294 + if (INFO_GTE(BACKUP, 1) && !am_sender)
295 + rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
296 + } else if (backup_dir) {
297 + backup_dir_dels = backup_dir;
298 + backup_dir_dels_len = backup_dir_len;
299 + backup_dir_dels_remainder = backup_dir_remainder;
300 + strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
301 + } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
302 + snprintf(err_buf, sizeof err_buf,
303 + "--suffix-dels cannot be a null string without --backup-dir-dels\n");
305 + } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
306 + snprintf(backup_dir_dels_buf, sizeof backup_dir_dels_buf,
307 + "P *%s", backup_suffix_dels);
308 + parse_rule(&filter_list, backup_dir_dels_buf, 0, 0);
311 if (make_backups && !backup_dir) {
312 omit_dir_times = 0; /* Implied, so avoid -O to sender. */
313 @@ -2484,6 +2546,10 @@ void server_options(char **args, int *argc_p)
314 args[ac++] = "--backup-dir";
315 args[ac++] = backup_dir;
317 + if (backup_dir_dels && backup_dir_dels != backup_dir) {
318 + args[ac++] = "--backup-dir-dels";
319 + args[ac++] = backup_dir_dels;
322 /* Only send --suffix if it specifies a non-default value. */
323 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
324 @@ -2492,7 +2558,14 @@ void server_options(char **args, int *argc_p)
329 + /* Only send --suffix-dels if it specifies a value different from the
330 + * --suffix value, which would normally be used for deletions too. */
331 + if (strcmp(backup_suffix_dels, backup_suffix) != 0) {
332 + /* We use the following syntax to avoid weirdness with '~'. */
333 + if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
338 if (max_delete > 0) {
339 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)