oshunluvr

autosnapshot

Nov 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Log the script activity
  4. exec 1> >(logger -s -t $(basename $0)) 2>&1
  5.  
  6. # Set the variables
  7. # Distro subvolume name to snapshot and backup;
  8. distro="@KDEneon"
  9.  
  10. # Source and backup parent file system mount points and devices with their UUIDs;
  11. source_mount="/subvol/"
  12. backup1_mount="/backup/"
  13. backup2_mount="/backup2/"
  14. source_device="/dev/sdc3"
  15. backup1_device="/dev/sda4"
  16. backup2_device="/dev/sdb3"
  17. source_UUID=`lsblk -no UUID "$source_device"`
  18. backup1_UUID=`lsblk -no UUID "$backup1_device"`
  19. backup2_UUID=`lsblk -no UUID "$backup2_device"`
  20.  
  21. # Dates to do things;
  22. today=_`date +%y%m%d`
  23. ## Age in days to remove snapshots.
  24. snapremoveday=_`date +%y%m%d --date="-3 day"`
  25. ## Age in days to keep backups.
  26. backupkeepday=_`date +%y%m%d --date="-7 day"`
  27. ## Age in days to remove backups.
  28. backupremoveday=_`date +%y%m%d --date="-14 day"`
  29.  
  30. # Assemble the above variables into single variables;
  31. ## The source is the full path and distro subvolume name combined;
  32. source="$source_mount""$distro"
  33. ## Snapshot name is the source with today's date appended;
  34. newsnap="$source""$today"
  35. ## Snapshot to be deleted is the source with the snapshot removal date appended;
  36. oldsnap="$source""$snapremoveday"
  37. ## Name for read-only snapshot used for backups is the source with _ro and today's date appended;
  38. newbackup="$distro"_ro"$today"
  39. ## Name for the oldest backup is the subvolume name with the backup removal date appended;
  40. oldbackup="$distro""$backupremoveday"
  41.  
  42. # Verify the parent file system is mounted and mount it if not;
  43. if ! mountpoint -q "$source_mount"
  44. then
  45. mount "$source_device" "$source_mount"
  46. fi
  47.  
  48. # Delete the oldest snapshot;
  49. if [ -d "$oldsnap" ]
  50. then
  51. btrfs su de -c "$oldsnap"
  52. fi
  53.  
  54. # Take a new read-write snapshot;
  55. btrfs su sn "$source" "$newsnap"
  56. touch "$newsnap"
  57.  
  58. # For backups; Take a read-only snapshot and send it to the backup
  59. # locations, make backup snapshots bootable, and delete oldest backups.
  60.  
  61. # Verify it's backup day;
  62. if [ `date +%w` = 0 ]
  63. then
  64.  
  65. # Take a read-only snapshot
  66. btrfs su sn -r "$source" "$source_mount""$newbackup"
  67.  
  68. # Verify the backup file systems are mounted and mount them it if not;
  69. if ! mountpoint -q "$backup1_mount"
  70. then
  71. mount "$backup1_device" "$backup1_mount"
  72. fi
  73. if ! mountpoint -q "$backup2_mount"
  74. then
  75. mount "$backup2_device" "$backup2_mount"
  76. fi
  77.  
  78. # Delete oldest backups;
  79. if [ -d "$backup1_mount""$oldbackup" ]
  80. then
  81. btrfs su de -c "$backup1_mount""$oldbackup"
  82. fi
  83. if [ -d "$backup2_mount""$oldbackup" ]
  84. then
  85. btrfs su de -c "$backup2_mount""$oldbackup"
  86. fi
  87.  
  88. # Rename previous week's backups by adding backupkeepday;
  89. mv "$backup1_mount""$distro" "$backup1_mount""$distro""$backupkeepday"
  90. mv "$backup2_mount""$distro" "$backup2_mount""$distro""$backupkeepday"
  91. ## The renaming prepares the oldest backup to be deleted next week and renames
  92. ## the current backup so we're ready to receive the newest backup.
  93.  
  94. # Send the read-only snapshot to the backup locations;
  95. btrfs send "$source_mount""$newbackup" | btrfs receive "$backup1_mount"
  96. btrfs send "$source_mount""$newbackup" | btrfs receive "$backup2_mount"
  97.  
  98. # Take a read-write snapshot of the received backups and using the distro name;
  99. btrfs su sn "$backup1_mount""$newbackup" "$backup1_mount""$distro"
  100. btrfs su sn "$backup2_mount""$newbackup" "$backup2_mount""$distro"
  101.  
  102. # Delete read-only snapshots because they are no longer needed;
  103. btrfs su de -c "$backup1_mount""$newbackup"
  104. btrfs su de -c "$backup2_mount""$newbackup"
  105. btrfs su de -c "$source_mount""$newbackup"
  106.  
  107. # Edit the UUIDs in grub and fstab so the backups are bootable;
  108. ### NOT YET VERIFIED AS WORKING###
  109. sed -i "s@$source_UUID@$backup1_UUID@" "$backup1_mount""$distro"/etc/fstab
  110. sed -i "s@$source_UUID@$backup2_UUID@" "$backup2_mount""$distro"/etc/fstab
  111. sed -i "s@$source_UUID@$backup1_UUID@" "$backup1_mount""$distro"/boot/grub/grub.cfg
  112. sed -i "s@$source_UUID@$backup2_UUID@" "$backup2_mount""$distro"/boot/grub/grub.cfg
  113. ## This sed command is a replace-in-place action to change the UUIDs from the source to
  114. ## the backup file systems. The goal here is to make the backups bootable.
  115. fi
  116.  
  117. # Notify of completion of tasks;
  118. # Verify it's backup day;
  119. if [ `date +%w` = 0 ]
  120. # Notify that backup tasks are complete and the old backups and snapshot are deleted;
  121. then
  122. su - stuart -c "notify-send -i package-install -t 0 'Desktop backup:' 'Backup made and sent. Old backups and snapshot removed.'"
  123. else
  124. # Notify that a snapshot has been taken and the old one deleted;
  125. su - stuart -c "notify-send -i package-install -t 0 'Desktop snapshot:' 'Daily snapshot made. Old snapshot removed.'"
  126. fi
  127.  
  128. # Script complete
  129. exit 0
Advertisement
Add Comment
Please, Sign In to add comment