Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
133a6b1a48 | |||
584532e2c7 | |||
97d1a14260 | |||
71a2b6d01e | |||
6649925ec9 | |||
871c8d6543 | |||
7035352cfd | |||
d31080e5e4 | |||
25954d5178 | |||
87f3bca955 | |||
a64219c321 | |||
7c3eb9ed24 |
12
README.md
12
README.md
@ -1,2 +1,10 @@
|
||||
# simple_backup
|
||||
A simple backup script using rsync
|
||||
# simple-backup
|
||||
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.
|
||||
|
217
simple-backup
217
simple-backup
@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
#Copyright 2015 Daniele Fucini <dfucini@gmail.com>
|
||||
#Copyright 2017 Daniele Fucini <dfucini@gmail.com>
|
||||
|
||||
#This program is free software: you can redistribute it and/or modify
|
||||
#it under the terms of the GNU General Public License as published by
|
||||
@ -19,7 +19,7 @@
|
||||
|
||||
#Help function
|
||||
function help_function {
|
||||
echo "simple_backup, version 1.2.3"
|
||||
echo "simple_backup, version 1.4.1"
|
||||
echo ""
|
||||
echo "Usage: $0 [OPTIONS]"
|
||||
echo ""
|
||||
@ -36,8 +36,8 @@ function help_function {
|
||||
echo " Default: keep all."
|
||||
echo "-u, --user USER User performing the backup."
|
||||
echo " Default: current user."
|
||||
echo " WARNING: This currently doesn't work with"
|
||||
echo " other options!"
|
||||
echo "-s, --checksum Use the checksum rsync option to compare files"
|
||||
echo " (MUCH slower)."
|
||||
echo ""
|
||||
echo "If no option is given, the program uses the default"
|
||||
echo "configuration file: $HOMEDIR/.simple_backup/config."
|
||||
@ -54,9 +54,15 @@ function read_conf {
|
||||
if [[ ! -f "$CONFIG" ]]; then
|
||||
#If default config file doesn't exist, exit
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
echo "Backup failed"
|
||||
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"
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
@ -71,9 +77,15 @@ function read_conf {
|
||||
if [[ ! -f "$CONFIG" ]]; then
|
||||
#If the provided configuration file doesn't exist, exit
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
echo "Backup failed"
|
||||
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"
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
@ -107,9 +119,15 @@ function read_conf {
|
||||
if [[ -z "$BACKUP_DEV" || ! -d "$BACKUP_DEV" ]]; then
|
||||
#If the backup directory is not set or doesn't exist, exit
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
echo "Backup failed"
|
||||
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"
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
@ -128,7 +146,7 @@ function read_conf {
|
||||
if [[ ! -d "$BACKUP_DIR" ]]; then
|
||||
mkdir -p "$BACKUP_DIR/$DATE"
|
||||
else
|
||||
#If previous backup(s) exist(s), save link to the last backup
|
||||
#If previous backups exist, save link to the last backup
|
||||
LAST_BACKUP=$(readlink -f "$BACKUP_DIR/last_backup")
|
||||
mkdir "$BACKUP_DIR/$DATE"
|
||||
fi
|
||||
@ -143,7 +161,6 @@ function read_conf {
|
||||
|
||||
#Parse options
|
||||
function parse_options {
|
||||
i=1
|
||||
n_in=0
|
||||
|
||||
#Create a temporary file to store inputs
|
||||
@ -171,26 +188,9 @@ function parse_options {
|
||||
do
|
||||
input="$2"
|
||||
|
||||
if [[ -z "$input" ]]; then
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
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 $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
mv $WARN "$HOMEDIR/.simple_backup/warnings.log"
|
||||
|
||||
rm $INPUTS
|
||||
rm $EXCLUDE
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -e "$input" ]]; then
|
||||
echo "Warning: input \"${INPUTS[$i]}\" not found. Skipping" | tee -a $WARN
|
||||
echo "Warning: input \"$input\" not found. Skipping" | tee -a $WARN
|
||||
else
|
||||
i=$((i+1))
|
||||
n_in=$((n_in+1))
|
||||
echo "$input" >> "$INPUTS"
|
||||
fi
|
||||
@ -205,16 +205,22 @@ function parse_options {
|
||||
|
||||
if [[ -z "$BACKUP_DEV" || ! -d "$BACKUP_DEV" ]]; then
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
echo "Backup failed"
|
||||
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"
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
mv $WARN "$HOMEDIR/.simple_backup/warnings.log"
|
||||
|
||||
rm $INPUTS
|
||||
rm $EXCLUDES
|
||||
rm $EXCLUDE
|
||||
|
||||
exit 1
|
||||
fi
|
||||
@ -258,46 +264,56 @@ function parse_options {
|
||||
;;
|
||||
|
||||
-u | --user)
|
||||
rm "$EXCLUDE"
|
||||
rm "$INPUTS"
|
||||
if [[ ! -d "/home/$2" ]]; then
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
echo "Backup failed"
|
||||
echo "Error: user $2 doesn't exist" | tee -a $ERR
|
||||
|
||||
if [[ ! -d "/home/$2" ]]; then
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
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"
|
||||
mv $ERR "HOMEDIR/.simple_bakup/errors.log"
|
||||
mv $WARN "HOMEDIR/.simple_backup/warnings.log"
|
||||
|
||||
exit 1
|
||||
if [[ ! -d "$HOMEDIR/.simple_backup" ]]; then
|
||||
mkdir "$HOMEDIR/.simple_backup"
|
||||
fi
|
||||
|
||||
if [[ ! -d "/home/$2/.simple_backup" ]]; then
|
||||
mkdir "/home/$2/.simple_backup"
|
||||
|
||||
echo "Created directory \"$HOMEDIR/.simple_backup\"."
|
||||
echo "Copy there sample configuration and edit it"
|
||||
echo "to your needs before running the backup."
|
||||
|
||||
exit 1
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
HOMEDIR="/home/$2"
|
||||
config="/home/$2/.simple_backup/config"
|
||||
USER="$2"
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
mv $WARN "$HOMEDIR/.simple_backup/warnings.log"
|
||||
|
||||
read_conf "$config"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
return
|
||||
if [[ ! -d "/home/$2/.simple_backup" ]]; then
|
||||
mkdir "/home/$2/.simple_backup"
|
||||
|
||||
echo "Created directory \"$HOMEDIR/.simple_backup\"."
|
||||
fi
|
||||
|
||||
HOMEDIR="/home/$2"
|
||||
USER="$2"
|
||||
|
||||
shift
|
||||
;;
|
||||
|
||||
-s | --checksum)
|
||||
OPTIONS="-arcvh -H -X -R"
|
||||
;;
|
||||
|
||||
*)
|
||||
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
|
||||
#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"
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
@ -319,6 +335,7 @@ ERR=$(mktemp)
|
||||
WARN=$(mktemp)
|
||||
|
||||
HOMEDIR="$HOME"
|
||||
OPTIONS="-arvh -H -X -R"
|
||||
|
||||
#Check number of parameters
|
||||
if [[ "$#" -eq 0 ]]; then
|
||||
@ -327,21 +344,61 @@ if [[ "$#" -eq 0 ]]; then
|
||||
|
||||
echo "Created directory \"$HOMEDIR/.simple_backup\"."
|
||||
echo "Copy there sample configuration and edit it"
|
||||
echo "to your needs before running the backup."
|
||||
echo "to your needs before running the backup,"
|
||||
echo "or pass options on the command line."
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
read_conf
|
||||
else
|
||||
parse_options $@
|
||||
parse_options "$@"
|
||||
|
||||
if [[ $n_in -gt 0 && ( -z $BACKUP_DIR || ! -d $BACKUP_DIR ) ]]; then
|
||||
#If the backup directory is not set or doesn't exist, exit
|
||||
echo "$(date): Backup failed (see errors.log)" >> $LOG
|
||||
echo "Backup failed"
|
||||
echo "Error: Output folder \"$BACKUP_DEV\" not found" | tee -a $ERR
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
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
|
||||
elif [[ $n_in -eq 0 && -z $BACKUP_DIR ]]; then
|
||||
if [[ ! -d "$HOMEDIR/.simple_backup" ]]; then
|
||||
mkdir "$HOMEDIR/.simple_backup"
|
||||
|
||||
echo "Created directory \"$HOMEDIR/.simple_backup\"."
|
||||
echo "Copy there sample configuration and edit it"
|
||||
echo "to your needs before running the backup,"
|
||||
echo "or pass options on the command line."
|
||||
|
||||
exit 1
|
||||
fi
|
||||
|
||||
read_conf $HOMEDIR/.simple_backup/config
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z $n_in || $n_in -eq 0 ]]; then
|
||||
if [[ $n_in -eq 0 ]]; then
|
||||
echo "$(date): Backup finished (no files copied)" >> $LOG
|
||||
echo "Backup finished"
|
||||
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)"
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
if [[ ! -z $USER ]]; then
|
||||
chown $USER:$USER $LOG && chmod 644 $LOG
|
||||
chown $USER:$USER $ERR && chmod 644 $ERR
|
||||
chown $USER:$USER $WARN && chmod 644 $WARN
|
||||
fi
|
||||
|
||||
mv $LOG "$HOMEDIR/.simple_backup/simple_backup.log"
|
||||
mv $ERR "$HOMEDIR/.simple_backup/errors.log"
|
||||
@ -351,8 +408,7 @@ if [[ -z $n_in || $n_in -eq 0 ]]; then
|
||||
fi
|
||||
|
||||
echo "$(date): Starting backup" > $LOG
|
||||
#If libnotify is installed, show desktop notification that backup is starting
|
||||
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Starting backup"
|
||||
echo "Starting backup..."
|
||||
|
||||
#If specified, keep the last n backups and remove the others. Default: keep all
|
||||
if [[ -n $KEEP ]]; then
|
||||
@ -360,7 +416,8 @@ if [[ -n $KEEP ]]; then
|
||||
N_BACKUP=$(($N_BACKUP-1))
|
||||
|
||||
if [[ $N_BACKUP -gt $KEEP ]]; then
|
||||
echo "$(date): Removing old backups..." >> $LOG
|
||||
echo "$(date): Removing old backups" >> $LOG
|
||||
echo "Removing old backups..."
|
||||
REMOVE=$(mktemp)
|
||||
find $BACKUP_DEV/simple_backup/* -maxdepth 0 -type d | sort | head -n $(($N_BACKUP - $KEEP)) >> $REMOVE
|
||||
|
||||
@ -370,6 +427,8 @@ if [[ -n $KEEP ]]; then
|
||||
echo "Removed backup: $line" >> $LOG
|
||||
done<$REMOVE
|
||||
|
||||
echo "Removed old backups"
|
||||
|
||||
rm $REMOVE
|
||||
fi
|
||||
|
||||
@ -380,22 +439,24 @@ if [[ ! -z "$INPUTS" ]]; then
|
||||
sort "$INPUTS" -o "$INPUTS"
|
||||
fi
|
||||
|
||||
echo "Copying files. This may take a long time..."
|
||||
|
||||
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 $OPTIONS --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" --ignore-missing-args >> $LOG 2>> $ERR
|
||||
else
|
||||
rsync -acrv -H -X -R --link-dest="$LAST_BACKUP" --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" --ignore-missing-args >> $LOG 2>> $ERR
|
||||
rsync $OPTIONS --link-dest="$LAST_BACKUP" --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" --ignore-missing-args >> $LOG 2>> $ERR
|
||||
fi
|
||||
|
||||
#Update the logs
|
||||
if [[ $(cat $ERR | wc -l) -gt 0 ]]; then
|
||||
echo "$(date): Backup finished with errors" >> $LOG
|
||||
error_flag=1
|
||||
echo "Backup finished with errors"
|
||||
elif [[ $(cat $WARN | wc -l) -gt 0 ]]; then
|
||||
echo "$(date): Backup finished with warnings" >> $LOG
|
||||
error_flag=2
|
||||
echo "Backup finished (warnings)"
|
||||
else
|
||||
echo "$(date): Backup finished" >> $LOG
|
||||
error_flag=0
|
||||
echo "Backup finished"
|
||||
fi
|
||||
|
||||
#Fix ownership and permissions of log files if needed
|
||||
@ -425,12 +486,4 @@ fi
|
||||
BACKUP_DIR_FULL=$(readlink -f "$BACKUP_DIR")
|
||||
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
|
||||
|
Reference in New Issue
Block a user