Last active
April 10, 2022 22:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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