User Tools

Site Tools


ubuntu:backups:backup_using_rsync

Ubuntu - Backups - Backup using rsync

Backup Server1

#!/bin/bash
#
# This fully backs up server1.
#
now=$(date +"%Y%m%d_%H%M%S")
backup_dir="/media/peter/Disk1/Server1/backup-"$now
mkdir "$backup_dir"
rsync -aAXvr -e ssh --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found/*"}  peter@192.168.1.2:/ $backup_dir/

BASH Script

Generates a backup named for the current date and time in the specified location.

This script:

  • will find the latest current backup, and will then generate a new backup hardlinked to the previous one, saving a LOT of disk space.
  • each backup will then only contain the changes made since the last one.
#!/bin/bash
#
# Backup script.
#
SCRIPTDIR=/Backups
BACKUPDIR=/Backups
SOURCE=”/COMPLETE=yes
clonemode=no
 
while getopts “d:c” opt
do
  case $opt in
    d) date=$OPTARG
    ;;
    c) clonemode=yes
    ;;
  esac
done
 
echo $date
echo $clonemode
if [ "$clonemode" = "yes" ]
then
  SOURCE="$BACKUPDIR/current/"
  COMPLETE=no
fi
 
mkdir -p $BACKUPDIR/incomplete \
  && cd $BACKUPDIR \
  && rsync -av --numeric-ids --delete \
    --exclude-from=$SCRIPTDIR/excludelist \
    --link-dest=$BACKUPDIR/current/ \
    $SOURCE $BACKUPDIR/incomplete/ \
    || COMPLETE=no
 
if [ "$COMPLETE" = "yes" ]
then
  date=`date "+%Y%m%d.%H%M%S"`
  echo "completing - moving current link"
  mv $BACKUPDIR/incomplete $BACKUPDIR/$date \
    && rm -f $BACKUPDIR/current \
    && ln -s $date $BACKUPDIR/current
else
  echo "not renaming or linking to \"current\""
fi

NOTE: To check the size of each backup, run the following in the folder your backups are in.

du -sch *

ALERT: A very important safety tip.

Since each file is generate through hardlinks, do not under any circumstances try and edit files in the backups.

Let's say you haven't changed /etc/passwd in a long time. While it looks like you have 30 copies of it, you actually only have one (i.e., the hardlink).

If you go and edit /etc/passwd, you will change it on every backup at once, effectively. So don't do that.

It's safe to just flat-out delete a backup, you won't trash anything. Just don't edit things.

ubuntu/backups/backup_using_rsync.txt · Last modified: 2021/01/04 22:55 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki