LinuxAnswers - the LQ Linux tutorial section.
Go Back > Blogs > sag47
User Name


Rate this Entry

Removing files with find, and LVM snapshots

Posted 04-24-2012 at 12:21 PM by sag47
Tags find, lvm, snapshot

Take an LVM snapshot using a loopback device
Create the snapshot
Take a look at the devices in your volume group. First there needs to be sufficient space in the volume group for a snapshot to occur. Check this by typing: vgdisplay and checking for Free PE / Size
vgdisplay -v
Create a loopback device to use for the snapshot. My volume group is named vg_echo360. I want my snapshot to be 9.9GB in size so I'm creating a loopback device which is 10GB in size.

dd if=/dev/zero of=/app/loop bs=1 count=1 seek=10737418240
losetup -f /app/loop
losetup -a
Now that you see which loopback device your /app/loop file is using you can add it to the volume group. For me it's /dev/loop0.

pvcreate /dev/loop0
vgextend vg_echo360 /dev/loop0
Create the snapshot.

lvcreate -s -n echo_snapshot1 /dev/vg_echo360/lv_echo360 -L 9.9G
Recover files from an existing snapshot
It's relatively easy to recover files from an existing snapshot. Basically just...

mkdir /mnt/snapshot_recovery
mount /dev/vg_echo360/echo_snapshot1 /mnt/snapshot_recovery
Now the path /mnt/snapshot_recovery contains the filesystem at a state from when the snapshot was taken.

Remove the snapshot when no longer needed
Be aware that LVM will throw out the snap space if it overflows. Keep an eye on it while changing things with the "lvs" command.
lvremove /dev/vg_echo360/echo_snapshot1
Do not allow the LVG to allocate data to /dev/loop0 any longer. This sets the PV Status to NOT allocatable.

pvchange -xn /dev/loop0
Check out the status of /dev/loop0 compared to the other devices.

vgdisplay -v
Remove the loopback device from the LVG entirely.

vgreduce vg_echo360 /dev/loop0
See that the device is missing in the following command?

vgdisplay -v
Now that the device is removed from the LVG you can remove the loopback device from the system entirely.

losetup -a
/dev/loop0 is the device I want to remove which is using /app/loop.

losetup -d /dev/loop0
rm /app/loop
FINAL NOTE ON LVM SNAPSHOTS: If you accidentally removed the loopback device before removing it from the LVM that's okay. Just be sure to run the following command on the LVG.

vgreduce --removemissing vg_echo360
Using find to remove files
Today at work I had to find all raw files a media server archived which were older than 120 days and delete them. They are regular files and have the following two possible extensions: .aac and .h264. So I built a single find command. I first created an LVM snapshot in case I needed to revert changes.

Before running the remove command put the found file names in a temporary file.

find . -mtime +120 -type f -name '*.h264' -o -mtime +120 -type f -name '*.aac' | tr '\n' ' ' > /tmp/rawfiles
You can see how much space you will save by doing that.

du -shc $(cat /tmp/rawfiles)
Now delete all of the files.

find . -mtime +120 -type f -name '*.h264' -exec rm {} \; -o -mtime +120 -type f -name '*.aac' -exec rm {} \;
Now that all the files are deleted make sure that there aren't any left over.

du -shc $(cat /tmp/rawfiles) 2> /dev/null
The disk usage should say 0 total used space.

See man find.
OPERATORS section for -o option.
See TESTS subsection in the EXPRESSIONS section of the find man page for the rest of the options.

Another thing to note, when using the -o option is that you have to treat each expression on either side like a completely new find command. It essentially allows you to put two find commands on a single line.
Posted in Uncategorized
Views 1146 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 11:39 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration