Backup "caseiro" com rsync


Primeiramente gostaria de salientar que não sou o autor principal do conteúdo deste artigo. A minha intenção é de mostrar o que outros autores tem realizado (referenciando links para o conteúdo original) neste blog até como um guia pessoal para uso próprio e ainda compartilhar minhas anotações e pequenas modificações que realizo no conteúdo, desejando que possa ajudar mais alguém em algum lugar.
First and foremost, I take no credit for any of this post’s content. I am really just taking what others have done (which I have links to bellow) and am putting it on my blog for a personal reference and hopefully the small changes that I made to their guides will help someone somewhere.


sudo mkdir -p /mnt/netshare
touch ~/backup.sh
chmod u+x ~/backup.sh
gedit ~/backup.sh

#!/bin/sh

# man date
# %V = ISO week number, with Monday as first day of week (01..53)
# %w = day of week (0..6); 0 is Sunday
# %y = last two digits of year (00..99)
# %F = full date; same as %Y-%m-%d
# %m = month (01..12)



SUFFIX="y$(date +%y)w$(date +%V)d$(date +%w)"
FULL="y$(date +%y)m$(date +%m)"
SVN="y$(date +%y)w$(date +%V)"
RLOG="backup_rsync_$(date +%F).log"
SLOG="backup_svn_$(date +%F).log"
TLOG="backup_tar_$(date +%F).log"
DEST="/mnt/netshare/manager/backup"
REMOTE="/srv/autofs/manager/backup"
SRC1="/home"
SRC2="/srv/svn"


CMDLN_ARGS="$@" # Command line arguments for this script
export CMDLN_ARGS

# Run this script as root if not already.
chk_root () {
  if [ ! $( id -u ) -eq 0 ]; then
    echo "Please enter root's password."
    exec su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
    exit ${?}  # sice we're 'execing' above, we wont reach this exit unless something goes wrong.
  fi
}
# Call this function early on - now or before your script's main loop starts.
chk_root

if [ ! -d "${DEST}" ]; then
mount -L NETSHARE_GX /mnt/netshare
if [ "$?" -ne 0 ]; then echo DEST="${REMOTE}"; fi 
fi
if [ ! -d "${DEST}" ]; then
echo "Need backup folder = ${DEST}"
exit
fi
if [ ! -d "${DEST}/full_backup/" ]; then
mkdir -p "${DEST}/full_backup/"
fi
if [ -e "${RLOG}" ]; then
rm -f "${RLOG}"
fi
if [ -e "${TLOG}" ]; then
rm -f "${TLOG}"
fi
if [ -e "${SLOG}" ]; then
rm -f "${SLOG}"
fi

# DIARIO
if [ ! -d "${DEST}${SRC1}/backup_${SUFFIX}" ]; then
mkdir -p "${DEST}${SRC1}"
rsync -Lvtxbr --exclude=backup_* --exclude=full_backup --exclude=.local/share --exclude=.gvfs --backup-dir="backup_${SUFFIX}" --delete --ignore-errors "${SRC1}/" "${DEST}${SRC1}" >> "${RLOG}" 2>&1
mkdir -p "${DEST}${SRC1}/backup_${SUFFIX}/"
mv -f "${RLOG}" "${DEST}${SRC1}/backup_${SUFFIX}/"
fi

# SEMANAL
if [ ! -d "${DEST}${SRC2}/backup_${SVN}" ]; then
mkdir -p "${DEST}${SRC2}"
rsync -Lvtxbr --exclude=backup_* --exclude=full_backup --backup-dir="backup_${SVN}" --delete --ignore-errors "${SRC2}/" "${DEST}${SRC2}" >> "${SLOG}" 2>&1
mkdir -p "${DEST}${SRC2}/backup_${SVN}/"
mv -f "${SLOG}" "${DEST}${SRC2}/backup_${SVN}/"
fi

# MENSAL
if [ ! -e "${DEST}/full_backup/backup_${FULL}.tar.bz2" ]; then
tar cvpjf "${DEST}/full_backup/backup_${FULL}.tar.bz2" --exclude=/proc --exclude=/lost+found --exclude="${DEST}/full_backup/backup_${FULL}.tar.bz2" --exclude=/mnt --exclude=/sys --exclude=/media --exclude=/tmp --exclude=/srv/storage --exclude=/srv/autofs --exclude=/srv/apt-mirror --exclude=/srv/svn --exclude=/srv/samba/lixeira --exclude=/home --exclude=/root/.local/share --exclude=/var/cache/apt/archives/ --exclude=/cdrom --exclude=/usr/share/help / >> "${TLOG}" 2>&1
mv -f "${TLOG}" "${DEST}/full_backup/"
fi

