To back up the data in our Linux OS, we will create a BASH shell script which will create .tar.bz2 files that contain the contents of the /home, /etc/, and /var directories. We can then transfer the .tar.bz2 files to a backup storage device. Move the the /etc/cron.daily directory.
[root@server1 ~]# cd /etc/cron.daily
Create a file named daily_backup.sh.
[root@server1 ~]# touch daily_backup.sh
Change the owner of the daily_backup.sh file to root:root. Also change the permissions of the backup file to 700, so only root has read / write / execute permission to the backup file.
[root@server1 ~]# chmod 0700 daily_backup.sh [root@server1 ~]# chown root:root daily_backup.sh
Add the following to the daily_backup.sh file:
#!/bin/bash tar -cjf /tmp/`date +%Y-%m-%d-`backup.home.tar.bz2 /home tar -cjf /tmp/`date +%Y-%m-%d-`backup.etc.tar.bz2 /etc tar -cjf /tmp/`date +%Y-%m-%d-`backup.var.tar.bz2 /var
This script uses bzip to compress the tar archive. Let's install bzip.
[root@server1 ~]# yum install bzip2
Manually run the script to ensure the .tar.bz2 files are created.
[root@server1 ~]# bash /etc/cron.daily/daily_backup.sh
To see if .tar.bz2 files were created, use the ls -lh (list long human readable) command to see if there are .tar.bz2 files in the backup directory. Notice the size of the backups in this example is 6.0M, 28K, 34M. As long as this file is greater than 0, the file contains data. If the file is zero, something went wrong, and nothing was backed up.
ls -lh /mnt/Backup -rw-r--r-- 1 root root 6.0M Jan 01 00:01 backup.etc.tar.bz2 -rw-r--r-- 1 root root 28K Jan 01 00:01 backup.home.tar.bz2 -rw-r--r-- 1 root root 34M Jan 01 00:01 backup.var.tar.bz2
Next we will want to update our BASH shell script to move these backup files to an external storage device. Let's say you have a computer with a hard drive that is dedicated as your backup storage device. You will need to share the backup storage device. If you are unsure on how to create a share, refer to these articles:
Let's say the external storage device is mounted at /mnt/backups on the local Linux OS. We can modify our BASH shell script to move the .tar.bz2 file to the /mnt/backups directory.
#!/bin/bash tar -cjf /mnt/backups/`date +-%Y-%m-%d`backup.home.tar.bz2 /home tar -cjf /mnt/backups/`date +-%Y-%m-%d`backup.etc.tar.bz2 /etc tar -cjf /mnt/backups/`date +-%Y-%m-%d`backup.var.tar.bz2 /var
For reassurance that the bz2 file contains the contents of /home or /etc/ or /var, the file can be viewed using the bzcat command.
[root@server1 ~]# bzcat /mnt/backups/<date>home.tar.bz2 [root@server1 ~]# bzcat /mnt/backups/<date>etc.tar.bz2 [root@server1 ~]# bzcat /mnt/backups/<date>var.tar.bz2
Our next task is to automatically run the backup_example.com_compress_job script once a day, every day. In Terminal, enter the below command. Ensure you are using roots account.
[root@server1 ~]# su - root [root@server1 ~]# whoamiroot
Edit the crontab file:
[root@server1 ~]# crontab -e
Add the following:
0 3 * * * bash /etc/cron.daily/daily_backup.sh