LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > sag47
User Name
Password

Notices


Rate this Entry

Removing files with find, and LVM snapshots

Posted 04-24-2012 at 11:21 AM 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
Code:
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.

Code:
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.

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

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

Code:
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.
Code:
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.

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

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

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

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

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

Code:
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.

Code:
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.

Code:
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.

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

Code:
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.

Code:
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 2557 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 07:33 PM.

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