7 Commits

Author SHA1 Message Date
Laurent Cozic 9128fa2a6f Merge branch 'master' of https://github.com/tyriis/rsync-time-backup into tyriis-master
Conflicts:
	rsync_tmbackup.sh
2013-11-12 15:40:00 +08:00
Laurent Cozic 1f5aabc31f Merge branch 'master' of https://github.com/laurent22/rsync-time-backup
Conflicts:
	rsync_tmbackup.sh
2013-11-12 15:31:20 +08:00
Laurent Cozic b481985f2a Move more setup code inside loop 2013-11-12 11:14:02 +08:00
Laurent 00ba40b617 Merge pull request #11 from Flimm/master
Deal with arguments with special characters better
2013-11-07 01:06:50 -08:00
David D Lowe 24540f33ee Handle source and dest arguments with spaces 2013-11-07 08:51:12 +00:00
nils biesalski c046c4dd42 Changed regex for grep iirc POSIX mode
http://stackoverflow.com/questions/6901171/is-d-not-supported-by-greps-basic-expressions
2013-10-31 13:51:08 +01:00
David D Lowe 396b42d483 Deal with arguments with special characters better
Script now handles filenames with special characters (such as spaces,
hard tabs, newlines, double quotes and a dash prefix). The only
exceptions are filenames with single quotes. In this case, the script
will fail early with a clear error message.
2013-10-30 13:45:48 +00:00
+54 -46
View File
@@ -5,15 +5,15 @@
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
fn_log_info() { fn_log_info() {
echo "[RB INFO] $1" echo "rsync_tmbackup: $1"
} }
fn_log_warn() { fn_log_warn() {
echo "[RB WARN] $1" echo "rsync_tmbackup: [WARNING] $1"
} }
fn_log_error() { fn_log_error() {
echo "[RB ERROR] $1" echo "rsync_tmbackup: [ERROR] $1"
} }
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@@ -21,7 +21,7 @@ fn_log_error() {
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
fn_terminate_script() { fn_terminate_script() {
echo "SIGINT caught" echo "rsync_tmbackup: SIGINT caught."
exit 1 exit 1
} }
@@ -35,6 +35,13 @@ SRC_FOLDER=${1%/}
DEST_FOLDER=${2%/} DEST_FOLDER=${2%/}
EXCLUSION_FILE=$3 EXCLUSION_FILE=$3
for arg in "$SRC_FOLDER" "$DEST_FOLDER" "$EXCLUSION_FILE"; do
if [[ "$arg" == *"'"* ]]; then
fn_log_error 'Arguments may not have any single quote characters.'
exit 1
fi
done
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Check that the destination drive is a backup drive # Check that the destination drive is a backup drive
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
@@ -67,12 +74,12 @@ fi
# Setup additional variables # Setup additional variables
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
BACKUP_FOLDER_PATTERN="\d\d\d\d-\d\d-\d\d-\d\d\d\d\d\d" BACKUP_FOLDER_PATTERN="^[[:digit:]][[:digit:]][[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]]-[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]$"
NOW=$(date +"%Y-%m-%d-%H%M%S") NOW=$(date +"%Y-%m-%d-%H%M%S")
PROFILE_FOLDER="$HOME/.rsync_tmbackup" PROFILE_FOLDER="$HOME/.rsync_tmbackup"
LOG_FILE="$PROFILE_FOLDER/$NOW.log" LOG_FILE="$PROFILE_FOLDER/$NOW.log"
DEST=$DEST_FOLDER/$NOW DEST=$DEST_FOLDER/$NOW
LAST_TIME=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | tail -n 1) LAST_TIME=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 1)
PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME PREVIOUS_DEST=$DEST_FOLDER/$LAST_TIME
INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress INPROGRESS_FILE=$DEST_FOLDER/backup.inprogress
@@ -94,10 +101,10 @@ if [ -f "$INPROGRESS_FILE" ]; then
# - Last backup is moved to current backup folder so that it can be resumed. # - Last backup is moved to current backup folder so that it can be resumed.
# - 2nd to last backup becomes last backup. # - 2nd to last backup becomes last backup.
fn_log_info "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there." fn_log_info "$INPROGRESS_FILE already exists - the previous backup failed or was interrupted. Backup will resume from there."
LINE_COUNT=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | tail -n 2 | wc -l) LINE_COUNT=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 2 | wc -l)
mv $PREVIOUS_DEST $DEST mv -- "$PREVIOUS_DEST" "$DEST"
if [ "$LINE_COUNT" -gt 1 ]; then if [ "$LINE_COUNT" -gt 1 ]; then
SECOND_LAST_TIME=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | tail -n 2 | head -n 1) SECOND_LAST_TIME=$(ls -1 -- "$DEST_FOLDER" | grep "$BACKUP_FOLDER_PATTERN" | tail -n 2 | head -n 1)
LAST_TIME=$SECOND_LAST_TIME LAST_TIME=$SECOND_LAST_TIME
else else
LAST_TIME="" LAST_TIME=""
@@ -106,36 +113,37 @@ if [ -f "$INPROGRESS_FILE" ]; then
fi fi
fi fi
# ----------------------------------------------------------------------------- # Run in a loop to handle the "No space left on device" logic.
# Check if we are doing an incremental backup (if previous backup exists) or not
# -----------------------------------------------------------------------------
LINK_DEST_OPTION=""
if [ "$LAST_TIME" == "" ]; then
fn_log_info "No previous backup - creating new one."
else
# If the path is relative, it needs to be relative to the destination. To keep
# it simple, just use an absolute path. See http://serverfault.com/a/210058/118679
PREVIOUS_DEST=`cd \`dirname "$PREVIOUS_DEST"\`; pwd`"/"`basename "$PREVIOUS_DEST"`
fn_log_info "Previous backup found - doing incremental backup from $PREVIOUS_DEST"
LINK_DEST_OPTION="--link-dest=$PREVIOUS_DEST"
fi
# -----------------------------------------------------------------------------
# Create destination folder if it doesn't already exists
# -----------------------------------------------------------------------------
if [ ! -d "$DEST" ]; then
fn_log_info "Creating destination $DEST"
mkdir -p $DEST
fi
# -----------------------------------------------------------------------------
# Start backup
# -----------------------------------------------------------------------------
# Run in a loop to handle the "No space left on device" logic
while [ "1" ]; do while [ "1" ]; do
# -----------------------------------------------------------------------------
# Check if we are doing an incremental backup (if previous backup exists) or not
# -----------------------------------------------------------------------------
LINK_DEST_OPTION=""
if [ "$LAST_TIME" == "" ]; then
fn_log_info "No previous backup - creating new one."
else
# If the path is relative, it needs to be relative to the destination. To keep
# it simple, just use an absolute path. See http://serverfault.com/a/210058/118679
PREVIOUS_DEST=`cd \`dirname -- "$PREVIOUS_DEST"\`; pwd`"/"`basename -- "$PREVIOUS_DEST"`
fn_log_info "Previous backup found - doing incremental backup from $PREVIOUS_DEST"
LINK_DEST_OPTION="--link-dest=$PREVIOUS_DEST"
fi
# -----------------------------------------------------------------------------
# Create destination folder if it doesn't already exists
# -----------------------------------------------------------------------------
if [ ! -d "$DEST" ]; then
fn_log_info "Creating destination $DEST"
mkdir -p -- "$DEST"
fi
# -----------------------------------------------------------------------------
# Start backup
# -----------------------------------------------------------------------------
LOG_FILE="$PROFILE_FOLDER/$(date +"%Y-%m-%d-%H%M%S").log" LOG_FILE="$PROFILE_FOLDER/$(date +"%Y-%m-%d-%H%M%S").log"
fn_log_info "Starting backup..." fn_log_info "Starting backup..."
@@ -151,21 +159,21 @@ while [ "1" ]; do
CMD="$CMD --delete-excluded" CMD="$CMD --delete-excluded"
CMD="$CMD --one-file-system" CMD="$CMD --one-file-system"
CMD="$CMD --archive" CMD="$CMD --archive"
CMD="$CMD --progress"
CMD="$CMD --itemize-changes" CMD="$CMD --itemize-changes"
CMD="$CMD --verbose" CMD="$CMD --verbose"
CMD="$CMD --log-file '$LOG_FILE'" CMD="$CMD --log-file '$LOG_FILE'"
if [ "$EXCLUSION_FILE" != "" ]; then if [ "$EXCLUSION_FILE" != "" ]; then
CMD="$CMD --exclude-from \"$EXCLUSION_FILE\"" # We've already checked that $EXCLUSION_FILE doesn't contain a single quote
CMD="$CMD --exclude-from '$EXCLUSION_FILE'"
fi fi
CMD="$CMD $LINK_DEST_OPTION" CMD="$CMD $LINK_DEST_OPTION"
CMD="$CMD $SRC_FOLDER/ $DEST/" CMD="$CMD -- '$SRC_FOLDER/' '$DEST/'"
CMD="$CMD | grep -E '^deleting|[^/]$'" CMD="$CMD | grep -E '^deleting|[^/]$'"
fn_log_info "Running command:" fn_log_info "Running command:"
fn_log_info "$CMD" fn_log_info "$CMD"
touch $INPROGRESS_FILE touch -- "$INPROGRESS_FILE"
eval $CMD eval $CMD
RSYNC_EXIT_CODE=$? RSYNC_EXIT_CODE=$?
@@ -185,6 +193,7 @@ while [ "1" ]; do
rm -- "$LOG_FILE" rm -- "$LOG_FILE"
if [ "$NO_SPACE_LEFT" == "0" ]; then if [ "$NO_SPACE_LEFT" == "0" ]; then
# TODO: -y flag
read -p "It looks like there is no space left on the destination. Delete old backup? (Y/n) " yn read -p "It looks like there is no space left on the destination. Delete old backup? (Y/n) " yn
case $yn in case $yn in
[Nn]* ) exit 0;; [Nn]* ) exit 0;;
@@ -197,9 +206,7 @@ while [ "1" ]; do
fn_log_error "No space left on device, and no old backup to delete." fn_log_error "No space left on device, and no old backup to delete."
exit 1 exit 1
fi fi
# TODO: handle case where only two backup folders are left. In which case, need to remove --link-dest option
OLDEST_BACKUP=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | head -n 1) OLDEST_BACKUP=$(ls -1 $DEST_FOLDER | grep "$BACKUP_FOLDER_PATTERN" | head -n 1)
if [ "$OLDEST_BACKUP" == "" ]; then if [ "$OLDEST_BACKUP" == "" ]; then
fn_log_error "No space left on device, and cannot get path to oldest backup to delete." fn_log_error "No space left on device, and cannot get path to oldest backup to delete."
@@ -227,7 +234,8 @@ while [ "1" ]; do
exit $RSYNC_EXIT_CODE exit $RSYNC_EXIT_CODE
fi fi
rm $INPROGRESS_FILE rm -- "$INPROGRESS_FILE"
# TODO: grep for "^rsync error:.*$" in log
fn_log_info "Backup completed without errors." fn_log_info "Backup completed without errors."
exit 0 exit 0
done done