-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdobackup.sh
executable file
·89 lines (82 loc) · 2.53 KB
/
dobackup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /bin/csh -f
#
# dobackup
#
# Make a copy of the specified files (in the current directory) into
# the ./backup subdirectory. The backedup files are marked read-only.
#
# Example:
# dobackup *.cxx *.h
#
# TODO:
# - make the setting of the read-only bit optional
# - figure out how to handle spaces in filenames with foreach
# - handle filenames starting with dashes (eg, -asciilatex.error)
# - handle filenames containing $'s (e.g., ~$exam-2.docx)
#
# Uncomment (or comment) the following for enabling (or disabling) tracing
## set echo=1
# Complain if the command-line is wrong
## TODO2: add --verbose
set allow_relative_backup = 0
set rel_backup_arg = "--rel-backup"
if ("$1" == "") then
set script = `basename "$0"`
echo "Format: dobackup [$rel_backup_arg] file_spec ..."
echo ""
echo "example:"
echo ""
echo "$0 *.perl"
echo ""
echo "$script $rel_backup_arg main.py tests/test_main.py"
echo ""
exit
endif
if ("$1" == "--rel-backup") then
set allow_relative_backup = 1
shift
endif
# Make sure that the backup directory exists
# NOTE: if the old-style BACKUP directory exists, it is used
# and a symbolic link is created
# TODO: make BACKUP the symbolic link instead
if (! -e ./backup/. ) then
if (-e ./BACKUP) then
ln -s BACKUP backup
else
mkdir ./backup
endif
endif
# Copy each file to the backup directory and set to read-only
# TODO: handle files with embedded spaces (e.g., for chmod processing)
while ("$1" != "")
set fil = "$1"
set dir = "."
set backup_dir = "./backup"
if ("$allow_relative_backup" == "1") then
set dir = `dirname "$1"`
set backup_dir = "$dir/backup"
mkdir -p "$backup_dir"
endif
if (! -d "$fil") then
# Output current file to backup (TODO: only do in verbose mode)
## TODO: set basefil = "`basename ""$fil""`"
set basefil = `basename "$fil"`
if (-e "$dir/$basefil") then
echo "Backing up '$fil' to '$backup_dir/$basefil'"
else
echo "Warning: not backing up '$fil' to '$backup_dir/$basefil'; try $rel_backup_arg"
endif
# Make existing version of backup file writable
## OLD: echo "Backing up '$fil' to '$backup_dir/$basefil'"
## OLD: if (-e "$backup_dir/$basefil") chmod u+w "$backup_dir/$basefil"
if (-e "$backup_dir/$basefil") then
chmod u+w "$backup_dir/$basefil"
endif
# Copy the file and remove write access
# TODO: just issue error message if file doesn't exist (i.e., don't run cp and chmod commands)
cp -p "$fil" "$backup_dir"
chmod ugo-w "$backup_dir/$basefil"
endif
shift
end