-1

We have a server that runs an application that is used internally. The application has a utility that creates its own backups every 24 hours.

Backups are stored in: /var/application/application-data/exports

We have a NFS mount under /mnt/AppDataBkp

I would like to set up a bash script to do the following:

  1. On a schedule (cron, every 24hrs), it will look for any .zip files in var/application/application-data/exports older than 7 days and remove them.

  2. Then, it will copy whatever it is left in that directory to /mnt/AppDataBkp

  3. Optionally, it would be nice if it could also remove any files/backups from /mnt/AppDataBkp older than 7 days.

So far, I've come up with this which is supposed to find files older than 7 days and delete them:

#!/bin/bash

find var/application/application-data/exports -name "*.zip" -type f -mtime +7 -exec rm {} \;
rsync -rlptgoD /var/application/application-data/exports /mnt/AppDataBkp &&
find /mnt/AppDataBkp -name "*.zip" -type f -mtime +7 -exec rm {} \;

Any help would be much appreciated.

  • How does this fail to achieve what you want ? – Iain Apr 27 '16 at 19:47
  • I think it should work, but I wanted to confirm. Also, is there an option/switch for rsync when it's done copying over stuff from source to automatically delete files on the destination which are no longer in the source folder? For example if i remove text1.txt from the source folder, can I get rsync to automatically remove it at the destination next time it runs? That way I don't have to run the find command again. – sonicc Apr 27 '16 at 20:38
  • You need to test it rather than getting someone to tell you it'll work. You may want to look into the touch command. While you're at it look at the documentation for rsync - you'll find something usefull there too. – Iain Apr 27 '16 at 21:27
-1

As simple as:

#!/bin/bash
echo ----Starting Backup----
echo Start Time: $(date +%m-%d-%Y) $(date +%H-%M)
echo ------------------------
echo Deleting if older than 7 days
echo ------------------------
find /var/application/application-data/exports/*.zip -mtime +7 -exec rm {} \;
echo ------------------------
echo Sending to AppDataBkp
echo ------------------------
rsync -rlptgoD /var/application/application-data/exports /mnt/AppDataBkp
echo ------------------------
echo Deleting if older than 7 days in AppDataBkp
echo ------------------------
find /mnt/AppDataBkp/*.zip -mtime +7 -exec rm {} \;
echo ------------------------
echo ----Backup Complete----
1

My help would be to use -delete instead of the exec rm ...

  • Any particular reason? – sonicc Apr 27 '16 at 20:03

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.