Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
204b73ddcd | |||
25954d5178 | |||
87f3bca955 | |||
a64219c321 | |||
7c3eb9ed24 | |||
a2c4e54c47 |
12
README.md
12
README.md
@ -1,2 +1,10 @@
|
|||||||
# simple_backup
|
# simple-backup
|
||||||
A simple backup script using rsync
|
A simple backup script
|
||||||
|
|
||||||
|
## Description
|
||||||
|
simple-backup is just a bash script that allows you to backup your files.
|
||||||
|
It reads from a configuration file the files/directories that must be copied,
|
||||||
|
the destination directory for the backup and a few other options.
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
rsync is used to perform the backup.
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#Help function
|
#Help function
|
||||||
function help_function {
|
function help_function {
|
||||||
echo "simple_backup, version 1.2.3"
|
echo "simple_backup, version 1.3.1"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Usage: $0 [OPTIONS]"
|
echo "Usage: $0 [OPTIONS]"
|
||||||
echo ""
|
echo ""
|
||||||
@ -54,9 +54,8 @@ function read_conf {
|
|||||||
if [[ ! -f "$CONFIG" ]]; then
|
if [[ ! -f "$CONFIG" ]]; then
|
||||||
#If default config file doesn't exist, exit
|
#If default config file doesn't exist, exit
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: Configuration file not found" | tee $ERR
|
echo "Error: Configuration file not found" | tee $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
@ -71,10 +70,8 @@ function read_conf {
|
|||||||
if [[ ! -f "$CONFIG" ]]; then
|
if [[ ! -f "$CONFIG" ]]; then
|
||||||
#If the provided configuration file doesn't exist, exit
|
#If the provided configuration file doesn't exist, exit
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: Configuration file not found" | tee -a $ERR
|
echo "Error: Configuration file not found" | tee -a $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
mv $WARN "$HOMEDIR/.simple_backup/warnings.log"
|
mv $WARN "$HOMEDIR/.simple_backup/warnings.log"
|
||||||
@ -107,9 +104,8 @@ function read_conf {
|
|||||||
if [[ -z "$BACKUP_DEV" || ! -d "$BACKUP_DEV" ]]; then
|
if [[ -z "$BACKUP_DEV" || ! -d "$BACKUP_DEV" ]]; then
|
||||||
#If the backup directory is not set or doesn't exist, exit
|
#If the backup directory is not set or doesn't exist, exit
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: Output folder \"$BACKUP_DEV\" not found" | tee -a $ERR
|
echo "Error: Output folder \"$BACKUP_DEV\" not found" | tee -a $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
@ -173,9 +169,8 @@ function parse_options {
|
|||||||
|
|
||||||
if [[ -z "$input" ]]; then
|
if [[ -z "$input" ]]; then
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: bad options format" | tee -a $ERR
|
echo "Error: bad options format" | tee -a $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
@ -205,9 +200,8 @@ function parse_options {
|
|||||||
|
|
||||||
if [[ -z "$BACKUP_DEV" || ! -d "$BACKUP_DEV" ]]; then
|
if [[ -z "$BACKUP_DEV" || ! -d "$BACKUP_DEV" ]]; then
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: output folder \"$BACKUP_DEV\" not found" | tee -a $ERR
|
echo "Error: output folder \"$BACKUP_DEV\" not found" | tee -a $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
@ -263,13 +257,16 @@ function parse_options {
|
|||||||
|
|
||||||
if [[ ! -d "/home/$2" ]]; then
|
if [[ ! -d "/home/$2" ]]; then
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: user $2 doesn't exist" | tee -a $ERR
|
echo "Error: user $2 doesn't exist" | tee -a $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "HOMEDIR/.simple_backup/simple_backup.log"
|
if [[ ! -d "$HOMEDIR/.simple_backup" ]]; then
|
||||||
mv $ERR "HOMEDIR/.simple_bakup/errors.log"
|
mkdir "$HOMEDIR/.simple_backup"
|
||||||
mv $WARN "HOMEDIR/.simple_backup/warnings.log"
|
fi
|
||||||
|
|
||||||
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
|
mv $WARN "$HOMEDIR/.simple_backup/warnings.log"
|
||||||
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@ -285,7 +282,7 @@ function parse_options {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
HOMEDIR="/home/$2"
|
HOMEDIR="/home/$2"
|
||||||
config="/home/$2/.simple_backup/config"
|
CONFIG="/home/$2/.simple_backup/config"
|
||||||
USER="$2"
|
USER="$2"
|
||||||
|
|
||||||
read_conf "$config"
|
read_conf "$config"
|
||||||
@ -295,9 +292,8 @@ function parse_options {
|
|||||||
|
|
||||||
*)
|
*)
|
||||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||||
|
echo "Backup failed"
|
||||||
echo "Error: Option $1 not recognised. Use 'simple-backup -h' to see available options" | tee -a $ERR
|
echo "Error: Option $1 not recognised. Use 'simple-backup -h' to see available options" | tee -a $ERR
|
||||||
#If libnotify is installed, show desktop notification that backup failed
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
@ -339,9 +335,8 @@ fi
|
|||||||
|
|
||||||
if [[ -z $n_in || $n_in -eq 0 ]]; then
|
if [[ -z $n_in || $n_in -eq 0 ]]; then
|
||||||
echo "$(date): Backup finished (no files copied)" >> $LOG
|
echo "$(date): Backup finished (no files copied)" >> $LOG
|
||||||
|
echo "Backup finished"
|
||||||
echo "Warning: no valid input selected. Nothing to do" | tee -a $WARN
|
echo "Warning: no valid input selected. Nothing to do" | tee -a $WARN
|
||||||
#If libnotify is installed, show desktop notification that backup finished
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished (warnings)"
|
|
||||||
|
|
||||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||||
@ -351,8 +346,7 @@ if [[ -z $n_in || $n_in -eq 0 ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$(date): Starting backup" > $LOG
|
echo "$(date): Starting backup" > $LOG
|
||||||
#If libnotify is installed, show desktop notification that backup is starting
|
echo "Starting backup..."
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Starting backup"
|
|
||||||
|
|
||||||
#If specified, keep the last n backups and remove the others. Default: keep all
|
#If specified, keep the last n backups and remove the others. Default: keep all
|
||||||
if [[ -n $KEEP ]]; then
|
if [[ -n $KEEP ]]; then
|
||||||
@ -360,7 +354,8 @@ if [[ -n $KEEP ]]; then
|
|||||||
N_BACKUP=$(($N_BACKUP-1))
|
N_BACKUP=$(($N_BACKUP-1))
|
||||||
|
|
||||||
if [[ $N_BACKUP -gt $KEEP ]]; then
|
if [[ $N_BACKUP -gt $KEEP ]]; then
|
||||||
echo "$(date): Removing old backups..." >> $LOG
|
echo "$(date): Removing old backups" >> $LOG
|
||||||
|
echo "Removing old backups..."
|
||||||
REMOVE=$(mktemp)
|
REMOVE=$(mktemp)
|
||||||
find $BACKUP_DEV/simple_backup/* -maxdepth 0 -type d | sort | head -n $(($N_BACKUP - $KEEP)) >> $REMOVE
|
find $BACKUP_DEV/simple_backup/* -maxdepth 0 -type d | sort | head -n $(($N_BACKUP - $KEEP)) >> $REMOVE
|
||||||
|
|
||||||
@ -370,6 +365,8 @@ if [[ -n $KEEP ]]; then
|
|||||||
echo "Removed backup: $line" >> $LOG
|
echo "Removed backup: $line" >> $LOG
|
||||||
done<$REMOVE
|
done<$REMOVE
|
||||||
|
|
||||||
|
echo "Removed old backups"
|
||||||
|
|
||||||
rm $REMOVE
|
rm $REMOVE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -380,6 +377,8 @@ if [[ ! -z "$INPUTS" ]]; then
|
|||||||
sort "$INPUTS" -o "$INPUTS"
|
sort "$INPUTS" -o "$INPUTS"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Copying files. This may take a long time..."
|
||||||
|
|
||||||
if [[ -z "$LAST_BACKUP" ]]; then
|
if [[ -z "$LAST_BACKUP" ]]; then
|
||||||
rsync -acrv -H -X -R --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" --ignore-missing-args >> $LOG 2>> $ERR
|
rsync -acrv -H -X -R --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" --ignore-missing-args >> $LOG 2>> $ERR
|
||||||
else
|
else
|
||||||
@ -389,13 +388,13 @@ fi
|
|||||||
#Update the logs
|
#Update the logs
|
||||||
if [[ $(cat $ERR | wc -l) -gt 0 ]]; then
|
if [[ $(cat $ERR | wc -l) -gt 0 ]]; then
|
||||||
echo "$(date): Backup finished with errors" >> $LOG
|
echo "$(date): Backup finished with errors" >> $LOG
|
||||||
error_flag=1
|
echo "Backup finished with errors"
|
||||||
elif [[ $(cat $WARN | wc -l) -gt 0 ]]; then
|
elif [[ $(cat $WARN | wc -l) -gt 0 ]]; then
|
||||||
echo "$(date): Backup finished with warnings" >> $LOG
|
echo "$(date): Backup finished with warnings" >> $LOG
|
||||||
error_flag=2
|
echo "Backup finished (warnings)"
|
||||||
else
|
else
|
||||||
echo "$(date): Backup finished" >> $LOG
|
echo "$(date): Backup finished" >> $LOG
|
||||||
error_flag=0
|
echo "Backup finished"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Fix ownership and permissions of log files if needed
|
#Fix ownership and permissions of log files if needed
|
||||||
@ -425,12 +424,4 @@ fi
|
|||||||
BACKUP_DIR_FULL=$(readlink -f "$BACKUP_DIR")
|
BACKUP_DIR_FULL=$(readlink -f "$BACKUP_DIR")
|
||||||
ln -sf "$BACKUP_DIR_FULL" "$BACKUP_DEV/simple_backup/last_backup"
|
ln -sf "$BACKUP_DIR_FULL" "$BACKUP_DEV/simple_backup/last_backup"
|
||||||
|
|
||||||
if [[ $error_flag -eq 0 ]]; then
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished"
|
|
||||||
elif [[ $error_flag -eq 1 ]]; then
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished (errors)"
|
|
||||||
else
|
|
||||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished (warnings)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
exit 0
|
exit 0
|
Reference in New Issue
Block a user