1

I'm really stuck on an rsync problem and I haven't been able to crack it by reading the documentation. I want to make 7 directories, one for each day of the week, for back-ups of some folders and files from my server. The problem I'm having is rsync seems to be deleting the directories it back up the day before.

For example...

/backup/Monday will contain the files I moved over, but not the directories /backup/Tuesday/ will contain the files and the directories

I would like the Monday directory to contain all the files that existed on Monday and the Tuesday directory to contain all the files that existed on Tuesday.

Here is my script:

ssh [email protected] 'find /backup/staging/ -mtime +7 -exec rm -rf {} \;' crontab -l > $(date +%Y%m%d).crontab mysqldump -password cherry --all-databases> $(date +%Y%m%d)_cherrydb.sql rsync -a --log-file=$(date +%Y%m%d).log /var/www/admin /var/www/customers /var/www/UploadedFiles /etc/apache2/certs /etc/apache2/sites-enabled $(date +%Y%m%d).crontab $(date +%Y%m%d)_cherrydb.sql [email protected]:/backup/staging/$(date +%A)/ rsync -a $(date +%Y%m%d).log [email protected]:/backup/staging/$(date +%A)/ rm $(date +%Y%m%d).log rm $(date +%Y%m%d).crontab rm $(date +%Y%m%d)_cherrydb.sql 
1
  • 2
    Have you considered rsnapshot for "rsync plus depth" backups? Commented Dec 22, 2015 at 17:48

2 Answers 2

1

I don't know for sure but I'm guessing the problem lies in the very first line of your script. I think your intent with that is to delete the previous week's backup but that's a dangerous way to do it. Two alternative options would be to replace the first line with

ssh [email protected] 'rm -rf /backup/staging/$(date +%A)/*' 

or to remove the first line of the script and use the --delete flag on the first rsync to remove any file in the destination that's not a part of the backup set.

1
  • Thank you! I was totally hunting in the wrong place and got a little too risky with my deletion. You saved the day! Commented Dec 22, 2015 at 20:31
0

The problem is with the combination of the first line of the script and the rsync command itself. Let's take a look...

The find /backup/staging/ -mtime +7 -exec rm -rf {} \; command removes all files and entire directory trees where the file or root of the directory tree hasn't been modified for at least seven days.

You are also using rsync -a ..., which copies across modification times of files and directories.

The net result is that your find will match and remove files and directory trees that have been created at least seven days ago. Typically this will include system files such as /bin/ls and almost certainly / itself. You can check with ls -ld /, and confirm that the modification date displayed is more than a week ago.

One solution would be to use rsnapshot, which manages timed and dated backups extremely well. Another might be to use rsync -a ... --delete, but on balance I'd recommend rsnapshot.

If you really want to roll your own solution, consider something like this pair of commands:

find /backup/staging -mtime +7 \! -type d -delete find /backup/staging -depth -type d -exec rmdir {} + 2>/dev/null 

This will remove all non-directories (i.e. files, device nodes, etc.) that are older than seven days, and then remove all empty directories.

1
  • Thank you! I would not have put that combination together. I was trying to use old tricks with new things and it was a bad combination. Thank you for your in-depth answer. Commented Dec 22, 2015 at 20:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.