Advertisement
Guest User

Untitled

a guest
Jul 8th, 2012
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # btrfs-snap - make periodic snapshots of btrfs filesystem
  4. #
  5. # Copyright (C) 2010 Birger Monsen birger@birger.sh
  6. #
  7. # This program is distributed under the GNU General Public License
  8. # http://www.gnu.org/licenses/gpl.txt
  9. #
  10.  
  11. LOG_FACILITY=local0
  12. VERSION="1.0"
  13. prog=${0##*/}
  14.  
  15. USAGE="Usage: ${prog} -h for usage help
  16. ${prog} -V for version
  17. ${prog} <mountpoint> <prefix> <count>"
  18. SYNOPSIS="${prog} <mountpoint> <prefix> <count>
  19. <mountpoint> is the mountpoint of the btrfs file system to make a
  20. snapshot of
  21. <prefix> is the prefix to be used in the name of the snapshot.
  22. E.g. hourly, daily, weekly...
  23. <count> The number of snapshots with the given prefix to keep.
  24.  
  25. btrfs-snap / hourly 24
  26. would make a snapshot in /.snapshot called hourly_<date>_<time>
  27. where <date> is on the form YYYY-MM-DD and <time> is on the form
  28. HH:MM:SS. This format makes shure snapshot names sort correctly in
  29. cronological order even when sorted alphabetically. The 24 newest
  30. snapshots matching the prefix are kept around. The rest are deleted.
  31.  
  32. Snapshots are always created in a directory called .snapshot at the
  33. top level of the given mount point.
  34.  
  35. Example usage for a system with 2 btrfs file systems mounted as
  36. / and /home (remember to make these scripts executable):
  37.  
  38. /etc/cron.hourly/btrfs-snap
  39.  
  40. #!/bin/bash
  41. ${0} / hourly 24
  42. ${0} /home hourly 24
  43.  
  44. /etc/cron.daily/btrfs-snap
  45.  
  46. #!/bin/bash
  47. ${0} / daily 7
  48. ${0} /home daily 7
  49.  
  50. /etc/cron.weekly/btrfs-snap
  51.  
  52. #!/bin/bash
  53. ${0} /home weekly 4
  54.  
  55. Remember that at the moment snapshots cannot be made read-only, but you
  56. should not modify the snapshots created by this script, as they will
  57. get deleted without any notice. To restore a file, just copy it back from
  58. a snapshot to the main branch."
  59.  
  60. while getopts "hV" arg; do
  61. case "${arg}" in
  62. h )
  63. echo "$SYNOPSIS"
  64. exit 0
  65. ;;
  66. V )
  67. echo "${prog} Version ${VERSION}"
  68. exit 0
  69. ;;
  70. * )
  71. echo "$USAGE"
  72. exit 1
  73. ;;
  74. esac
  75. done
  76.  
  77.  
  78. if [ $# -ne 3 ] ; then
  79. echo "$USAGE"
  80. exit 1
  81. fi
  82.  
  83. if [ -f /etc/sysconfig/btrfs-snap ] ; then
  84. . /etc/sysconfig/btrfs-snap
  85. fi
  86.  
  87. mp=$1
  88. pf=$2
  89. cnt=$(( $3+1 ))
  90.  
  91. mount -t btrfs | cut -d " " -f 3 | grep "^${mp}$" > /dev/null
  92. if [ $? -ne 0 ] ; then
  93. echo "Error: ${mp} is not a btrfs mountpoint"
  94. exit 1
  95. fi
  96.  
  97. if [ ! -d "${mp}/.snapshot" ]; then
  98. logger -p ${LOG_FACILITY}.info -t ${prog} "Creating ${mp}/.snapshot"
  99. mkdir "${mp}/.snapshot"
  100. fi
  101.  
  102. dt=`date +'%Y-%m-%d_%H:%M:%S'`
  103. out=`/sbin/btrfs subvol snapshot ${mp} ${mp}/.snapshot/${pf}_${dt} 2>&1`
  104. if [ $? -eq 0 ] ; then
  105. logger -p ${LOG_FACILITY}.info -t ${prog} "${out}"
  106. else
  107. logger -p ${LOG_FACILITY}.err -t ${prog} "${out}"
  108. fi
  109.  
  110. ls -dr ${mp}/.snapshot/${pf}_* | tail -n +${cnt} | while read snap ; do
  111. out=`/sbin/btrfs subvolume delete ${snap} 2>&1`
  112. if [ $? -eq 0 ] ; then
  113. logger -p ${LOG_FACILITY}.info -t ${prog} "${out}"
  114. else
  115. logger -p ${LOG_FACILITY}.err -t ${prog} "${out}"
  116. fi
  117. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement