Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Log the script activity
- exec 1> >(logger -s -t $(basename $0)) 2>&1
- # Set the variables
- # Distro subvolume name to snapshot and backup;
- distro="@KDEneon"
- # Source and backup parent file system mount points and devices with their UUIDs;
- source_mount="/subvol/"
- backup1_mount="/backup/"
- backup2_mount="/backup2/"
- source_device="/dev/sdc3"
- backup1_device="/dev/sda4"
- backup2_device="/dev/sdb3"
- source_UUID=`lsblk -no UUID "$source_device"`
- backup1_UUID=`lsblk -no UUID "$backup1_device"`
- backup2_UUID=`lsblk -no UUID "$backup2_device"`
- # Dates to do things;
- today=_`date +%y%m%d`
- ## Age in days to remove snapshots.
- snapremoveday=_`date +%y%m%d --date="-3 day"`
- ## Age in days to keep backups.
- backupkeepday=_`date +%y%m%d --date="-7 day"`
- ## Age in days to remove backups.
- backupremoveday=_`date +%y%m%d --date="-14 day"`
- # Assemble the above variables into single variables;
- ## The source is the full path and distro subvolume name combined;
- source="$source_mount""$distro"
- ## Snapshot name is the source with today's date appended;
- newsnap="$source""$today"
- ## Snapshot to be deleted is the source with the snapshot removal date appended;
- oldsnap="$source""$snapremoveday"
- ## Name for read-only snapshot used for backups is the source with _ro and today's date appended;
- newbackup="$distro"_ro"$today"
- ## Name for the oldest backup is the subvolume name with the backup removal date appended;
- oldbackup="$distro""$backupremoveday"
- # Verify the parent file system is mounted and mount it if not;
- if ! mountpoint -q "$source_mount"
- then
- mount "$source_device" "$source_mount"
- fi
- # Delete the oldest snapshot;
- if [ -d "$oldsnap" ]
- then
- btrfs su de -c "$oldsnap"
- fi
- # Take a new read-write snapshot;
- btrfs su sn "$source" "$newsnap"
- touch "$newsnap"
- # For backups; Take a read-only snapshot and send it to the backup
- # locations, make backup snapshots bootable, and delete oldest backups.
- # Verify it's backup day;
- if [ `date +%w` = 0 ]
- then
- # Take a read-only snapshot
- btrfs su sn -r "$source" "$source_mount""$newbackup"
- # Verify the backup file systems are mounted and mount them it if not;
- if ! mountpoint -q "$backup1_mount"
- then
- mount "$backup1_device" "$backup1_mount"
- fi
- if ! mountpoint -q "$backup2_mount"
- then
- mount "$backup2_device" "$backup2_mount"
- fi
- # Delete oldest backups;
- if [ -d "$backup1_mount""$oldbackup" ]
- then
- btrfs su de -c "$backup1_mount""$oldbackup"
- fi
- if [ -d "$backup2_mount""$oldbackup" ]
- then
- btrfs su de -c "$backup2_mount""$oldbackup"
- fi
- # Rename previous week's backups by adding backupkeepday;
- mv "$backup1_mount""$distro" "$backup1_mount""$distro""$backupkeepday"
- mv "$backup2_mount""$distro" "$backup2_mount""$distro""$backupkeepday"
- ## The renaming prepares the oldest backup to be deleted next week and renames
- ## the current backup so we're ready to receive the newest backup.
- # Send the read-only snapshot to the backup locations;
- btrfs send "$source_mount""$newbackup" | btrfs receive "$backup1_mount"
- btrfs send "$source_mount""$newbackup" | btrfs receive "$backup2_mount"
- # Take a read-write snapshot of the received backups and using the distro name;
- btrfs su sn "$backup1_mount""$newbackup" "$backup1_mount""$distro"
- btrfs su sn "$backup2_mount""$newbackup" "$backup2_mount""$distro"
- # Delete read-only snapshots because they are no longer needed;
- btrfs su de -c "$backup1_mount""$newbackup"
- btrfs su de -c "$backup2_mount""$newbackup"
- btrfs su de -c "$source_mount""$newbackup"
- # Edit the UUIDs in grub and fstab so the backups are bootable;
- ### NOT YET VERIFIED AS WORKING###
- sed -i "s@$source_UUID@$backup1_UUID@" "$backup1_mount""$distro"/etc/fstab
- sed -i "s@$source_UUID@$backup2_UUID@" "$backup2_mount""$distro"/etc/fstab
- sed -i "s@$source_UUID@$backup1_UUID@" "$backup1_mount""$distro"/boot/grub/grub.cfg
- sed -i "s@$source_UUID@$backup2_UUID@" "$backup2_mount""$distro"/boot/grub/grub.cfg
- ## This sed command is a replace-in-place action to change the UUIDs from the source to
- ## the backup file systems. The goal here is to make the backups bootable.
- fi
- # Notify of completion of tasks;
- # Verify it's backup day;
- if [ `date +%w` = 0 ]
- # Notify that backup tasks are complete and the old backups and snapshot are deleted;
- then
- su - stuart -c "notify-send -i package-install -t 0 'Desktop backup:' 'Backup made and sent. Old backups and snapshot removed.'"
- else
- # Notify that a snapshot has been taken and the old one deleted;
- su - stuart -c "notify-send -i package-install -t 0 'Desktop snapshot:' 'Daily snapshot made. Old snapshot removed.'"
- fi
- # Script complete
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment