Removing files with find, and LVM snapshots
Posted 04-24-2012 at 11:21 AM by sag47
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
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.
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.
Create the snapshot.
Recover files from an existing snapshot
It's relatively easy to recover files from an existing snapshot. Basically just...
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.
Do not allow the LVG to allocate data to /dev/loop0 any longer. This sets the PV Status to NOT allocatable.
Check out the status of /dev/loop0 compared to the other devices.
Remove the loopback device from the LVG entirely.
See that the device is missing in the following command?
Now that the device is removed from the LVG you can remove the loopback device from the system entirely.
/dev/loop0 is the device I want to remove which is using /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.
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.
You can see how much space you will save by doing that.
Now delete all of the files.
Now that all the files are deleted make sure that there aren't any left over.
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.
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
Code:
dd if=/dev/zero of=/app/loop bs=1 count=1 seek=10737418240 losetup -f /app/loop losetup -a
Code:
pvcreate /dev/loop0 vgextend vg_echo360 /dev/loop0
Code:
lvcreate -s -n echo_snapshot1 /dev/vg_echo360/lv_echo360 -L 9.9G lvs
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
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
Code:
pvchange -xn /dev/loop0
Code:
vgdisplay -v
Code:
vgreduce vg_echo360 /dev/loop0
Code:
vgdisplay -v
Code:
losetup -a
Code:
losetup -d /dev/loop0 rm /app/loop
Code:
vgreduce --removemissing vg_echo360
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
Code:
du -shc $(cat /tmp/rawfiles)
Code:
find . -mtime +120 -type f -name '*.h264' -exec rm {} \; -o -mtime +120 -type f -name '*.aac' -exec rm {} \;
Code:
du -shc $(cat /tmp/rawfiles) 2> /dev/null
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.
Total Comments 0