if [ "${DEST}" != "${REMOTE}" ]; then
umount -l /mnt/netshare
fi

:<<'END'
UNIX Wildcards

?   Matches any one character in a filename.
*   Matches any character or characters in a filename.
[ ]  Matches one of the characters included inside the [ ] symbols.

man rsync
What to copy:
 -r, --recursive             recurse into directories
 -R, --relative              use relative path names
     --exclude=PATTERN       Exclude files matching PATTERN
     --exclude-from=FILE     Read exclude patterns from FILE
 -I, --ignore-times          Don't exclude files that match length and time
     --size-only             only use file size when determining if a file should be transferred
     --modify-window=NUM     Timestamp window (seconds) for file match (default=0)
     --include=PATTERN       Don't exclude files matching PATTERN
     --include-from=FILE     Read include patterns from FILE

How to copy it:
 -n, --dry-run               Perform a trial run with no changes made
 -l, --links                 Copy symlinks as symlinks
 -L, --copy-links            Transform symlink into referent file/dir
     --copy-unsafe-links     Only "unsafe" symlinks are transformed
     --safe-links            Ignore links outside the destination tree
 -H, --hard-links            Preserve hard links
 -D, --devices               Preserve devices (super-user only)
 -g, --group                 Preserve group
 -o, --owner                 Preserve owner (super-user only)
 -p, --perms                 Preserve permissions
 -t, --times                 Preserve times
 -S, --sparse                Handle sparse files efficiently
 -x, --one-file-system       Don't cross filesystem boundaries
 -B, --block-size=SIZE       Force a fixed checksum block-size (default 700)
 -e, --rsh=COMMAND           Specify rsh replacement
     --rsync-path=PATH       Specify path to rsync on the remote machine
     --numeric-ids           Don't map uid/gid values by user/group name
     --timeout=TIME          Set IO timeout in seconds
 -W, --whole-file            Copy whole files, no incremental checks

Destination options:
 -a, --archive               Archive mode
 -b, --backup                Make backups (see --suffix & --backup-dir)
     --backup-dir=DIR        Make backups into this directory
     --suffix=SUFFIX         Override backup suffix
 -z, --compress              Compress file data during the transfer
 -c, --checksum              Skip based on checksum, not mod-time & size
 -C, --cvs-exclude           Auto ignore files in the same way CVS does
     --existing              Only update files that already exist
     --delete                Delete files that don't exist on the sending side
     --delete-excluded       also delete excluded files on the receiving side
     --delete-after          Receiver deletes after transfer, not during
     --force                 Force deletion of directories even if not empty
     --ignore-errors         Delete even if there are IO errors
     --max-delete=NUM        Don't delete more than NUM files
     --log-format=FORMAT     Log file transfers using specified format
     --partial               Keep partially transferred files
     --progress              Show progress during transfer
 -P                          equivalent to --partial --progress
     --stats                 Give some file transfer stats
 -T  --temp-dir=DIR          Create temporary files in directory DIR
     --compare-dest=DIR      also compare destination files relative to DIR
 -u, --update                update only (don't overwrite newer files)

Misc Others:
     --address=ADDRESS       bind to the specified address
     --blocking-io           Use blocking IO for the remote shell
     --bwlimit=KBPS          Limit I/O bandwidth, KBytes per second
     --config=FILE           Specify alternate rsyncd.conf file
     --daemon                Run as a rsync daemon
     --no-detach             Do not detach from the parent
     --password-file=FILE    Get password from FILE
     --port=PORT             Specify alternate rsyncd port number
 -f, --read-batch=FILE       Read batch file
 -F, --write-batch           Write batch file
     --version               Print version number
 -v, --verbose               Increase verbosity
 -q, --quiet                 Decrease verbosity
 -4, --ipv4                  Prefer IPv4
 -6, --ipv6                  Prefer IPv6
 -h, --help                  show this help screen
END

Como o disco é FAT32 e tem limitação de tamanho vamos utilizar o comando tar com o recurso de gerar arquivos múltiplos.

Crie o script abaixo com o nome new_volume:
#! /bin/sh echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE. name=`expr $TAR_ARCHIVE : '\(.*\)-.*'` case $TAR_SUBCOMMAND in -c) ;; -d|-x|-t) test -r ${name:-$TAR_ARCHIVE}-$TAR_VOLUME || exit 1 ;; *) exit 1 esac echo ${name:-$TAR_ARCHIVE}-$TAR_VOLUME >&$TAR_FD
Para criar um arquivo multiplo com 1Gb cada:
tar -c -M -L1024 -f archive.tar -F new-volume

Para extrair de um arquivo múltiplo:
tar -x -M -f archive.tar -F new-volume

O URL específico do gadget não foi encontrado



Comments