Sunteți pe pagina 1din 4

Automating backups with tar It is always interesting to automate the tasks of a backup.

Automation offers enormous opportunities for using your Linux server to achieve the goals you set. The following example below is our backup script, called backup.cron.This script is designed to run on any computer by changing only the four variables: i.COMPUTER ii.DIRECTORIES iii.BACKUPDIR iv.TIMEDIR We suggest that you set this script up and run it at the beginning of the month for the first time, and then run it for a month before making major changes. In our example below we do the backup to a directory on the local server BACKUPDIR, but you could modify this script to do it to a tape on the local server or via an NFS mounted file system. 1. Create the backup script backup.cron file, touch /etc/cron.daily/backup.cron and add the following lines to this backup file:
#!/bin/sh # full and incremental backup script # created 07 February 2000 # Based on a script by Daniel O'Callaghan <danny@freebsd.org> # and modified by Gerhard Mourani <gmourani@videotron.ca> #Change the 5 variables below to fit your computer/backup COMPUTER=deep DIRECTORIES="/home" BACKUPDIR=/backups TIMEDIR=/backups/last-full TAR=/bin/tar # # # # name of this computer directoris to backup where to store the backups where to store time of full backup # name and locaction of tar

#You should not have to change anything below here PATH=/usr/local/bin:/usr/bin:/bin DOW=`date +%a` # Day of the week e.g. Mon DOM=`date +%d` # Date of the Month e.g. 27 DM=`date +%d%b` # Date and Month e.g. 27Sep # # # # # # On the 1st of the month a permanet full backup is made Every Sunday a full backup is made - overwriting last Sundays backup The rest of the time an incremental backup is made. Each incremental backup overwrites last weeks incremental backup of the same name. if NEWER = "", then tar backs up all files in the directories

# otherwise it backs up files newer than the NEWER date. NEWER # gets it date from the file written every Sunday. # Monthly full backup if [ $DOM = "01" ]; then NEWER="" $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DM.tar $DIRECTORIES fi # Weekly full backup if [ $DOW = "Sun" ]; then NEWER="" NOW=`date +%d-%b` # Update full backup date echo $NOW > $TIMEDIR/$COMPUTER-full-date $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES # Make incremental backup - overwrite last weeks else # Get date of last full backup NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`" $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES fi

Example 33-1. Backup directory of a week Here is an abbreviated look of the backup directory after one week:
[root@deep] /# ls -l /backups/

total 22217 -rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x

1 1 1 1 1 1 1 1 2

root root root root root root root root root

root root root root root root root root root

10731288 6879 2831 7924 11923013 5643 3152 4567 1024

Feb Feb Feb Feb Feb Feb Feb Feb Feb

7 7 7 7 7 7 7 7 7

11:24 11:24 11:24 11:25 11:24 11:25 11:25 11:25 11:20

deep-01Feb.tar deep-Fri.tar deep-Mon.tar deep-Sat.tar deep-Sun.tar deep-Thu.tar deep-Tue.tar deep-Wed.tar last-full

: The directory where to store the backups BACKUPDIR, and the directory where to store time of full backup TIMEDIR must exist or be created before the use of the backup-script, or you will receive an error message. 2. If you are not running this backup script from the beginning of the month 01month-year, the incremental backups will need the time of the Sunday backup to be able to work properly. If you start in the middle of the week, you will need to create the time file in the TIMEDIR. To create the time file in

the TIMEDIR directory, use the following command:


[root@deep] /# date +%d%b < /backups/last-full/myserver-full-date

Where /backups/last-full is our variable TIMEDIR wherein we want to store the time of the full backup, and myserver-full-date is the name of our server e.g. deep, and our time file consists of a single line with the present date i.e. 15-Feb. 3. Make this script executable and change its default permissions to be writable only by the super-user root 755.
[root@deep] /# chmod 755 /etc/cron.daily/backup.cron

: Because this script is in the /etc/cron.daily directory, it will be automatically run as a cron job at one o'clock in the morning every day.

Remote Server backup-script


#!/bin/bash # Authur :- Rahul Patil # Pupose :- Backup remote server using rsync # Note :- SSH keys required on all server { Password less ssh access required} SSH="/usr/bin/ssh" ECHO="/bin/echo" SCP="/usr/bin/scp" RSYNC="/usr/bin/rsync" TAR="/bin/tar" LOGFILE="/var/log/copy.log" # Path of Local Store BACKUP_PATH="/path/to/local/backup/folder" # Path of file/directories of remote server REMOTE_PATH="/opt/test" # remote user USER="root" # list of server seperated by space # if you have list in file then you can use `cat /path/of/file` # for server in `cat /path/of/file` for servers in 192.168.3.1 192.168.3.2 do BACKUP_LOCATION="${BACKUP_PATH}/$servers" echo $'\n\n' >> $LOGFILE $ECHO "*************{ Backup Started on $server @$(date +%F)@ }***************************** *********" >> $LOGFILE $ECHO $servers >> $LOGFILE # if dir not exist then created them [[ ! -d ${BACKUP_LOCATION} ]] && mkdir -p ${BACKUP_LOCATION}

# tar remote folder $SSH ${USER}@${servers} $TAR -czf /tmp/backup_$(date +%F).tar.gz ${REMOTE_PATH} # move tar backup from remote server to local path $RSYNC -av --remove-source-files ${USER}@${servers}:/tmp/backup_$(date +%F).tar.gz $ {BACKUP_LOCATION} >> $LOGFILE $ECHO "*************{ Backup End on $server @$(date +%F)@ }********************************* *****" >> $LOGFILE

S-ar putea să vă placă și