Advanced Backup Strategies

Introduction to Command Line Scripts

Since the techniques I will be using in this article will be through the command line, I have included to some links to some references and guides. You do not have to be an expert to follow anything I will cover, however prior experience certainly would be helpful.

Command Line References and Tutorials
Windows Linux

You can always get help and parameter options by invoking the OS’s help switch. In Windows, this is "/?", for example: command_name /? (note: legacy applications may still use the "help" command: help command_name) and in Linux, the "man" command is used to invoke the manual pages: man command_name.

Why Use Command Line Scripts?

When there are so many effective commercial products available, why bother using the command line? There are numerous benefits, including:

  • Cost effective (free)
  • Fast
  • Reliable
  • Dynamic
  • Direct access to OS and application functions
  • Virtually unlimited flexibility and control
  • Practices are transferable to any system (highly adaptable)

Of course, this comes at the cost of development time, testing and an inherent knowledge of (or willingness to learn) using the command line as a trade off. In my humble opinion, tools the operating system and services (such as a database server) make available through their respective command line interface are more bullet proof than what a third party can implement. This may not be case all the time, but it is a solid guideline.

Common Service References

Before I cover script examples, I want to provide references to command line functions of the applications I will be using in my examples. These are generally bookmarks for anyone who uses, or wants to use, the command line interface for the respective application.

Creating A Basic Script

Command line scripts are executed in a completely linear fashion. While external script calls and "go to’s" can be used, typically they will not for something as simplistic as backing up data. While some may argue otherwise, to me, task centric scripts are best approached with an input-output attitude. Programming rules, such as code encapsulation and modularity are too complex to introduce into command line scripts. Of course, inline comments are always recommended. Putting a comment above each line to state what it does only takes a few seconds and makes a script much easier to understand.

Since I am in the unusually backwards position of managing Windows servers and running Linux as a desktop OS, the server script examples I am going to use will be Windows syntax and the desktop OS scripts will be for Linux. The logic from the scripts is completely applicable to the opposite OS and are adaptable by simply replacing the keywords and switches (use either /? or man to see the respective switches).

Please keep in mind, this is not a command line script tutorial. I have added comments for each line to explain what is happening, but for more detailed information on the respective commands, please use the links I supply in my article or do a Google search.

Notice: Some of the command line scripts I provided may wrap on your screen depending on your monitor resolution. By copy-pasting the scripts into a text editor, you can view the files without the wrapping.

Desktop Backup Script

Copy files from a local profile to a backup location:

# copy all files and folders to a backup folder
cp -r /home/jason /var/backups
 
# copy all files modified in the last 24 hours in a user profile to a backup folder
# note: this will not maintain or create the folder structure, only copy the files into a single directory
find /home/jason -mtime 0 -exec cp ‘{}’ /var/backups ‘;’

While the above can have it s purpose, for the most part a desktop backup process should compress the backup to conserve disk space. The Linux "tar" command has everything we need to create a compressed full and daily incremental backup:

# first, set a variable, $today, equal to the current date (yyyy-mm-dd)
declare today=$(date +%F)
 
# create a full backup
tar –create –gzip –file /var/backups/$today-full.tar.gz /home/jason/
 
# OR create an incremental backup of all files created or modified today
tar –create –newer ‘$today’ –gzip –file /var/backups/$today-inc.tar.gz /home/jason/

Keep in mind, some of the commands used above may need root access to run. If so, you can prepend, depending on your distribution, something like the Debian command:

echo root_pw | sudo -S

to the front of each line which needs root access to execute.

For the most part, a similar script to this will be sufficient for the needs of a desktop user. The typical frequency is a weekly full backup with daily incrementals, so you can adjust your scheduled jobs accordingly.

Server Backup Script

Since Windows (XP/Server 2003) currently does not have a command line compression tool, I am going to use the excellent open source program 7-Zip to perform command line compression.

This script will create a temporary directory, dump all the data we want to backup, then compress it.

REM create a variable, %filedate%, equal to today’s date in format yyyy-mm-dd
for /F "tokens=2,3,4 delims=/ " %%a in (‘date /t’) do set filedate=%%a-%%b-%%c
 
REM make a backup directory
mkdir "c:\\temp_bak"
 
REM backup websites on the server
mkdir "c:\\temp_bak\\websites"
REM full backup
xcopy "c:\\websites" "c:\\temp_bak\\websites" /c /h /i /q /-y /e
REM incremental backup of files created or modified today
xcopy "c:\\websites" "c:\\temp_bak\\websites" /c /h /i /q /-y /s /d:%filedate%
 
REM backup DNS entries
mkdir "c:\\temp_bak\\dns"
xcopy "%windir%\\system32\\dns" "c:\\temp_bak\\dns" /c /h /i /q /-y /e
 
REM backup SQL databases
mkdir "c:\\temp_bak\\sql"
REM Microsoft SQL Server 2000 databases
set path=%programfiles%\\Microsoft SQL Server\\80\\Tools\\Binn
isql -E -Q "backup database [database1] to disk=’c:\\temp_bak\\sql\\database1.mssql’"
isql -E -Q "backup database [database2] to disk=’c:\\temp_bak\\sql\\database2.mssql’"
REM MySQL Server databases
set path=%programfiles%\\MySQL\\MySQL Server 5.0\\bin
mysqldump -a -u root –password=mysql_password database1 > "c:\\temp_bak\\sql\\database1.mysql"
mysqldump -a -u root –password=mysql_password database2 > "c:\\temp_bak\\sql\\database2.mysql"
 
REM compress the backup using maximum 7z format (really good) compression
set path=%programfiles%\\7-zip
7z a -t7z -r -mx9 "d:\\backups\\%filedate%_backup.7z" "c:\\temp_bak\\*"
 
REM our backup is complete, remove the temp files
rmdir "c:\\temp_bak" /s /q

The above script, with the appropriate mail server backup, would adequately cover a Windows web server. Of course, the locations would be different on your server and you may need to append additional data to backup.

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...