Advanced Backup Strategies

Advanced Techniques

The previous demonstration is just the tip of the iceberg when it comes to the power and flexibility of the command line. For example the above scripts are executed sequentially without logic. Using the same script for a full or incremental backup is ideal, so to accomplish this, we use a combination of logic and command line arguments.

For the purposes of my examples, assume a value of 1 denotes a full backup and any other value denotes an incremental backup.

A Linux would be called from the command line like so:

# do a full backup
sh backup_script.sh 1

# do an incremental backup
sh backup_script.sh 0

Now modify the Linux script to handle the parameter appropriately:

# test the first parameter and handle accordingly
if [ $1 -eq 1 ]
then
# do a full backup
else
# do an incremental backup
fi

Now for the same using the Windows script, which would be called from the command line via:

REM do a full backup
backup_script.bat 1

REM do an incremental backup
backup_script.bat 0

Now apply the modification to the Windows script:

REM test the first parameter and handle accordingly
if %1==1 (
REM do a full backup
) else (
REM do an incremental backup
)

Remember, the scope of this article is not to be a tutorial on writing command line scripts. This has already been accomplished very well by others.

For those of you familiar with PHP, it can also be used from the command line to perform tasks where the command line may be lacking or archaic. Alternatively, if you are not comfortable with the respective command line syntax, however are with PHP’s, many of the same tasks can be done using PHP. Additionally, since PHP scripts are cross-platform, scripts written for Windows or Linux are completely transferable. Combining the convenience of PHP scripts with the power of the command line makes virtually anything is possible.

Alternatives

Of course you do not have to use the command line to do backups. There are, literally, hundreds of free and commercial products which do everything from drive-to-drive file copy backups to complex and intricate detailed server serialization. Here are a couple of freely available tools I have used:

  • Windows: Ntbackup
    Ntbackup ships with Windows and allows you to use a graphical tool to configure what data to backup. It is great for doing file copy backup and restores and features a command line interface. The downside is the resulting backup file is not compressed or dynamically named based on date (you can accomplish this using the command line, however) and backing up service data (such as SQL databases) requires either finding a plugin (if one even exists) or the use of an additional tool.
  • Linux: SBackup
    SBackup is lightweight and features everything you really need in a file copy backup program. Backups are compressed and aging backups can be automatically removed to make it truly "set and forget". The downside, much like Ntbackup, is service data requires additional tools to be backed up properly.

If none of the above suit what you are looking for, do a Google search for backup programs. As stated above, there hundreds available, many are free, which may fit your exact needs.

Scheduling Backup Tasks

As discussed above, your backups should be run at a time where system resources are in lowest demand. This can be difficult to pinpoint if you cater to a world-wide audience, so in order to find the ideal backup time, check your traffic statistics to find when the fewest visitors hit your server. If you still are unsure, choosing a local time of 3 AM is usually a safe bet. Another consideration is the day to run a full backup. Again, usage statistics go a long way in helping to determine the best date.

Once you have a time established, all you need to do is configure your backup task. Since we are using command line scripts, all you really need to specify in your scheduler is the time to execute followed by the path the script with the appropriate parameters (i.e. the full or incremental switch) appended. The process is, basically, what is outlined below. My examples are in reference to the scripts above, so you can adjust accordingly.

Create a weekly full backup and daily incremental backup using the example scripts above.
Windows Linux
Task Creation
  1. Open Task Scheduler (Start > Control Panel > Task Scheduler).
  2. Double click Add Scheduled Task.
  3. When the wizard asks for the program, browse to the backup batch (.bat) file.

The command used to create scheduled tasks (for the purposes of this article) in Linux is the crontab command. This makes use of the Vixie-Cron task scheduler in Linux. Whether or not this is installed on your system can vary, however just about every major distribution has this command included.
For more information, see Scheduling commands to run in the background.

  1. Open a terminal.
  2. Enter the following command into a terminal while you are logged in or have root privileges:
    crontab -e
Weekly Full Backup
  1. Title the task Full Backup and select Weekly for the frequency.
  2. Select the day and time to run the full backup (from the considerations above).
  3. Enter an administrator’s credentials to run the task.
  4. Select the option to view the advanced properties then click Ok.
  5. In the Run box, add a 1 to the end, so the the task looks like this:
    C:\backups\backup_script.bat 1
  6. Apply the changes.
  1. Add the following to the end of the file:
    # run full backup at 3 AM every Sunday
    0 3 * * 0 sh /var/backup/backup_script.sh 1
  2. Save and close (Ctrl + X).
Daily Incremental Backup
  1. Title the task Incremental Backup and select Daily for the frequency.
  2. Select the time to run the incremental backup (from the considerations above).
  3. Enter an administrator’s credentials to run the task.
  4. Select the option to view the advanced properties then click Ok.
  5. In the Run box, add a 0 to the end, so the the task looks like this:
    C:\backups\backup_script.bat 0
  6. Apply the changes.
  1. Add the following to the end of the file:
    # run incremental backup at 1 AM everyday
    0 1 * * * sh /var/backup/backup_script.sh 0
  2. Save and close (Ctrl + X).

Conculsion

As you can see, implementing a reliable backup process is not really difficult. Most of the work just needs be done upfront during the planning phase. Personally, I have found no better backup method than a well thought out script, so if you decide to go the route of the command line, I encourage you to thoroughly test your scripts before implementing.

Of course I cannot cover every aspect of backups in depth, however I hope you enjoyed and learned something from this article, so please let me know what you thought.

Opt In Image
Free Weekly PCMech Newsletter
Almost 500 Issues So Far, Received By Thousands Every Week.

The PCMech.com weekly newsletter has been running strong for over 8 years. Sign up to get tech news, updates and exclusive content - right in your inbox. Also get (several) free gifts.

Pages: 1 2 3

Leave a Reply

PCMech Insider Cover Images - Subscribe To Get Your Copies!
Learn More
Tech Information you can use, sent to your inbox each and every week. Check out PCMech's digital e-zine...