Se você não instalou rsync
, instale-os
sudo apt-get install rsync
Criar script backup_script.sh
#!/bin/bash
# Script to backup personal files to the external USB drive.
# Specify the mount point here (DO NOT end mount_point with a forward-slash).
mount_point='/mnt/'
echo "#####"
echo ""
# Check whether target volume is mounted, and mount it if not.
if ! mountpoint -q ${mount_point}/; then
echo "Mounting the external USB drive."
echo "Mountpoint is ${mount_point}"
if ! mount ${mount_point}; then
echo "An error code was returned by mount command!"
exit 5
else echo "Mounted successfully.";
fi
else echo "${mount_point} is already mounted.";
fi
# Target volume **must** be mounted by this point. If not, die screaming.
if ! mountpoint -q ${mount_point}/; then
echo "Mounting failed! Cannot run backup without backup volume!"
exit 1
fi
echo "Preparing to transfer differences using rsync."
# Use the year to create a new backup directory each year.
current_year='date +%Y'
# Now construct the backup path, specifying the mount point followed by the path
# to our backup directory, finishing with the current year.
# (DO NOT end backup_path with a forward-slash.)
backup_path=${mount_point}'/rsync-backup/'${current_year}
echo "Backup storage directory path is ${backup_path}"
echo "Starting backup of /home/XXX/Pictures . . . "
mkdir --parents ${backup_path}/Pictures
# This time use the -a flag with the tee command, so that it appends to the end
# of the rsync-output.txt file rather than start a new file from scratch.
sudo rsync --archive --verbose --human-readable --itemize-changes --progress --no-o --no-g \
--delete --delete-excluded \
/home/XXX/Pictures/ ${backup_path}/Pictures/ 2>&1 | tee -a /home/XXX/rsync-output.txt
echo ""
echo "####"
Você deve alterar
- ponto de montagem do seu ext USB - no exemplo eu uso
/mnt/
- caminho para a pasta que você deseja fazer backup - por exemplo, eu uso
/home/XXX/Pictures
- lugar para gravar arquivo de log - no exemplo, uso
/home/XXX/
Conceder privilégio de execução de script
chmod +x /path_to_script/backup_script.sh
Configure crontab
para executar este comando em cada x
de tempo
Para editar crontab
run
crontab -e
Para executar o comando em cada código de 5 min é
*/5 * * * * /path_to_script/backup_script.sh
em todas as horas
0 */1 * * * /path_to_script/backup_script.sh
para ser executado às 4h
0 4 * * * /path_to_script/backup_script.sh
Opcionalmente, você pode enviar o arquivo de log para o seu e-mail com o comando
mail -s "SMB backup" [email protected] < /path_to_log/rsync-output.txt
Configure o cron para fazer por você
0 4 * * * mail -s "SMB backup" [email protected] < /path_to_log/rsync-output.txt