开发者

Bash script to verify backup folder existence

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.

Examples:

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-11610563189691开发者_如何学JAVA354026753564484829291576
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?


120 = 2h:

#!/bin/bash

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

[ "$ok" == ok ] ||
  printf >&2 'no recent backup found\n'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