Last active
August 29, 2015 14:07
-
-
Save shouya/6cc3393d738a9003af96 to your computer and use it in GitHub Desktop.
system-wide backup to btrfs with rsync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PART_LABEL=linuxbackup | |
DEST_DIR=/mnt/backup | |
snapshot_name=`date +bak_%Y%m%d` | |
echo "Before running this script, please make sure have your fstab set up." | |
echo "A working example looks like:" | |
cat <<EOF | |
/dev/sdb2 / <FS> <MOUNT_OPTS> 0 1 | |
/dev/sdb2 /home <FS> <MOUNT_OPTS> 0 1 | |
LABEL=linuxbackup /mnt/backup btrfs subvol=latest,noauto,rw 0 0 | |
EOF | |
echo "pausing for 3 secs, you will have chance to interrupt this script." | |
seq 3 | while read x; do | |
sleep 1 | |
echo -n '.' | |
done | |
echo | |
echo 'checking if the device is been using' | |
if [ $(lsof $DEST_DIR | wc -l) != "0" ]; then | |
echo 'backup stopped, the device is been used, here output the result of lsof' | |
lsof $DEST_DIR | |
exit | |
fi | |
echo 'mounting backup device' | |
sync | |
umount $DEST_DIR | |
mount $DEST_DIR 2>/dev/null | |
echo 'rsync-ing root' | |
rsync --force --progress --delete-excluded -avx / $DEST_DIR \ | |
--exclude '/tmp/*' \ | |
--exclude '/var/tmp/*' \ | |
--exclude '/dev/*' \ | |
--exclude '/proc/*' \ | |
--exclude '/sys/*' \ | |
|| exit | |
echo 'rsync-ing home' | |
rsync --force --progress --delete-excluded -avx /home/ $DEST_DIR/home \ | |
--exclude '/home/*/tmp' \ | |
--exclude '/home/*/.cache' \ | |
|| exit | |
echo 'generating snapshot' | |
sync | |
umount $DEST_DIR || exit | |
mount -t btrfs -o subvolid=0,rw /dev/disk/by-label/linuxbackup $DEST_DIR || exit | |
btrfs subvolume delete "$DEST_DIR/$snapshot_name" 2>/dev/null | |
btrfs subvolume snapshot -r $DEST_DIR/latest $DEST_DIR/$snapshot_name || exit | |
echo 'umounting backup device' | |
sync | |
umount $DEST_DIR || exit | |
echo 'done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment