Be aware that flash drives have a limited number of write cycles to each location; they do go bad!
Note that you have to be careful using the wildcard asterisk. If Bob wanted to backup his home directory and specified "/home/bob/*", he would not get any of the so-called "hidden" files in the directory /home/bob whose names begin with a period (he would get the hidden files in any subdirectories of /home/bob). If he specified "/home/bob/", he would get everything.
In the examples above, root can backup the user files in the /home/ directory to a file in /root, but how does an ordinary user back up their home directory to a file? They can use /tmp/ - anyone can write to it, but anyone can also read from it. To get around the security implications of that, first touch the backup file in /tmp/, then chmod the file so that only you can read from it, and be sure to erase it when you are done.You might also want to exclude some directories from your backup; for example, before the "/home/*" you might addOur distribution has umask set to 0007, so a user can place a file in /tmp without worrying about other users reading it.
--exclude='home/*/.cache/*'
Here, "starting-directory" is the place in the directory tree at which to begin the search (the search will continue through the entire directory sub-tree from this vertex), and "pattern" is a file name pattern (either a full file or directory name, or one that uses wild cards, ie. '*.mpg' or 'X*').
In this example, the file name search will be case insensitive.
Here "path" is a file or directory, and only those files are found which have been modified more recently than path.The newer option is very convenient in designing backup scripts, in conjunction with the touch command: if you touch /root/.backup at the end of each backup, find / -newer /root/.backup will find all files and directories modified since the last backup.
The option "-type d" will find only directories.
Any of the find commands will produce output more like ls -l by using the option "-ls". This is useful if information about the found files is needed.
All of these options should come after the "starting-directory".
find / -newer /root/.backup -type f | tar -czf backup.tgz -T -Here, the files modified since the last backup will be placed in the gzipped tarball "backup.tgz". The "-T" option to tar tells it to get the list of files to place in the tarball from a file, and "-T -" specifies that stdin is the file to be used.
By piping stdout from the find command (which is of course the list of file names) into stdin for the tar command, we avoid the necessity of creating a temporary file on disk with the list of file names to be backed up. Note that stdin and stdout (as well as stderr) are streams: a stream of data can only be scanned once (the water in a stream passes by only once; it never returns as long as you ignore evaporation and rain). This means that some tar options won't work, since the list of file names cannot be rescanned (ie., -W to verify the tarball).Since tar stores path names it is sometimes hard to remember exactly what filename to use when extracting a specific file. You can use a pipe to make the job easier:
tar -tzf backup.tgz | grep -e 'filename' | tar -xvzf backup.tgz -T -will list the table of contents of the tarball, grep the one with the filename you are looking for (but with the path information tar needs to find it) and extract that file.
tar -czf backup.$(date +%y%m%d).tgz /home/*The "$(command)" inserts the output of the command into the filename; the "+%y%m%d" parameter to the date command gives the date in the form "YYMMDD", which is convenient for allowing ls to show the backup files in order.
cmp backup.tgz /media/usbstg/backup.tgzwill compare the backup tarball on the hard drive with the one you just wrote to the backup flash drive.
Note that if you write a file to disk and then use cmp to see if it has been written correctly, you must first umount and re-mount the disk. This guarantees that you are not checking the actual file on the disk, and not just the copy in cache.
md5sum filename > filename.md5sumwill compute the checksum and
md5sum -c filename.md5sumwill check the file against the previously computed checksum.
Be sure to test it!Your backup script should be in "/root/bin/backup".
If you want to get fancy, try this:if [ "$1" = "full" ]; then"$1" is the first positional parameter to a script, so backup would do an incremental backup while backup full would do a full backup.(place your full backup code here)else(place your incremental backup code here)fi
Place the output of the md5sum commands in /root/ (with filenames ending in ".md5").
©2015, Kenneth R. Koehler. All Rights Reserved. This document may be freely reproduced provided that this copyright notice is included.
Please send comments or suggestions to the author.