Navigation Menu

Skip to content

Commit

Permalink
Extract new backup action out of archive.
Browse files Browse the repository at this point in the history
  • Loading branch information
inkarkat committed Apr 13, 2012
1 parent 16e3356 commit c56b74b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
14 changes: 3 additions & 11 deletions actions/archive
Expand Up @@ -16,7 +16,7 @@
# under the terms of the GNU General Public License.
# See http://www.gnu.org/copyleft/gpl.txt
#
# @(#)archive 002 (19-Mar-2012) todo.txt-cli-ex
# @(#)archive 003 (13-Apr-2012) todo.txt-cli-ex
###############################################################################

action=$1
Expand Down Expand Up @@ -104,18 +104,10 @@ processItems 'x' "$DONE_FILE" 'archived' "$items" "$@"
processItems 'X' "$TRASH_FILE" 'trashed' "$items" "$@"


if [ $# -eq 0 ]; then
if [ $# -eq 0 -a -x "$TODO_ACTIONS_DIR/backup" ]; then
# This is a complete, regular archive.
# Use this occasion to make a backup.
if type -P writebackup >/dev/null; then
readonly TODO_BACKUPDIR="${TODO_DIR}.backup/"
if [ -d "$TODO_BACKUPDIR" ]; then
cp -f "$TODO_FILE" "${TODO_BACKUPDIR}/todo.txt"
writebackup "${TODO_BACKUPDIR}/todo.txt"
else
writebackup "$TODO_FILE"
fi
fi
"$TODO_FULL_SH" backup
fi

# Defragment blank lines.
Expand Down
56 changes: 56 additions & 0 deletions actions/backup
@@ -0,0 +1,56 @@
#!/bin/bash
###############################################################################
##
# FILE: backup
# PRODUCT: todo.txt-cli-ex
# AUTHOR: Ingo Karkat <ingo@karkat.de>
# DATE CREATED: 13-Apr-2012
#
###############################################################################
# CONTENTS:
#
# REMARKS:
#
# COPYRIGHT: (C) 2012 Ingo Karkat
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License.
# See http://www.gnu.org/copyleft/gpl.txt
#
# @(#)backup 001 13-Apr-2012 todo.txt-cli-ex
###############################################################################

action=$1
shift

[ "$action" = "usage" ] && {
echo " $(basename $0)"
echo " Writes a backup copy of todo.txt."
echo ""
exit
}

# Configuration to place the backups into a different directory.
: ${TODOTXT_BACKUP_DIR="${TODO_DIR}.backup"}
# Use a custom backup script if it exists, fall back on a simple copy to a
# timestamp; do nothing when the command is "none".
: ${TODOTXT_BACKUP_COMMAND:=writebackup}


if [ "$TODOTXT_BACKUP_COMMAND" = 'none' ]; then
# Skip backup.
:
elif type -P "$TODOTXT_BACKUP_COMMAND" >/dev/null; then
if [ -d "${TODOTXT_BACKUP_DIR%/}" ]; then
"$TODOTXT_BACKUP_COMMAND" -d "${TODOTXT_BACKUP_DIR%/}" "$TODO_FILE"
else
"$TODOTXT_BACKUP_COMMAND" "$TODO_FILE"
fi
else
readonly timestamp=$(date +%F_%H%M%S)
readonly backupFilename="todo_${timestamp}.bak"
if [ -d "${TODOTXT_BACKUP_DIR%/}" ]; then
cp "$TODO_FILE" "${TODOTXT_BACKUP_DIR%/}/${backupFilename}" && echo "Backed up to ${TODOTXT_BACKUP_DIR%/}/${backupFilename}"
else
cp "$TODO_FILE" "$backupFilename" && echo "Backed up to ${backupFilename}"
fi
fi

0 comments on commit c56b74b

Please sign in to comment.