Linux rsync: verschil tussen versies

Uit Rolandow
Ga naar: navigatie, zoeken
(Nieuwe pagina aangemaakt met '= examples of exclude list = <pre> Here are some examples of exclude/include matching: o "- *.o" would exclude all names matching *.o o …')
 
 
Regel 36: Regel 36:
 
               and will not prevent a directory that was removed on the source from being deleted on the destination.
 
               and will not prevent a directory that was removed on the source from being deleted on the destination.
 
</pre>
 
</pre>
 +
 +
 +
= incremental backup script =
 +
 +
Script to run incremental backups, based on this article: [http://www.mikerubel.org/computers/rsync_snapshots/ http://www.mikerubel.org/computers/rsync_snapshots/]
 +
 +
<source lang="bash">
 +
#!/bin/bash
 +
#
 +
# Create incremental backups of code directories
 +
#
 +
ionice -c3 -p$$
 +
 +
LOCKFILE=/var/run/rsync_backup.pid
 +
LOGFILE=/var/log/rsync_backup.log
 +
BACKUP_ROOT=/backup
 +
DAILY_ROTATE=6
 +
MONTHLY_ROTATE=12
 +
 +
f_LOG() {
 +
  echo "`date`: $@" >>$LOGFILE
 +
  echo "`date`: $@"
 +
}
 +
 +
if [ -e "$LOCKFILE" ];
 +
then
 +
  f_LOG "Process is already running at $(cat $LOCKFILE)"
 +
  exit 1
 +
else
 +
  echo $$ >$LOCKFILE
 +
fi
 +
 +
f_LOG "Start of backup at PID $$"
 +
 +
webdirs=`grep DocumentRoot /etc/apache2/sites-enabled/* | grep -v 000-default | awk '{ print $3 }'`
 +
 +
for webdir in $webdirs
 +
do
 +
  f_LOG "Backing up directory $webdir"
 +
  for (( i=$DAILY_ROTATE; i>=0; i-- ))
 +
  do
 +
    if [ "$i" -eq "$DAILY_ROTATE" ]; then
 +
      if [ -e $BACKUP_ROOT/daily$webdir.$i ]; then
 +
        rm -rf $BACKUP_ROOT/daily$webdir.$i
 +
      fi
 +
      continue
 +
    fi
 +
 +
    if [ -e $BACKUP_ROOT/daily$webdir.$i ]; then
 +
      mv $BACKUP_ROOT/daily$webdir.$i $BACKUP_ROOT/daily$webdir.$((i+1))
 +
    fi
 +
   
 +
    if [ "$i" -eq "0" ]; then
 +
      mkdir -p $BACKUP_ROOT/daily$webdir.$i
 +
      rsync -a --delete --link-dest=$BACKUP_ROOT/daily$webdir.$((i+1)) $webdir/  $BACKUP_ROOT/daily$webdir.$i/
 +
    fi
 +
  done
 +
 +
  if [ "$(date +%d)" -eq "1" ]; then
 +
    f_LOG "Doing monthly rotate"
 +
    for (( i=$MONTHLY_ROTATE; i>=0; i-- ))
 +
    do
 +
      if [ "$i" -eq "$MONTHLY_ROTATE" ]; then
 +
        if [ -e $BACKUP_ROOT/monthly$webdir.$i ]; then
 +
          rm -rf $BACKUP_ROOT/monthly$webdir.$i
 +
        fi
 +
        continue
 +
      fi
 +
 
 +
      if [ -e $BACKUP_ROOT/monthly$webdir.$i ]; then
 +
        mv $BACKUP_ROOT/monthly$webdir.$i $BACKUP_ROOT/monthly$webdir.$((i+1))
 +
      fi
 +
 
 +
      if [ "$i" -eq "0" ]; then
 +
        mkdir -p $BACKUP_ROOT/monthly$webdir.$i/
 +
        rsync -a --delete --link-dest=$BACKUP_ROOT/monthly$webdir.$((i+1)) $webdir/  $BACKUP_ROOT/monthly$webdir.$i/
 +
      fi
 +
    done
 +
  fi
 +
done
 +
 +
rm $LOCKFILE
 +
 +
f_LOG "End of backup"

Huidige versie van 17 apr 2013 om 14:27

examples of exclude list

       Here are some examples of exclude/include matching:

       o      "- *.o" would exclude all names matching *.o

       o      "- /foo" would exclude a file (or directory) named foo in the transfer-root directory

       o      "- foo/" would exclude any directory named foo

       o      "- /foo/*/bar" would exclude any file named bar which is at two levels below a directory named foo in the transfer-root directory

       o      "- /foo/**/bar" would exclude any file named bar two or more levels below a directory named foo in the transfer-root directory

       o      The combination of "+ */", "+ *.c", and "- *" would include all directories and C source files but nothing else (see also the --prune-empty-dirs option)

       o      The combination of "+ foo/", "+ foo/bar.c", and "- *" would include only the foo directory and foo/bar.c (the foo directory must be explicitly included or it would be excluded by the "*")

       The following modifiers are accepted after a "+" or "-":

       o      A / specifies that the include/exclude rule should be matched against the absolute pathname of the current item.  For example, "-/ /etc/passwd" would exclude the passwd file any time the transfer was sending files from
              the "/etc" directory, and "-/ subdir/foo" would always exclude "foo" when it is in a dir named "subdir", even if "foo" is at the root of the current transfer.

       o      A ! specifies that the include/exclude should take effect if the pattern fails to match.  For instance, "-! */" would exclude all non-directories.

       o      A C is used to indicate that all the global CVS-exclude rules should be inserted as excludes in place of the "-C".  No arg should follow.

       o      An s is used to indicate that the rule applies to the sending side.  When a rule affects the sending side, it prevents  files  from  being  transferred.   The  default  is  for  a  rule  to  affect  both  sides  unless
              --delete-excluded was specified, in which case default rules become sender-side only.  See also the hide (H) and show (S) rules, which are an alternate way to specify sending-side includes/excludes.

       o      An r is used to indicate that the rule applies to the receiving side.  When a rule affects the receiving side, it prevents files from being deleted.  See the s modifier for more info.  See also the protect (P) and risk
              (R) rules, which are an alternate way to specify receiver-side includes/excludes.

       o      A p indicates that a rule is perishable, meaning that it is ignored in directories that are being deleted.  For instance, the -C optionâs default rules that exclude things like "CVS" and "*.o" are marked as perishable,
              and will not prevent a directory that was removed on the source from being deleted on the destination.


incremental backup script

Script to run incremental backups, based on this article: http://www.mikerubel.org/computers/rsync_snapshots/

<source lang="bash">

  1. !/bin/bash
  2. Create incremental backups of code directories

ionice -c3 -p$$

LOCKFILE=/var/run/rsync_backup.pid LOGFILE=/var/log/rsync_backup.log BACKUP_ROOT=/backup DAILY_ROTATE=6 MONTHLY_ROTATE=12

f_LOG() {

 echo "`date`: $@" >>$LOGFILE
 echo "`date`: $@"

}

if [ -e "$LOCKFILE" ]; then

 f_LOG "Process is already running at $(cat $LOCKFILE)"
 exit 1

else

 echo $$ >$LOCKFILE

fi

f_LOG "Start of backup at PID $$"

webdirs=`grep DocumentRoot /etc/apache2/sites-enabled/* | grep -v 000-default | awk '{ print $3 }'`

for webdir in $webdirs do

 f_LOG "Backing up directory $webdir"
 for (( i=$DAILY_ROTATE; i>=0; i-- ))
 do
   if [ "$i" -eq "$DAILY_ROTATE" ]; then
     if [ -e $BACKUP_ROOT/daily$webdir.$i ]; then
       rm -rf $BACKUP_ROOT/daily$webdir.$i
     fi
     continue
   fi
   if [ -e $BACKUP_ROOT/daily$webdir.$i ]; then
     mv $BACKUP_ROOT/daily$webdir.$i $BACKUP_ROOT/daily$webdir.$((i+1))
   fi
   
   if [ "$i" -eq "0" ]; then
     mkdir -p $BACKUP_ROOT/daily$webdir.$i
     rsync -a --delete --link-dest=$BACKUP_ROOT/daily$webdir.$((i+1)) $webdir/  $BACKUP_ROOT/daily$webdir.$i/
   fi
 done
 if [ "$(date +%d)" -eq "1" ]; then
   f_LOG "Doing monthly rotate"
   for (( i=$MONTHLY_ROTATE; i>=0; i-- ))
   do
     if [ "$i" -eq "$MONTHLY_ROTATE" ]; then
       if [ -e $BACKUP_ROOT/monthly$webdir.$i ]; then
         rm -rf $BACKUP_ROOT/monthly$webdir.$i
       fi
       continue
     fi
 
     if [ -e $BACKUP_ROOT/monthly$webdir.$i ]; then
       mv $BACKUP_ROOT/monthly$webdir.$i $BACKUP_ROOT/monthly$webdir.$((i+1))
     fi
 
     if [ "$i" -eq "0" ]; then
       mkdir -p $BACKUP_ROOT/monthly$webdir.$i/
       rsync -a --delete --link-dest=$BACKUP_ROOT/monthly$webdir.$((i+1)) $webdir/  $BACKUP_ROOT/monthly$webdir.$i/
     fi
   done
 fi

done

rm $LOCKFILE

f_LOG "End of backup"