Backup Scripts for Your Server

In a previous comment I was asked to post a backup procedure that I use on my root servers. The following is a backup stategy that I use on my SuSE servers.

Backup directories

You create the following directories:

  • /root/backup/scripts
  • /root/backup/files

Hourly backup script

Create /root/backup/scripts/1h.sh with the following contents and make it executable:
tar -czf /root/backup/files/partial_`date +%Y-%m-%d_%H-%M`.tar.gz `find /etc /home /srv /var -type f -mmin -60` 2> /dev/null
find /root/backup/files -name 'partial*' -mtime +7 -exec unlink '{}' \;

This creates backups of files that were changed in the past 60 minutes. Also it deletes those hourly backups that are older than 7 days.

Daily backup script

Create /root/backup/scripts/1d.sh with the following contents and make it executable:
rm /root/backup/files/complete*
tar -czf /root/backup/files/complete.tar.gz /etc/ /home/ /srv/ /var/ 2> /dev/null
lftp ftp://#username#:#password#@#host# -e "put /root/backup/files/complete.tar.gz; quit"

This creates a full daily backup of the important files of your server. That complete backup is then copied to an external FTP server. That way you can access your backup even if the whole machine becomes inaccessible. You may need to install the lftp program if it’s not on your machine by default.

You don’t need to create separate MySQL dumps. The MySQL data are stored in the /var structure, in my case under /var/lib/mysql/#database#.

Comments

One thought on “Backup Scripts for Your Server

  1. Really great Peter!
    This will improve my old backup strategy a lot, but how big is your FTP Server? When I would save the whole /var directory with all logfiles, I would need a own server just for the logs ;-) . However, I will test it ASAP…
    Thank you!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>