Piotr Piotr -5 years ago 156
Linux Question

Bash script to verify backup folder existence

To all the bash experts - could you point me in the right direction here, please?
I am trying to create a method that would verify my backup procedure.

There is a folder with backups (snapshots) /mnt/backup/snapshots/ that are taken daily.
Each backup is represented by a folder with a following name snapshot-YYYYMMHH-HHMMSS-TOKEN-TTTTTTTTTTTTTTTTTTTTTTTTTTTTT
where YYYYMMHH-HHMMSS represent the time when snapshot was initiated. Additionally, if the snapshot was taken correctly there will be an empty 'completed' file in the folder.


root@SPTestCache000:~# ls -l /mnt/backup/snapshots/
total 8
drwxr-xr-x 17 root root 4096 2011-10-12 09:29 snapshot-20111012-092902-TOKEN-11610563189691354026753564484829291576
drwxr-xr-x 13 root root 4096 2011-10-12 10:08 snapshot-20111012-100827-TOKEN-11610563189691354026753564484829291576
root@SPTestCache000:~# ls -l /mnt/backup/snapshots/snapshot-20111012-092902-TOKEN-11610563189691354026753564484829291576/
drwxr-xr-x 2 root root 4096 2011-10-12 09:29 SomeTest
-rw-r--r-- 1 root root 18 2011-10-12 09:29 completed

I would like to have a bash script that could be run in regular intervals (cron) and it would check the following:
- is there a snapshot folder that is NOT OLDER than X hours. If such snapshot folder exists - does it have the completed file inside?
- otherwise throw an error

Do you have any suggestions especially for how to do the NOT OLDER than X hours check?


Answer Source

120 = 2h:


  find  /mnt/backup/snapshots -maxdepth 1 -type d -name 'snapshot-*' -mmin -120 |
    while IFS= read -r; do
      [ -f "$REPLY"/completed ] && {
        echo ok

[ "$ok" == ok ] ||
  printf >&2 'no recent backup found\n'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download