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.
- Put the script in
/root/cron_scripts/server_backup.sh
- Set the script to executable
chmod +x /root/crons_cripts/server_backup.sh
- Here you can use
crontab -e
(as root)0 3 * * * bash /root/crons_cripts/server_backup.sh > /dev/null 2>&1
…or - 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
- Set the script to executable
chmod +x /etc/cron.daily/server_backup.sh
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #!/bin/bash #Server Backup ######################## ### Global Variables ### ######################## USER="your_username" DIR="system_backup" DATE=`date +%d-%b-%Y-%a-%I:%M:%S-%p-%Z` SERVER=`uname -n` ########################################### ### Setting System Time, Date, Location ### ########################################### ## Reset System Timezone After tzdata Updates ## echo "Resetting System Timezone..." ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime ## Sync System Time ## #echo "Setting System Time..." ntpdate pool.ntp.org ###################### ### Backup Pruning ### ###################### echo "Removing Stale Backups..." ## Delete any backups over 15 minutes old ## #find /home/$USER/$DIR/ -mmin +15 -exec rm -rf {} + ## Delete any backups over 15 days old ## find /home/$USER/$DIR/ -mtime +15 -exec rm -rf {} + ## *CAUTION* Delete all previous backups *CAUTION* ## #echo "Removing Previous Backups..." #rm -rf /home/$USER/$DIR ##################################### ### Create Dated Backup Directory ### ##################################### echo "Starting System Backup for $SERVER..." mkdir -p /home/$USER/$DIR/$DATE ##################################################### ### Backup Config, Databases, Logs, Documentroots ### ##################################################### echo "Backing up $SERVER Logs..." tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-logs.tar.gz /var/log ### Main Server Configuration Files ### echo "Backing up $SERVER Configuration..." tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-etc.tar.gz /etc ### MySQL / MariaDB Database Dump/Backup ### echo "Dumping $SERVER MySQL Databases..." mysqldump --events -u backupdba -pA$n_y7#Qpv3 --all-databases > /var/lib/mysql/all_databases.sql mysqldump --events -u backupdba -pA$n_y7#Qpv3 example_com > /var/lib/mysql/example_com.sql && mv /var/lib/mysql/example_com.sql /var/www/example.com/sql/example_com.sql mysqldump --events -u backupdba -pA$n_y7#Qpv3 example_org > /var/lib/mysql/example_org.sql && mv /var/lib/mysql/example_org.sql /var/www/example.org/sql/example_org.sql mysqldump --events -u backupdba -pA$n_y7#Qpv3 example_net > /var/lib/mysql/example_net.sql && mv /var/lib/mysql/example_net.sql /var/www/example.net/sql/example_net.sql ### MySQL / MariaDB Data Directory Backup ### echo "Backing up $SERVER MySQL Directories & Files..." tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-sql.tar.gz /var/lib/mysql ### Backup Apache httpd / nginx Website DocumentRoots ### echo "Backing up $SERVER DocumentRoots..." ### Individual Apache httpd / nginx DocumentRoots ### tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-www-example.com.tar.gz /var/www/example.com/ tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-www-example.org.tar.gz /var/www/example.org/ tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-www-example.net.tar.gz /var/www/example.net/ ### Entire Main Apache httpd / nginx DocumentRoot ### tar -cvzPf /home/$USER/$DIR/$DATE/$DATE-$SERVER-www.tar.gz /var/www/ ######################################### ### Installed RPMs & Yum Repos Backup ### ######################################### echo "Backing up $SERVER Installed RPMs & Yum Repos..." rpm -qa --qf '%{name}-%{version}-%{release}\n' | sort -d > /home/$USER/$DIR/$DATE/rpmlist.txt yum repolist all > /home/$USER/$DIR/$DATE/repolist.txt ################################################ ### File & Directory Ownership / Permissions ### ################################################ echo "Changing File Ownership..." chown -Rf $USER:$USER /home/$USER/$DIR echo "Changing File Permissions..." chmod -Rf 755 /home/$USER/$DIR ############################ ### scp to remote server ### ############################ #scp -r /home/$USER/$DIR/$DATE your_username@remotehost.com:/some/existing/remote/directory/bar echo "$SERVER Backups Complete." |
Sample Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [webdev@example.com ~]$ ls -als system_backup total 84 4 drwxr-xr-x 21 webdev webdev 4096 Dec 17 03:50 . 4 drwx------ 6 webdev webdev 4096 Nov 16 20:19 .. 4 drwxr-xr-x 2 webdev webdev 4096 Dec 2 03:57 02-Dec-2015-Wed-03:44:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 3 03:54 03-Dec-2015-Thu-03:42:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 4 03:41 04-Dec-2015-Fri-03:28:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 5 03:51 05-Dec-2015-Sat-03:38:02-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 6 03:35 06-Dec-2015-Sun-03:21:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 7 03:34 07-Dec-2015-Mon-03:20:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 8 03:28 08-Dec-2015-Tue-03:15:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 9 03:25 09-Dec-2015-Wed-03:11:02-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 10 03:37 10-Dec-2015-Thu-03:25:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 11 03:25 11-Dec-2015-Fri-03:11:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 12 04:07 12-Dec-2015-Sat-03:51:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 13 03:34 13-Dec-2015-Sun-03:20:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 14 03:36 14-Dec-2015-Mon-03:25:02-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 15 03:38 15-Dec-2015-Tue-03:25:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 16 03:33 16-Dec-2015-Wed-03:13:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 17 04:03 17-Dec-2015-Thu-03:50:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 5 03:38 18-Nov-2015-Wed-03:17:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 8 03:15 21-Nov-2015-Sat-03:11:01-AM-PST 4 drwxr-xr-x 2 webdev webdev 4096 Dec 12 03:51 25-Nov-2015-Wed-03:09:02-AM-PST [webdev@example.com sysbackup]# cd 02-Dec-2015-Wed-03\:44\:01-AM-PST/ [webdev@example.com 02-Dec-2015-Wed-03:44:01-AM-PST]# ls -lash total 5.9G 4.0K drwxr-xr-x 2 webdev webdev 4.0K Dec 2 03:57 . 4.0K drwxr-xr-x 21 webdev webdev 4.0K Dec 17 03:50 .. 2.6M -rwxr-xr-x 1 webdev webdev 2.6M Dec 2 03:56 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-etc.tar.gz 9.7M -rwxr-xr-x 1 webdev webdev 9.7M Dec 2 03:44 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-logs.tar.gz 251M -rwxr-xr-x 1 webdev webdev 251M Dec 2 03:57 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-sql.tar.gz 2.4G -rwxr-xr-x 1 webdev webdev 2.4G Dec 2 03:50 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-www-example.com.tar.gz 397M -rwxr-xr-x 1 webdev webdev 397M Dec 2 03:51 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-www-example.org.tar.gz 181M -rwxr-xr-x 1 webdev webdev 181M Dec 2 03:51 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-www-example.net.tar.gz 3.1G -rwxr-xr-x 1 webdev webdev 3.1G Dec 2 03:56 02-Dec-2015-Wed-03:44:01-AM-PST-example.com-www.root.tar.gz 8.0K -rwxr-xr-x 1 webdev webdev 6.2K Dec 2 03:58 repolist.txt 16K -rwxr-xr-x 1 webdev webdev 15K Dec 2 03:57 rpmlist.txt |
Last Modified: 19 Apr, 2023 at 09:34:57