Finding Recent Files
Before you suggest that it is better to use a backup program like Bacula or Amanda, I shall insist that making backups from the command-line is mighty useful. In scenarios where you are running in an embedded system (Rpi, Beaglebone), or a headless server that you want to keep a minimum install footprint on, writing a just-fits backup script can often be the correct choice.
This will be the first of a series of backup articles: we will learn various aspects of the find command, and move onto ways of using rsync and find in conjunction. I’m totally sure it will be a killer trip, man.
A bunk trip for most people is reading the man page for find. It’s not a command you learn from the man page. Instead, there are numerous options and endless possibilities for confusion…unless you have a tutorial. So, little dudes, let’s begin with the general features of find:
- There’s a way to mark paths: -path and not descend them: -prune
- You can make file name patterns with -name “*thing1*” and -iname “*thing2*”
- And they can be combined with AND, OR and NOT: -a, -o, !
- But don’t stop there. ALL aspects of the unix-style file system can be queried. The most likely things we want to know are file type:-type and last modified time: -mtime.
Excellent! Or well, that’s the theory. Let’s do three beginner examples:
- Find assumes the current directory, which is always the first argument.
cd ~; find -type f
That shows way too many files. Great for a first backup…not useful for a daily backup.
- Getting sharper- What config files did we modify in the last day?
find .config -type f -mtime -1
and the last two days?
find .config -type f -mtime -2
- You and I know we don’t backup every day: that’s just life. Being smart, we’ll use the date of our last backup to build up the list of files we have modified since our last backup. Getting gnarly:
find ~/.config -type f -newer /var/backup/home-jed-2014-12-11.tgz
I’ve done my best to keep this one simple. Next edition, we’ll be making more of our backup by taking less. Dwell on using the last command above, but on your home directory. Is that what you really want to back up? Let me know below in the comments section.