Bash backup script for RHEL, Centos and Fedora servers

This script is written with RHEL, Centos and Fedora servers in mind, however you can edit the paths and file names to suit your distro as well. For most situations running this script on a cronjob once every 24 hours is sufficient, of course you can run it as often as you like. use this script as-is or edit/add/delete to it as you see fit.

This simple script will backup the following:

/etc
System-wide configuration files
/var/log
System-wide log files
/var/lib/mysql
All Maria / MySQL Databases
/var/www
All Apache httpd / nginx website DocumentRoots
Full list of installed RPM packages
All RPM packages listed in alphabetical order with software version version and release.
Full list of installed software repositories
All installed software repositories configured on the system in /etc/yum.repos.d/*

Additionally, this script will perform the following:

  • Reset the timezone
  • Sync the time/date with the official NTP servers
  • Backup Pruning (optional)
  • Transfer the finished backup to a remote server via SCP (optional)
  • Change file/directory ownership/permissions for the user in the global variable

Implementation

How to implement this script after you’ve edited it for your environment.

  1. Put the script in /root/cron_scripts/server_backup.sh
  2. Set the script to executable chmod +x /root/crons_cripts/server_backup.sh
  3. Here you can use crontab -e (as root) 0 3 * * * bash /root/crons_cripts/server_backup.sh > /dev/null 2>&1 …or
  4. Create a file in /etc/cron.daily/server_backup.sh with the following code: bash /root/crons_cripts/server_backup.sh > /dev/null 2>&1
  5. Set the script to executable chmod +x /etc/cron.daily/server_backup.sh

The Script

Sample Output

Last Modified: 18 Dec, 2015 at 10:45:57