summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md10
-rw-r--r--cron_hourly_backup.sh20
-rw-r--r--rsync_tmbackup.sh48
3 files changed, 28 insertions, 50 deletions
diff --git a/README.md b/README.md
index b4417a6..1d63c9d 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Rsync time backup
-Time Machine style backup with rsync. Should work on Linux, Mac OS X and Windows with Cygwin.
+Time Machine style backup with rsync. Should work on Linux (tested on some distros), Mac OS X (fully tested) and Windows with Cygwin (not tested yet but feeback would be welcome).
# Installation
@@ -36,12 +36,18 @@ An optional exclude file can be provided as a third parameter. It should be comp
* Exclude file - support for pattern-based exclusion via the `--exclude-from` rsync parameter.
-* The application is one bash script that can be easily edited.
+* Automatically purge old backups - within 24 hours, all backups are kept. Within one month, the most recent backup for each day is kept. For all previous backups, the most recent of each month is kept.
+
+* "latest" symlink that points to the latest successful backup.
+
+* The application is just one bash script that can be easily edited.
# TODO
* Minor changes (see TODO comments in the source).
+* Backup to remote drive?
+
# LICENSE
[MIT](http://opensource.org/licenses/MIT)
diff --git a/cron_hourly_backup.sh b/cron_hourly_backup.sh
deleted file mode 100644
index 8db0b8a..0000000
--- a/cron_hourly_backup.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/bash
-#
-# Installation instructions:
-#
-# 1. Declare where you installed rsync_tmbackup.sh to:
-
-TMBACKUP="/usr/local/bin/rsync_tmbackup.sh"
-
-# 2. Copy this script to /etc/cron.hourly
-#
-# 3. Run `sudo chmod 755 /etc/cron.hourly/cron_hourly_backup.sh`
-#
-
-# Ubuntu Fedora
-for DEST in /media/*/*/ /run/media/*/*/; do
- [ -f "$DEST/backup.marker" ] || continue
- USERNAME=$(basename $(dirname "$DEST"))
- EXCLUDES=$(find "/home/$USERNAME/.backup.excludes" 2>/dev/null)
- bash "$TMBACKUP" "/home/$USERNAME" "$DEST" "$EXCLUDES"
-done
diff --git a/rsync_tmbackup.sh b/rsync_tmbackup.sh
index be4c982..8e94931 100644
--- a/rsync_tmbackup.sh
+++ b/rsync_tmbackup.sh
@@ -57,8 +57,8 @@ SRC_FOLDER="${1%/}"
DEST_FOLDER="${2%/}"
EXCLUSION_FILE="$3"
-for arg in "$SRC_FOLDER" "$DEST_FOLDER" "$EXCLUSION_FILE"; do
- if [[ "$arg" == *"'"* ]]; then
+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
@@ -92,7 +92,6 @@ EPOCH=$(date "+%s")
KEEP_ALL_DATE=$(($EPOCH - 86400)) # 1 day ago
KEEP_DAILIES_DATE=$(($EPOCH - 2678400)) # 31 days ago
-
export IFS=$'\n' # Better for handling spaces in filenames.
PROFILE_FOLDER="$HOME/.$APPNAME"
DEST="$DEST_FOLDER/$NOW"
@@ -130,7 +129,7 @@ fi
while : ; do
# -----------------------------------------------------------------------------
- # Check if we are doing an incremental backup (if previous backup exists) or not
+ # Check if we are doing an incremental backup (if previous backup exists).
# -----------------------------------------------------------------------------
LINK_DEST_OPTION=""
@@ -157,28 +156,29 @@ while : ; do
# Purge certain old backups before beginning new backup.
# -----------------------------------------------------------------------------
- # Default value for $prev ensures that the most recent backup is never deleted.
- prev="0000-00-00-000000"
- for fname in $(fn_find_backups); do
- date=$(basename "$fname")
- stamp=$(fn_parse_date $date)
+ # Default value for $PREV ensures that the most recent backup is never deleted.
+ PREV="0000-00-00-000000"
+ for FILENAME in $(fn_find_backups | sort -r); do
+ BACKUP_DATE=$(basename "$FILENAME")
+ TIMESTAMP=$(fn_parse_date $BACKUP_DATE)
# Skip if failed to parse date...
- [ -n "$stamp" ] || continue
-
- if [ $stamp -ge $KEEP_ALL_DATE ]; then
- : # Don't expire any backups in this range
+ if [ -z "$TIMESTAMP" ]; then
+ fn_log_warn "Could not parse date: $FILENAME"
+ continue
+ fi
- elif [ $stamp -ge $KEEP_DAILIES_DATE ]; then
+ if [ $TIMESTAMP -ge $KEEP_ALL_DATE ]; then
+ true
+ elif [ $TIMESTAMP -ge $KEEP_DAILIES_DATE ]; then
# Delete all but the most recent of each day.
- [ "${date:0:10}" == "${prev:0:10}" ] && fn_expire_backup "$fname"
-
+ [ "${BACKUP_DATE:0:10}" == "${PREV:0:10}" ] && fn_expire_backup "$FILENAME"
else
# Delete all but the most recent of each month.
- [ "${date:0:7}" == "${prev:0:7}" ] && fn_expire_backup "$fname"
+ [ "${BACKUP_DATE:0:7}" == "${PREV:0:7}" ] && fn_expire_backup "$FILENAME"
fi
- prev=$date
+ PREV=$BACKUP_DATE
done
# -----------------------------------------------------------------------------
@@ -196,8 +196,6 @@ while : ; do
CMD="$CMD --numeric-ids"
CMD="$CMD --links"
CMD="$CMD --hard-links"
- CMD="$CMD --delete"
- CMD="$CMD --delete-excluded"
CMD="$CMD --one-file-system"
CMD="$CMD --archive"
CMD="$CMD --itemize-changes"
@@ -234,16 +232,10 @@ while : ; do
rm -- "$LOG_FILE"
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
- case $yn in
- [Nn]* ) exit 0;;
- esac
-
fn_log_warn "No space left on device - removing oldest backup and resuming."
BACKUP_FOLDER_COUNT=$(fn_find_backups | wc -l)
- if [ "$BACKUP_FOLDER_COUNT" -lt "2" ]; then
+ if [[ "$BACKUP_FOLDER_COUNT" -lt "2" ]]; then
fn_log_error "No space left on device, and no old backup to delete."
exit 1
fi
@@ -272,7 +264,7 @@ while : ; do
rm -rf -- "$DEST_FOLDER/latest"
ln -vs -- "$NOW" "$DEST_FOLDER/latest"
- rm -- "$INPROGRESS_FILE"
+ rm -f -- "$INPROGRESS_FILE"
# TODO: grep for "^rsync error:.*$" in log
fn_log_info "Backup completed without errors."
exit 0