Last active
May 22, 2017 06:14
-
-
Save natanfelles/6fdc8b9424273e66a59b0d30a0bd1514 to your computer and use it in GitHub Desktop.
Backup specific directories to external disk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Backup specific directories to external disk | |
# Created by Natan Felles | |
# Install rsync to do this script work. Execute "apt-get install rsync" in Debian distros | |
# To discover the UUID of the external disk, use gnome-disks ou execute "blkid" | |
# Do not forget to configure the variables LOGDIR, LOGFILE, DIRS and UUID | |
# Local log dir path | |
LOGDIR=/var/log/backup/ | |
# Local log file name | |
LOGFILE="`date +%Y%m%d`-`date +%H%M%S`".log | |
# Local log location | |
LOG=$LOGDIR$LOGFILE | |
# Directories to backup without bars. Use "" if you want backup the / dir | |
DIRS=("etc" "home") | |
# The hard disk UUID to save the backup files | |
UUID=166cb153-c1ba-4d6c-acf0-2fffb1bb1bbf | |
# Message | |
echo "Backup started at `date`. Please wait." | |
echo "If you wants to see logs, open the file $LOG" | |
# Actions | |
sudo echo "/--------------------------------------------------------------------------------" >> $LOG | |
sudo echo "| Sync started at `date`" >> $LOG | |
for d in "${DIRS[@]}" | |
do | |
sudo echo "|--------------------------------------------------------------------------------" >> $LOG | |
sudo echo "| /$d status:" >> $LOG | |
echo "- Sync from /$d to /mnt/$UUID/$d/ is running" | |
sudo echo "|--------------------------------------------------------------------------------" >> $LOG | |
sudo rsync -Cravzp /$d/ /mnt/$UUID/$d/ >> $LOG | |
done | |
sudo echo "|--------------------------------------------------------------------------------" >> $LOG | |
sudo echo "| Sync finished at `date`" >> $LOG | |
sudo echo "\--------------------------------------------------------------------------------" >> $LOG | |
sudo echo "Backup was finished at `date`. Bye!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To backup automatically you can use a cron job.
Edit /etc/crontab and add this line:
0 12 * * * root /root/backup.sh
It says that the file localized in /root will be executed every day at 12 hours.