Script for creating/managing snapshots of all subvolumes of all filesystems
Hi
FWIW, I've also written a script which creates and "manages" (ie. deletes old) snapshots.
It figures out all the available filesystems and creates snaps for all the available (sub)volumes.
It's also on https://copy.com/WI9AXqTH2nD4 and http://pastebin.com/YX8WKcsR
to avoid line break issues and also with comments.
Regards,
Alexander
----- cut here
#!/bin/sh
echo "Usage: $0 SNAPSHOT_TAG NUM_SNAPSHOTS
Create hourly, daily, weekly, and monthly snapshots of btrfs filesystems.
Based somewhat on http://article.gmane.org/gmane.comp.file-
systems.btrfs/12609
Here's my crontab:
00,15,30,45 * * * * $0 frequently 4 38 * * * * $0 hourly 24 08 00 * * * $0 daily 7 08 12 * * 0 $0 weekly 4"
exit 1
fi
SNAPSHOT_TAG="$1"
NUM_SNAPSHOTS="$2"
snap_prefix="snapshot:$SNAPSHOT_TAG:"
snap_date="date +%Y-%m-%d--%H.%M.%S.%N"
script_name=basename "$0"
log_fac="local5"
log_tag="$script_name"
btrfs_progs_dev_path="/home/a/Copy/Computerkram/Programme/btrfs- progs.dev/bin"
PATH="$btrfs_progs_dev_path:$PATH"
btrfs fi show 2>/dev/null | awk '/ path / {print $NF}' | while read dev; do
set -- `btrfs fi show 2>/dev/null | grep -B2 " path $dev" | \
grep "Label:" | sed 's,.*: \(.*\) uuid: \(.*\),\1 \2,'`
label="$1"
uuid="$2"
logger -t "$log_tag" -p "$log_fac.info" -- \
"Processing filesystem with label $label and uuid $uuid on $dev"
safe_dev=`echo "$dev" | tr / .`
tmp_mount_dir=`mktemp -d "/tmp/.btrfs.mount.$uuid.$safe_dev.XXXXXX"`
if ! mount -t btrfs "$dev" "$tmp_mount_dir"; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: mount -t btrfs $dev $tmp_mount_dir"
exit 1
fi
_snap_name="$tmp_mount_dir/,$snap_prefix$snap_date"
if ! btrfs subv snaps -r "$tmp_mount_dir" "$_snap_name" > /dev/null; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: btrfs subv snaps -r $tmp_mount_dir
$_snap_name"
exit 1
else
logger -t "$log_tag" -p "$log_fac.info" -- \
"Created snapshot $Path,$snap_prefix$snap_date for root volume of fs
with uuid $uuid"
fi
(btrfs subv list -r "$tmp_mount_dir" | grep " path ,$snap_prefix" \
| tail -"$NUM_SNAPSHOTS"
btrfs subv list -r "$tmp_mount_dir" | grep " path ,$snap_prefix") \
| sort | uniq -u \
| while read __id IdDel __gen GenDel __top __level ToplevelDel __path PathDel; do
if ! btrfs subv del "$tmp_mount_dir/$PathDel" > /dev/null; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: btrfs subv del $tmp_mount_dir/$PathDel"
exit 1
else
logger -t "$log_tag" -p "$log_fac.info" -- \
"Removed snapshot $PathDel"
fi
done
(btrfs subv list -ar $tmp_mount_dir; btrfs subv list -a $tmp_mount_dir)
\
| sort | uniq -u \
| while read _id Id _gen Gen _top _level Toplevel _path Path; do _snap_name="$tmp_mount_dir/$Path,$snap_prefix$snap_date" if ! btrfs subv snaps -r "$tmp_mount_dir/$Path" "$_snap_name" >/dev/null; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: btrfs subv snaps -r $tmp_mount_dir/$Path
$_snap_name"
exit 1
else
logger -t "$log_tag" -p "$log_fac.info" -- \
"Created snapshot $Path,$snap_prefix$snap_date for subvolume
$Path"
fi
(btrfs subv list -r "$tmp_mount_dir" \
| grep " path $Path,$snap_prefix" | tail -"$NUM_SNAPSHOTS" btrfs subv list -r "$tmp_mount_dir"|grep " path
$Path,$snap_prefix") \
| sort | uniq -u \
| while read __id IdDel __gen GenDel __top __level ToplevelDel __path PathDel; do
if ! btrfs subv del "$tmp_mount_dir/$PathDel" > /dev/null; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: btrfs subv del
$tmp_mount_dir/$PathDel"
exit 1
else
logger -t "$log_tag" -p "$log_fac.info" -- \
"Removed snapshot $PathDel"
fi
done
done
if ! umount "$tmp_mount_dir"; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: umount$tmp_mount_dir"
fi
if ! rmdir "$tmp_mount_dir"; then
logger -t "$log_tag" -p "$log_fac.err" -- \
"Error! Could not do: rmdir $tmp_mount_dir"
fi
done
exit 0
Quoted 9 lines · Alexander Skwar
FWIW, I've also written a script which creates and "manages" (ie. deletes old) snapshots.
It figures out all the available filesystems and creates snaps for all the available (sub)volumes.
It's also on https://copy.com/WI9AXqTH2nD4 and
http://pastebin.com/YX8WKcsR to avoid line break issues and also with comments.
You should change the calls of "btrfs subv del" etc to use unabbreviated versions of the commands. Otherwise you expose your script to erratic behavior as soon as new commands are added to btrfs that make your usage of the commands ambigious.
Regards,
Kai
Hi
Quoted 8 lines · Kai Krakow
Quoted 6 lines · Alexander Skwar
FWIW, I've also written a script which creates and "manages" (ie. deletes old) snapshots.
It figures out all the available filesystems and creates snaps for all the available (sub)volumes.
Quoted 6 lines
Quoted 3 lines
It's also on https://copy.com/WI9AXqTH2nD4 and
http://pastebin.com/YX8WKcsR to avoid line break issues and also with comments.You should change the calls of "btrfs subv del" etc to use unabbreviated versions of the commands. Otherwise you expose your script to erratic
Oh, yes. Very good point. And there really isn't any benefit in using abbreviated versions of the command names in a script anyway.
On the command line: Yes, less to type = better.
But in a script?
Thanks. Very good point indeed!
Alexander
--
=> Google+ => http://plus.skwar.me <==
=> Chat (Jabber/Google Talk) => [email protected] <==