The simplest method of creating a backup of a file is simply to copy it. Note that copying to the same hard drive doesn't address the issue of having a copy of data remote and off-site. (However, copying to a remote filesystem, which acts similar to local data storage, may address that need.) However, simply making a quick copy is often a fast and convenient way to back up a small number of files. Also, “restoring” a file is as quick as just copying the backup file, which may be faster than the restore process used by some backup software (which may need to open up some sort of index/catalog to locate the data).

Note that it can be useful to have each backup go to a slightly different location, so that each location represents a unique “point in time” that may be restored. If the data being backed up is actually a single file, rather than multiple, then it can be convenient to create a unique filename for a different version of the file, instead of storing the file (with the same filename) to different locations.

This sort of backup approach can become unwieldy: without a clear system, it may take some time to figure out how to create a unique filename, and to remember where the backup files are getting stored to. The simplest solution may be to not use this backup method, but to just start using some actual backup software that is designed to do things like providing some organization for different versions of the data.

This approach doesn't cover the topic of scheduling. Many operating systems include a method for scheduling tasks.

Example steps for Unix

It may be possible to streamline this a bit, but the goal was to create a straightforward example which is easy to implement and/or adapt as needed.

Creating the cpytobak command:

Choose from one of the following methods of getting the file where it needs to be, and then perform the follow-up tasks for making the file easily usable.

These directions do reference a /usr/local/bin/ directory as a place for the file to be installed to. That directory location might be an OpenBSD-ism. In other operating systems, specify a different directory that is part of the PATH that is visible when running “ echo ${PATH} ”.

Supplying contents of the text file

If possible, create a text file. That may be done using any of the methods described by editing a text file, including running the following:

cat >> ~/cpytobak

Version 1.0 ran these commands:

[ "$CPYBKDST" ] || CPYBKDST=/origbak
[ "$CPYBKSU" ] || CPYBKSU=sudo
${CPYBKSU} mkdir -p ${CPYBKDST}/.
${CPYBKSU} chown root:wheel ${CPYBKDST}
${CPYBKSU} chmod og+rwx ${CPYBKDST}
export VERCPDIR=$( ${CPYBKSU} mktemp -d ${CPYBKDST}/$( date +%F%H%M%S%Z%a )_uniqXXXXXXXXXX )
echo Backing up to ${VERCPDIR}
#${CPYBKSU} mkdir -p ${VERCPDIR}$( dirname $1 )
#${CPYBKSU} cp -i $* $( dirname ${VERCPDIR}/$1 )
${CPYBKSU} tar -cPvzf ${VERCPDIR}/files.tgz $*

If using the method of redirecting cat's output in order to create a text file, then type (or, paste from a clipboard) the desired contents and then (on a new line, after pressing Enter if needed) press Ctrl-D.

Then, it may be worthwhile to make sure the file is in the PATH. This task tasks may be as simple as running:

sudo cp ~/cpytobak /usr/local/bin/.
Downloading the file

If downloading would be easier than copy and paste, the file is available as cpytobak version 2.1 (compressed in GZIP format)

Also available is cpytobak version 1.0. (That downloadable file is the same as some sample code shown earlier, but simply added a comment for the version number.)

To obtain the file, use one of the web transfering options. Old versions of OpenBSD were probably fairly unusual in that it comes with a text-based web browser (Lynx), but may not have had one of the other web transfering options like curl. Running the following worked well:

lynx -dump http://cyberpillar.com/dirsver/1/mainsite/techns/bhndscen/disasrec/backup/cpytobak/cpybakv1 | sudo tee -a /usr/local/bin/cpytobak

Newer versions of OpenBSD did away with bundling Lynx, but at least newer versions of OpenBSD can download from HTTP(S) using the ftp command that is built into that operating system. Despite being named ftp in that operating system, the command is actually a copy of tnftp (for further info, see: section on tnftp), and so it is more complex than basic programs named ftp that are pre-bundled in some other operating systems.

Also, make sure that it is easily runnable.

sudo chmod a+x /usr/local/bin/cpytobak
Backing up files

One method of backing up files is to simply manually run each of the commands shown in that example text file, except to skip all of the lines that start with the comment character (the # hash mark), and also replace the references to $1 with the full path of the file to be backed up. Also, if doing this manually, skip the echo command and, after the first time, skip the lines before the export statement.

Even simpler, though, is to have those commands in a text file. Then run the script file with a reference to the full path of the file to be backed up. For example, customizing the reference to the name of the script file (if necessary), and customizing the reference to the name of the file to be backed up (which likely will be necessary), run something like the following:

. ~/cpytobak somepath/somefile
More usage details on the cpytobak script file

Using the tar command, as shown in the above example, is generally going to be the easy approach. The main reason to not use the tar command would just be if the tar command is not installed.

The exact version shown requires tar and gzip. To avoid requiring gzip, just remove the z in the first parameter of the tar command. To use neither tar nor gzip, remove the comment character (which is the “#” “hash mark” character) from both of the previous couple of lines, and insert such a character at the beginning of the line that runs the tar command.

If using tar

Multiple files may be specified on the command line, if using the provided tar command. The cp command might support multiple files if they are all in the same directory, but beware that only files are specified. Specifically, the concern is that dirname will may chop off the last subdirectory. For instance, dirname could convert a reference to /usr/local or /usr/local/ to just the parent directory, which would be the /usr directory in this example. (The end result might end up backing up what was specified, but may also undesirably back up much more data than intended).

If the tar complains about stripping a leading slash, know that can be easily worked around by using the P command line switch. If that switch is not being used, know that the leading slash is stripped, as it will be important if/when extracting.

Locating the backed up data

To see the most recently created backup files, use:

ls -lt /origbak/

Depending on what method is being used to back up, this will show either a bunch of *.tgz files, or a bunch of *.tar files, or a bunch of directories. Whatever is shown will have some rather randomish names. Searching the *.tar archives (which my be effectively embedded in the *.tgz files) may be rather inconvenient. This backup method was created with a focus on an extremely simple process for backing up, not with a focus on easily restoring. (However, locating the file to restore may still be a lot more painless than re-creating whatever file needs to be retrieved.)

If sudo is unavailable

The sudo command comes bundled in with some operating system distributions, but others might not have it. The recommended solution is to simply get sudo installed and functional (if it isn't functional yet). However, that's not absolutely necessary.

This example makes some pretty heavy use of sudo to try to work around permissions restrictions. However, sudo may not be something that is necessarily usable, perhaps intentionally (due to security restrictions) or simply because it hasn't been set up yet. In such a case, either comment out the line that defines CPYBKSU to run sudo, or set CPYBKDST to ~/origbak and set CPYBKSU to a space character. Example commands to do that would be:

echo This may be unneeded and undesirable if there is access to use sudo.
export CPYBKDST=~/origbak
export CPYBKSU=" "
cpytobak program development notes

Besides updates to comments internal to the program, here is a summary of updates:

Version 2.1
  • Tries multiple elevating commands, including doas. Requires "which"
    to find a common elevation command before trying to run it. Reports
    program version number, and which elevation command is used.
    which also checks that tar is available. If not, fallback to use cp
    as done before cpytobak added support to use tar instead.
  • Added support for using tar options -J, to try to use xz, and if that does not work, -j, to try to use bzip2. If neither of those work, -z is tried and presumed to work okay.
Version 2.0
  • Output more
Ideas for future updates
  • might want to consider using xz rather than "tar -z"?