วันจันทร์ที่ 2 พฤษภาคม พ.ศ. 2554

shell script backup

First you need to connect your external hard drive and then mount it. Assuming you’ve done so already, here’s the backup script.

#! /bin/bash
echo Backup Started `date` >> ~/backuplog
mkdir /mnt/usbdrive/backups/`date +%Y%m%d`
tar -czf /mnt/usbdrive/backups/`date +%Y%m%d`/data.tar.gz /data
echo Backup Completed `date` >> ~/backuplog

The first line tells the script that is is using the BASH shell, which is located in /bin/bash on the file server. This is the standard location on nearly any UNIX machine.

The second line tells the script to write a line to a log file (located in our users home directory by specifying the tilde ~ ) with the days date. This log file can be referenced to ensure that the backup is completed each day.

The third line will create the folder on our mounted usbdrive, in the backups folder with the days date in the format YYYYMMDD (example: 20060915).

The fourth line actually creates the backup. On our file server we have created the folder /data where we store everything. We have multiple directories within the data folder for clients, internal documentation, resources, etc… and everything in that folder is compressed into an archive file using the tar command. The file is given the name data.tar.gz.

Finally, the fifth line writes a line to the backuplog telling us that the backup was completed and at what time.

NOTE: You must save this file somewhere on your computer, we recommend in the bin folder of your home directory, and you must give it execute permissions. If you want all users to be able to run the script, you need to use the chmod a+x command, if you want to keep it to yourself, then use the chmod u+x command. S, with it being saved in our home folder’s bin directory we type at the shell terminal:

chmod u+x /home/username/bin/backup

To run the script, simply type: backup from the terminal. Because we placed it in our bin directory (which is in our user’s execute path by default) the system looks there to execute the file. The entire process takes between one and two hours, and using a cronjob we have set this up to run automatically at noon everyday.

00 12 * * 1-5 /home/username/bin/backup

This CRON statement tells the operating system to run the backup command, which is located in the bin directory of the user’s home folder, at 12:00 noon, monday to friday. (We TRY to take weekends off so there isn’t a need for backups).

Now we have a simple script in place that will create a compressed archive of all our sensitive information on a daily basis and store it on our external hard drive.


////////////////////////////////////////////////////////////////////////////////////////
Backup script for Linux using tar and find

Every Linux distribution provides a range of utilities that you can use to make backups of your files. Here is the how I get the job done with crontab and a shell script using tar and find
Goal

I wanted to secure all the data-files on my system on a regular basis. Regular for me implies on an automated basis. I've tried doing a manual backup every week or so but that caused too much hassle, so effectively I stopped making backups....
Further, I wanted to have an up-to-date backup of the most essential configuration settings on my system. This would help me in case I accidentally lost some datafiles or setting on my system. In case of losing everything I would need to reinstall my Linux distribution (plus extra installed software) and restore data files and settings. I decided that a full backup of the whole system wouldn't be worth the effort (and resources!).
Choice of hardware

Say "backup" and most Unix people think "tapedrive". However, nowadays harddrives come that cheap that I chose to add an extra harddrive to my AMD 400 machine. This cheap option has the advantage that a harddrive can be mounted automatically, no need for manually inserting tapes. A disadvantage is that the backup resides in the same physical unit as the very data it is supposed to secure. However, since I do have a CD-writer om my local network I still have the option to copy a backup to a CD once in a while.
My main HD is 6Mb. The backup HD has 10Mb.
Script

After adding the drive to my machine I wrote a little shell script (for bash) that basically does the following:

* it mounts my backupdrive
* it checks the date
* every sunday it makes a full backup of some datafiles and some configuration settings, older incremental backups are removed. other days it backups files that have been accessed the last day
* it dumps all the contents of a mysql database to the backup drive and zips the file
* it unmounts the backup drive

This script (I've stored it in /root/scripts) is called every night at 3:01 AM by cron. The crontab file looks like:

1 3 * * * /root/scripts/daily_backup

Add this line using contab -e when root.

Code

Here's the actual code:

#!/bin/bash
#
# creates backups of essential files
#
DATA="/home /root /usr/local/httpd"
CONFIG="/etc /var/lib /var/named"
LIST="/tmp/backlist_$$.txt"
#
mount /mnt/backup
set $(date)
#
if test "$1" = "Sun" ; then
# weekly a full backup of all data and config. settings:
#
tar cfz "/mnt/backup/data/data_full_$6-$2-$3.tgz" $DATA
rm -f /mnt/backup/data/data_diff*
#
tar cfz "/mnt/backup/config/config_full_$6-$2-$3.tgz" $CONFIG
rm -f /mnt/backup/config/config_diff*
else
# incremental backup:
#
find $DATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
tar cfzT "/mnt/backup/data/data_diff_$6-$2-$3.tgz" "$LIST"
rm -f "$LIST"
#
find $CONFIG -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
tar cfzT "/mnt/backup/config/config_diff_$6-$2-$3.tgz" "$LIST"
rm -f "$LIST"
fi
#
# create sql dump of databases:
mysqldump -u root --password=mypass --opt mydb > "/mnt/backup/database/mydb_$6-$2-$3.sql"
gzip "/mnt/backup/database/mydb_$6-$2-$3.sql"
#
umount /mnt/backup

Discussion

data files:
All my data files are in /root, /home or /usr/local/httpd.

settings:
I chose to backup all the setting in /etc (where most essential settings are stored), /var/named (nameserver settings) and /var/lib (not sure about the importance of this one...). I might need to add more to the list but I still far from being a Unix-guru ;-). All suggestions are welcome!

tar versus cpio
The first version of this script used cpio to create backups iso tar. However, I found the cpio format not very handy for restoring single files so I chang ed it to tar. A disadvantage of using tar is that you can't (as far as I know) simply pipe the output of a find to it.
Using a construct like tar cvfz archive.tgz `find /home -ctime -1 -depth -print` caused errors for files that contained a space " " character. This problem was solved by wring the output of find to a file first (and using tar with the -T option).

1 ความคิดเห็น:

  1. Hi, Nice post thanks for sharing. Would you please consider adding a link to my website on your page. Please email me back.

    Thanks!

    Joel
    JHouston791@gmail.com

    ตอบลบ