Advertisement
KervyN

restic-bachup.sh

Mar 23rd, 2024 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.81 KB | None | 0 0
  1. #!/bin/bash
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
  3.  
  4. # Defining variables
  5. TIMESTAMP=$(date '+%Y%m%d%H%M%S')
  6. PID=$$
  7. STATUSFILE=/var/run/restic_backup_status
  8. STDERR=/var/log/restic_backup-error.log
  9. STDOUT=/var/log/restic_backup-status.log
  10.  
  11. # What should be backed up
  12. if [ "$1" == "--wife" ]; then
  13.   TAG=wife
  14. elif [ "$1" == "--full"  ]; then
  15.   TAG=full
  16. else
  17.   TAG=kervyn
  18. fi
  19.  
  20.  
  21. # Checking if backup is already running
  22. if [ -f $STATUSFILE ]; then
  23.   echo "Backup is already running"
  24.   exit 99
  25. fi
  26.  
  27. # Write Backupinformation
  28. cat << __EOF__ > $STATUSFILE
  29. $PID
  30. Started: $TIMESTAMP
  31. __EOF__
  32.  
  33. # Creating snapshots and mount it
  34. lvcreate -L100G -s -n $TIMESTAMP-snap-lvsrv /dev/vg0/lvsrv >/dev/null
  35. mkdir -p /mnt/lv-snapshots/snap-lvsrv
  36. mount -o ro /dev/vg0/$TIMESTAMP-snap-lvsrv /mnt/lv-snapshots/snap-lvsrv
  37.  
  38. lvcreate -L100G -s -n $TIMESTAMP-snap-lvtimemashine /dev/vg0/lvtimemashine >/dev/null
  39. mkdir -p /mnt/lv-snapshots/snap-lvtimemashine
  40. mount -o ro /dev/vg0/$TIMESTAMP-snap-lvtimemashine /mnt/lv-snapshots/snap-lvtimemashine
  41.  
  42. # Backup
  43. /opt/restic --repo rclone:hetzner-storagebox-europa: \
  44.   --tag ${TAG} --json --compression=max \
  45.   --password-file=/etc/restic/resticpw backup \
  46.   --files-from=/etc/restic/${TAG}.include \
  47.   --exclude-file=/etc/restic/${TAG}.exclude > $STDOUT 2> $STDERR
  48.  
  49. # Unmouning and removing snapshots
  50. umount /mnt/lv-snapshots/snap-lvsrv
  51. lvremove -f /dev/vg0/$TIMESTAMP-snap-lvsrv >/dev/null
  52. rmdir /mnt/lv-snapshots/snap-lvsrv
  53.  
  54. umount /mnt/lv-snapshots/snap-lvtimemashine
  55. lvremove -f /dev/vg0/$TIMESTAMP-snap-lvtimemashine >/dev/null
  56. rmdir /mnt/lv-snapshots/snap-lvtimemashine
  57.  
  58. # removing pid file
  59. rm $STATUSFILE
  60.  
  61. # send statistics to zabbix
  62. grep -F '"message_type":"summary",' $STDOUT | /etc/restic/restic_stats_to_zabbix.rb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement