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-deleted.diff
19 patch -p1 <patches/backup-dir-dels.diff
20 ./configure (optional if already run)
23 based-on: patch/master/backup-deleted
24 diff --git a/backup.c b/backup.c
27 @@ -29,25 +29,32 @@ extern int preserve_specials;
28 extern int preserve_links;
29 extern int safe_symlinks;
30 extern int backup_dir_len;
31 +extern int backup_dir_dels_len;
32 extern unsigned int backup_dir_remainder;
33 +extern unsigned int backup_dir_dels_remainder;
34 extern char backup_dir_buf[MAXPATHLEN];
35 +extern char backup_dir_dels_buf[MAXPATHLEN];
36 extern char *backup_suffix;
37 +extern char *backup_suffix_dels;
38 extern char *backup_dir;
39 +extern char *backup_dir_dels;
41 +static BOOL deleting;
43 /* Returns -1 on error, 0 on missing dir, and 1 on present dir. */
44 -static int validate_backup_dir(void)
45 +static int validate_backup_dir(char *buf)
49 - if (do_lstat(backup_dir_buf, &st) < 0) {
50 + if (do_lstat(buf, &st) < 0) {
53 - rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
54 + rsyserr(FERROR, errno, "backup lstat %s failed", buf);
57 if (!S_ISDIR(st.st_mode)) {
58 int flags = get_del_for_flag(st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
59 - if (delete_item(backup_dir_buf, st.st_mode, flags) == 0)
60 + if (delete_item(buf, st.st_mode, flags) == 0)
64 @@ -58,20 +65,20 @@ static int validate_backup_dir(void)
65 * backup_dir_buf. Any new directories (compared to the prior backup
66 * path) are ensured to exist as directories, replacing anything else
67 * that may be in the way (e.g. a symlink). */
68 -static BOOL copy_valid_path(const char *fname)
69 +static BOOL copy_valid_path(const char *fname, char *buf, int prefix_len, unsigned int remainder, const char *suffix)
75 - char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
76 + char *b, *rel = buf + prefix_len, *name = rel;
78 for (f = fname, b = rel; *f && *f == *b; f++, b++) {
83 - if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
84 + if (stringjoin(rel, remainder, fname, suffix, NULL) >= remainder) {
85 rprintf(FERROR, "backup filename too long\n");
88 @@ -82,7 +89,7 @@ static BOOL copy_valid_path(const char *fname)
92 - val = validate_backup_dir();
93 + val = validate_backup_dir(buf);
97 @@ -98,9 +105,9 @@ static BOOL copy_valid_path(const char *fname)
98 for ( ; b; name = b + 1, b = strchr(name, '/')) {
101 - while (do_mkdir(backup_dir_buf, ACCESSPERMS) < 0) {
102 + while (do_mkdir(buf, ACCESSPERMS) < 0) {
103 if (errno == EEXIST) {
104 - val = validate_backup_dir();
105 + val = validate_backup_dir(buf);
109 @@ -134,7 +141,7 @@ static BOOL copy_valid_path(const char *fname)
113 - set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
114 + set_file_attrs(buf, file, NULL, NULL, 0);
118 @@ -156,7 +163,12 @@ static BOOL copy_valid_path(const char *fname)
119 /* Make a complete pathname for backup file and verify any new path elements. */
120 char *get_backup_name(const char *fname)
122 + char *buf = deleting ? backup_dir_dels_buf : backup_dir_buf;
123 + char *suffix = deleting ? backup_suffix_dels : backup_suffix;
126 + int prefix_len = deleting ? backup_dir_dels_len : backup_dir_len;
127 + unsigned int remainder = deleting ? backup_dir_dels_remainder : backup_dir_remainder;
128 static int initialized = 0;
131 @@ -170,14 +182,14 @@ char *get_backup_name(const char *fname)
134 /* copy fname into backup_dir_buf while validating the dirs. */
135 - if (copy_valid_path(fname))
136 - return backup_dir_buf;
137 + if (copy_valid_path(fname, buf, prefix_len, remainder, suffix))
139 /* copy_valid_path() has printed an error message. */
143 - if (stringjoin(backup_dir_buf, MAXPATHLEN, fname, backup_suffix, NULL) < MAXPATHLEN)
144 - return backup_dir_buf;
145 + if (stringjoin(backup_dir_buf, MAXPATHLEN, fname, suffix, NULL) < MAXPATHLEN)
148 rprintf(FERROR, "backup filename too long\n");
150 @@ -352,3 +364,13 @@ int make_backup(const char *fname, BOOL prefer_rename)
151 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
155 +/* backup switch routine called only when backing-up removed file */
156 +int safe_delete(const char *fname)
160 + ret = make_backup(fname, True);
164 diff --git a/delete.c b/delete.c
167 @@ -28,16 +28,23 @@ extern int max_delete;
168 extern char *backup_dir;
169 extern char *backup_suffix;
170 extern int backup_suffix_len;
171 +extern char *backup_dir_dels;
172 +extern char *backup_suffix_dels;
173 +extern int backup_suffix_dels_len;
174 extern struct stats stats;
176 int ignore_perishable = 0;
177 int non_perishable_cnt = 0;
178 int skipped_deletes = 0;
180 +/* Function now compares both backup_suffix and backup_suffix_dels. */
181 static inline int is_backup_file(char *fn)
183 int k = strlen(fn) - backup_suffix_len;
184 - return k > 0 && strcmp(fn+k, backup_suffix) == 0;
185 + if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
187 + k += backup_suffix_len - backup_suffix_dels_len;
188 + return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
191 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
192 @@ -162,9 +169,9 @@ enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
194 ok = do_rmdir(fbuf) == 0;
196 - if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir || !is_backup_file(fbuf))) {
197 + if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir_dels || !is_backup_file(fbuf))) {
198 what = "make_backup";
199 - ok = make_backup(fbuf, True);
200 + ok = safe_delete(fbuf);
203 ok = robust_unlink(fbuf) == 0;
204 diff --git a/options.c b/options.c
207 @@ -153,10 +153,14 @@ int no_detach
210 int backup_dir_len = 0;
211 +int backup_dir_dels_len = 0;
212 int backup_suffix_len;
213 +int backup_suffix_dels_len;
214 unsigned int backup_dir_remainder;
215 +unsigned int backup_dir_dels_remainder;
217 char *backup_suffix = NULL;
218 +char *backup_suffix_dels = NULL;
220 char *partial_dir = NULL;
221 char *basis_dir[MAX_BASIS_DIRS+1];
222 @@ -168,7 +172,9 @@ char *stdout_format = NULL;
223 char *password_file = NULL;
224 char *rsync_path = RSYNC_PATH;
225 char *backup_dir = NULL;
226 +char *backup_dir_dels = NULL;
227 char backup_dir_buf[MAXPATHLEN];
228 +char backup_dir_dels_buf[MAXPATHLEN];
229 char *sockopts = NULL;
230 char *usermap = NULL;
231 char *groupmap = NULL;
232 @@ -681,6 +687,8 @@ void usage(enum logcode F)
233 rprintf(F," --backup-deleted make backups only of deleted files\n");
234 rprintf(F," --backup-dir=DIR make backups into hierarchy based in DIR\n");
235 rprintf(F," --suffix=SUFFIX set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
236 + rprintf(F," --backup-dir-dels=DIR backup removed files into hierarchy based in DIR\n");
237 + rprintf(F," --suffix-dels=SUFFIX set removed-files suffix (def. --suffix w/o b-d-d)\n");
238 rprintf(F," -u, --update skip files that are newer on the receiver\n");
239 rprintf(F," --inplace update destination files in-place (SEE MAN PAGE)\n");
240 rprintf(F," --append append data onto shorter files\n");
241 @@ -999,7 +1007,9 @@ static struct poptOption long_options[] = {
242 {"backup-deleted", 0, POPT_ARG_VAL, &make_backups, 1, 0, 0 },
243 {"no-backup", 0, POPT_ARG_VAL, &make_backups, 0, 0, 0 },
244 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
245 + {"backup-dir-dels", 0, POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
246 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
247 + {"suffix-dels", 0, POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
248 {"list-only", 0, POPT_ARG_VAL, &list_only, 2, 0, 0 },
249 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
250 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
251 @@ -2125,6 +2135,8 @@ int parse_arguments(int *argc_p, const char ***argv_p)
252 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
254 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
255 + if (backup_dir_dels)
256 + backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, SP_DEFAULT);
258 if (daemon_filter_list.head && !am_sender) {
259 filter_rule_list *elp = &daemon_filter_list;
260 @@ -2146,6 +2158,14 @@ int parse_arguments(int *argc_p, const char ***argv_p)
261 if (check_filter(elp, FLOG, dir, 1) < 0)
262 goto options_rejected;
264 + /* Clean backup_dir_dels same as for backup_dir */
265 + if (backup_dir_dels) {
266 + if (!*backup_dir_dels)
267 + goto options_rejected;
268 + clean_fname(backup_dir_dels, 1);
269 + if (check_filter(elp, FLOG, backup_dir_dels, 1) < 0)
270 + goto options_rejected;
275 @@ -2157,6 +2177,20 @@ int parse_arguments(int *argc_p, const char ***argv_p)
279 + /* --suffix-dels defaults to --suffix, or empty for a client given an
280 + * explicit --backup-dir-dels (just as --suffix defaults to empty when
281 + * a --backup-dir is given). The second case does not apply to the
282 + * server for consistency with server_options, which sends --suffix-dels
283 + * to the server iff it differs from --suffix. */
284 + if (!backup_suffix_dels)
285 + backup_suffix_dels = backup_dir_dels && !am_server ? "" : backup_suffix;
286 + backup_suffix_dels_len = strlen(backup_suffix_dels);
287 + if (strchr(backup_suffix_dels, '/') != NULL) {
288 + snprintf(err_buf, sizeof err_buf,
289 + "--suffix-dels cannot contain slashes: %s\n",
290 + backup_suffix_dels);
295 while (*backup_dir == '.' && backup_dir[1] == '/')
296 @@ -2192,6 +2226,34 @@ int parse_arguments(int *argc_p, const char ***argv_p)
297 "P *%s", backup_suffix);
298 parse_filter_str(&filter_list, backup_dir_buf, rule_template(0), 0);
300 + if (backup_dir_dels) {
301 + backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
302 + backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
303 + if (backup_dir_dels_remainder < 32) {
304 + snprintf(err_buf, sizeof err_buf,
305 + "the --backup-dir-dels path is WAY too long.\n");
308 + if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
309 + backup_dir_dels_buf[backup_dir_dels_len++] = '/';
310 + backup_dir_dels_buf[backup_dir_dels_len] = '\0';
312 + if (INFO_GTE(BACKUP, 1) && !am_sender)
313 + rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
314 + } else if (backup_dir) {
315 + backup_dir_dels = backup_dir;
316 + backup_dir_dels_len = backup_dir_len;
317 + backup_dir_dels_remainder = backup_dir_remainder;
318 + strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
319 + } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
320 + snprintf(err_buf, sizeof err_buf,
321 + "--suffix-dels cannot be a null string without --backup-dir-dels\n");
323 + } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
324 + snprintf(backup_dir_dels_buf, sizeof backup_dir_dels_buf,
325 + "P *%s", backup_suffix_dels);
326 + parse_filter_str(&filter_list, backup_dir_dels_buf, rule_template(0), 0);
329 if (preserve_times) {
330 preserve_times = PRESERVE_FILE_TIMES;
331 @@ -2618,6 +2680,10 @@ void server_options(char **args, int *argc_p)
332 args[ac++] = "--backup-dir";
333 args[ac++] = backup_dir;
335 + if (backup_dir_dels && backup_dir_dels != backup_dir) {
336 + args[ac++] = "--backup-dir-dels";
337 + args[ac++] = backup_dir_dels;
340 /* Only send --suffix if it specifies a non-default value. */
341 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
342 @@ -2626,6 +2692,14 @@ void server_options(char **args, int *argc_p)
346 + /* Only send --suffix-dels if it specifies a value different from the
347 + * --suffix value, which would normally be used for deletions too. */
348 + if (strcmp(backup_suffix_dels, backup_suffix) != 0) {
349 + /* We use the following syntax to avoid weirdness with '~'. */
350 + if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
355 if (checksum_choice) {
356 if (asprintf(&arg, "--checksum-choice=%s", checksum_choice) < 0)