Skip to content

Instantly share code, notes, and snippets.

@RyanTheTechMan
Last active April 10, 2022 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanTheTechMan/a56ae885909be1c6babd51d98aa8c9d7 to your computer and use it in GitHub Desktop.
Save RyanTheTechMan/a56ae885909be1c6babd51d98aa8c9d7 to your computer and use it in GitHub Desktop.
A script that will backup all files located in the current directory to a backup directory. Useful for school projects. Run "sh ./backup-setup.sh" to install. Then restart your terminal and now run "backup" to backup your project.
echo "[EZBackup] Installing..."
cat > ~/backup.sh <<- EOM
#!/bin/bash
function backup() {
if [ \$PWD = \$HOME ]; then
echo "[EZBackup] ERROR: Your current directory is your home directory. Please change to a different directory before running this script."
return 1
fi
if [ \${PWD##*/} = "backups" ]; then
echo "[EZBackup] ERROR: Your current directory is the backups directory. Please change to a different directory before running this script."
return 1
fi
doBackup=false
if [ ! -d "./backups" ]; then
echo -e "[EZBackup] '\${PWD##*/}' has never been backed up!\n[EZBackup] Would you like to backup '\${PWD##*/}' now? (y/n)"
read answer
if [[ \$answer = "y" || \$answer = "Y" ]]; then
doBackup=true
else
echo "[EZBackup] Canceled backup."
return 1
fi
else
doBackup=true
fi
if [ \$doBackup ]; then
folder=\$(date +"%Y-%m-%d-%T") # folder name is the current date and time
mkdir -p backups # create the "backups" folder if it doesn't exist
mkdir -p backups/\${folder} # create the folder for the current backup
cp *.* backups/\${folder} # copy all files and folders to the backup folder
echo "[EZBackup] Backup complete. Saved to ./backups/\${folder}/"
fi
}
EOM
echo "\nsource ~/backup.sh" >> ~/.bashrc
echo "[EZBackup] Done! Please restart your terminal and run 'backup' to start backing up your project."
rm -- "$0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment