Skip to content

Instantly share code, notes, and snippets.

@icedream
Last active June 26, 2017 09:12
Show Gist options
  • Save icedream/73e7b4934fe7db8f21c08a5332936674 to your computer and use it in GitHub Desktop.
Save icedream/73e7b4934fe7db8f21c08a5332936674 to your computer and use it in GitHub Desktop.
Simple stupid reproducible Linux backup solution

Backup

Just a simple backup solution aimed to allow 100% exact reproduction of all necessary files of a system on an ext4 file system.

This is by far not the fastest solution but it works and can still be expanded with encryption and other inter-device transformations.

As a performance hint, my USB backup drive has a normal throughput around 120 MB/s, iotop reported that rsync ran at 33 MB/s for large files. There's definitely room for optimization, for example because every write I/O to the block device causes a timestamp update on the file itself.

file="/run/media/icedream/Backup/backup-laptop"
device="/dev/backup/$(basename "$file")"
source="/" # all files will be backed up from here
target="/mnt/backup/$(basename "$file")" # should be mounted somewhere in the ignored parts of the filesystem (see below)
ignore='{"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'

# Create loopback device
if [ ! -b "$device" ]; then
	mkdir -p "$(dirname "$device")"
  
	# Not so intelligent way to find next available loopback device ID
	for i in $(seq 200 299); do
		mknod "$device" b 7 $i && break || continue
	done

	# Connect with actual file
	losetup "$device" "$file"
	# ...or if you need encryption:
	#losetup -e des "$device" "$file"
fi

# Set up ext4 file system, just to be done exactly once
mkfs.ext4 "$device"

# Mount
mkdir -p "$(dirname "$target")"
mount "$device" "$target"

# Synchronize files into backup filesystem
rsync -aAXv --delete --exclude="$ignore" "$source" "$target"

# Unmount and destroy device file
umount "$device"
losetup -d "$device"
rm "$device"

Restore

Restores a backup done using the backup script shown in the Backup section.

file="/run/media/icedream/Backup/backup-laptop"
device="/dev/backup/$(basename "$file")"
source="/" # all files will be restored here
target="/mnt/backup/$(basename "$file")" # should be mounted somewhere in the ignored parts of the filesystem (see below)

# Create loopback device
if [ ! -b "$device" ]; then
	mkdir -p "$(dirname "$device")"
  
	# Not so intelligent way to find next available loopback device ID
	for i in $(seq 200 299); do
		mknod "$device" b 7 $i && break || continue
	done

	# Connect with actual file
	losetup "$device" "$file"
	# ...or if you need encryption:
	#losetup -e des "$device" "$file"
fi

# Mount
mkdir -p "$(dirname "$target")"
mount "$device" "$target"

# Copy files into actual filesystem, note the / at the end of $target to
# indicate synchronizing the contents of the folder instead of the folder itself.
rsync -aAXv "$target/" "$source"

# Unmount and destroy device file
umount "$device"
losetup -d "$device"
rm "$device"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment