Skip to content

Instantly share code, notes, and snippets.

@yusufb
Last active March 31, 2016 17:33
Show Gist options
  • Save yusufb/b53fd6ae5847ad87c6327a002b07dccd to your computer and use it in GitHub Desktop.
Save yusufb/b53fd6ae5847ad87c6327a002b07dccd to your computer and use it in GitHub Desktop.
backup files before diving into code
#!/bin/sh
if [ $# -eq 0 ]; then
echo "filename(s) to backup is missing. exiting."
exit 1
fi
BACKUP_DATE=`date +%Y-%m-%d_%H:%M:%S`
BACKUP_EXT_PREFIX="backup"
for file in "$@"
do
if [ ! -f $file ]; then
echo "\"$file\" does not exist. skipping."
continue
fi
NEW_FILENAME="$file.$BACKUP_EXT_PREFIX.$BACKUP_DATE"
cp $file $NEW_FILENAME
echo "\"$file\" has been backed up."
FILES_TO_LIST="$FILES_TO_LIST $file* "
done
if [ ${#FILES_TO_LIST} -lt 1 ]; then
echo "no files have been backed up."
else
ls -a $FILES_TO_LIST
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment