Basic Server Backup Script Using tar & bash

This is a basic script that can be set to run automatically every day, every week or every hour if you like and backup key files and directories.

Applicable to Centos Versions:

  • Centos 5.x

Requirements

Explanation of requirements.

  1. Root or sudo with the appropriate privileges.

Doing the Work

Basic description of what will be done and what is expected.

  1. Explanation and download link for the script:
  2. This Script will backup the following directories as tar files with the date and location in the title of the resulting tar files:

    /etc (location of most of the systems configuration files)
    /home (location of users home directories/files)
    /var/log (location of most of the systems logs files)
    /var/www (location of webserver files and content)
    /var/lib/mysql (location of raw MySQL databases, all databases are first dumped to a .sql file before the directory is tarred)

    The tar files will be stored in:
    /root/serverbackup/$DATE/ These files can then be moved or another separate storage media to complete the backup.

    The tar files will be named like this example:
    01-Oct-2009-Thu-04:46:42-AM-PDT-host.example.org-etc.tar.gz
  3. Automating the script to run once daily:
  4. Copy The Script above into the /root directory.
    chmod +x serverbackup.sh
    cd /etc/cron.daily; vi 00serverbackup (hit 'i' to insert text)
    Add this line:
    bash /root/serverbackup.sh > /dev/null 2>&1
    Hit 'esc' :wq to exit vi
    chmod +x 00serverbackup
  5. Running the script:
  6. Copy The Script to /root
    chmod +x serverbackup.sh
    ./serverbackup.sh

    The script will output to the screen so you'll be able to see what's being done. Once it's finished check the /root/serverbackup directory for the tar files.
    IMPORTANT: You'll also need a MySQL DBA with full DBA privileges. The default DBA in the script is: backupdba password: dbapass. Change these to suit your own needs.

Troubleshooting

How to test

Explanation troubleshooting basics and expectations.

  1. Make sure you’ve properly chmoded the script:
  2. ls -als serverbackup.sh (make sure the "x" permission is set for whatever level user you are, root should be able to execute any executable)

Common problems and fixes

Describe common problems here, include links to known common problems if on another site

More Information

Any additional information or notes.

Disclaimer

We test this stuff on our own machines, really we do. But you may run into problems, if you do, come to #centos on irc.freenode.net

Added Reading


© 2012 CentosHelp.org