Skip to content

Instantly share code, notes, and snippets.

@fabiopiovam
Last active August 29, 2015 14:12
Show Gist options
  • Save fabiopiovam/2789f339a0e015c920b7 to your computer and use it in GitHub Desktop.
Save fabiopiovam/2789f339a0e015c920b7 to your computer and use it in GitHub Desktop.
Backup into removable device using rsync
#!/bin/bash
############################################################################################
##### Script created by Romulo Grandini with my changes ####################################
##### (read the orginal script in 'backup_files_original.sh' file) #########################
##### Publication: http://www.vivaolinux.com.br/script/Backup-em-HD-Externa-com-Rsync #####
############################################################################################
# USE
# ./backup_files.sh SRC DEST
SRC="$1"
DEST="$2"
LOG="/var/www/backup_logs/`date +%d-%m-%y | tr / -`.log"
echo "" >> $LOG
echo "" >> $LOG
echo "######################################" >> $LOG
echo "######## BACKUP FILES #########" >> $LOG
echo "######################################" >> $LOG
echo "" >> $LOG
echo Initiating script................[OK] >> $LOG
echo Cleaning old logs................[OK] >> $LOG
# specific change to raspberry pi: Enable the high power USB mode
echo "" >> $LOG
echo "Enable the high power USB mode (1.2A)" >> $LOG
echo 38 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio38/direction
echo 1 > /sys/class/gpio/gpio38/value
# Search and remove logs older than 5 days
find /var/www/backup_logs -type f -mtime +5 -exec rm -rf {} ";"
umount -l $DEVICE
mount_attempts=0
echo "" >> $LOG
echo "Mounting of device ..." >> $LOG
mount_device(){
# Define /dev of removable device (To get the device of blkid use blkid as root in terminal)
DEVICE=`/sbin/blkid |grep 61762bb5-c13b-4e8d-870a-48d982926e95 | awk -F: '{print $1}'`
umount -l $DEVICE
# Verify if device is mounted
echo Executing: mount -t ext4 $DEVICE $DEST >> $LOG
if mount -t ext4 $DEVICE $DEST
then
{
echo "" >> $LOG
echo "DEVICE IS MOUNTED: Initiating the sync..." >> $LOG
echo "" >> $LOG
#rsync -auv --delete $SRC $DEST >> $LOG
rsync -auv $SRC $DEST >> $LOG
echo "" >> $LOG
echo "SUCCESS: BACKUP COMPLETED!" >> $LOG
}
else
{
if [ $mount_attempts -lt 10 ]
then {
$(( mount_attempts += 1 ))
echo Attempt to mount: $mount_attempts of 10 ... >> $LOG
sleep 15
mount_device
echo "" >> $LOG
}
else {
echo "" >> $LOG
echo "ERROR TO MOUNT THE REMOVABLE DEVICE: BACKUP CANCELLED!" >> $LOG
}
fi
}
fi
}
mount_device
echo "End report." >> $LOG
umount -l $DEVICE
echo 0 > /sys/class/gpio/gpio38/value
#!/bin/bash
#####################################
##### BACKUP v2.0 #####
##### Romulo Grandini 06/03/12 #####
#####################################
# Dica de como agendar no cron para backup diario em tal horario
# 0 17 * * * /home/scripts/rbackup.sh
# DEFININDO VARIAVEIS
# Origens de Backup
ORIGEM1="/mnt/hd-1"
ORIGEM2="/mnt/hd-2"
# Destino de backups (Uma Hd Externa)
DESTINO="/mnt/backup"
# Caminho dos logs e nome por data
LOG="/var/www/backup_logs/`date +%d-%m-%y | tr / -`.log"
# Logs de leitura iniciais
echo "" >> $LOG
echo "" >> $LOG
echo "######################################" >> $LOG
echo "### BACKUP v2.0 ###"
>> $LOG
echo "######## BACKUP AUTOMATIZADO #########" >> $LOG
echo "######################################" >> $LOG
echo "" >> $LOG
echo Iniciando script................[OK] >> $LOG
echo Limpando logs antigos ..........[OK] >> $LOG
# Procura e remove logs com mais de 5 dias
find /var/www/backup_logs -type f -mtime +5 -exec rm -rf {} ";"
# Define o /dev da HD Externa (Para pegar o blkid do device use blkid como root
no terminal)
DEVICE=`/sbin/blkid |grep 10C83EF3C83ED6A5 | awk -F: '{print $1}'`
# Desmonta e monta a HD Externa
umount -l $DEVICE
# Verifica se HD esta montada ou nao
if mount -t ntfs-3g $DEVICE $DESTINO
then
{
# Se estiver montado, inicia a sincronia de Hd-1 e Hd-3 somente
echo "" >> $LOG
echo "HD EXTERNA OK: Iniciando a sincronia de discos..." >> $LOG
echo "" >> $LOG
rsync -auv --delete $ORIGEM1 $DESTINO >> $LOG
rsync -auv --delete $ORIGEM2 $DESTINO >> $LOG
echo "" >> $LOG
echo "BACKUP REALIZADO COM SUCESSO!" >> $LOG
}
else
{
echo "" >> $LOG
echo "ERRO AO MONTAR HD EXTERNA: BACKUP CANCELADO!" >> $LOG
}
fi
# Desmonta a HD ao finalizar
echo "Fim do Relatorio." >> $LOG
umount -l $DEVICE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment