Skip to content

Instantly share code, notes, and snippets.

@akyag
Created October 22, 2020 06:57
Show Gist options
  • Save akyag/1342cd70ef06fa5334d139eeabb2bbc5 to your computer and use it in GitHub Desktop.
Save akyag/1342cd70ef06fa5334d139eeabb2bbc5 to your computer and use it in GitHub Desktop.
Daily backup script
#!/bin/zsh
SHELL=/bin/zsh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
set -ex
# Replacing daily python script with this one since the tarfile cant use zst correctly yet.
echo "Execution time:" $(date +%Y%m%d/%H:%M:%S)
echo "-----------------------------"
date=$(date +"%Y%m%d")
########################################
######## Daily stuff ########
########################################
src="<path>"
dest="<path>"
myfile="$date--daily"
function compress-daily(){
if [ -e $dest/"$myfile".tar.zst.gpg ];
then
echo ""$myfile".gpg already exists."
echo "-----------------------------"
else
tar -cp -X $src/daily-exclude -T $src/daily-source > $dest/"$myfile".tar
echo "Tar file ready - "$myfile".tar"
zstd -c -T0 -15 $dest/"$myfile".tar -o $dest/"$myfile".tar.zst && mv $dest/"$myfile".tar /var/tmp
echo "Zstandard compression applied, tar file removed."
gpg2 -es -r akshay $dest/"$myfile".tar.zst && mv $dest/"$myfile".tar.zst /var/tmp
echo "Archive encrypted, daily backup done."
echo "-----------------------------"
fi
}
compress-daily
########################################
######## Firefox ##########
########################################
src="<path>h"
dest="<path>"
myfile="$date--firefox-profile"
function compress-firefox(){
if [ -e $dest/"$myfile".tar.zst.gpg ];
then
echo ""$myfile".gpg already exists."
echo "-----------------------------"
else
cp -r -p <path> <path>
tar -cp -X $src/firefox-exclude /home/akya/mozilla > $dest/"$myfile".tar
rm -r /home/akya/mozilla
echo "Tar file ready - "$myfile".tar"
zstd -c -T0 --ultra -20 $dest/"$myfile".tar -o $dest/"$myfile".tar.zst && mv $dest/"$myfile".tar /var/tmp
echo "Zstandard compression applied, tar file removed."
gpg2 -es -r akshay $dest/"$myfile".tar.zst && mv $dest/"$myfile".tar.zst /var/tmp
echo "Archive encrypted, firefox profile backed up."
echo "-----------------------------"
fi
}
compress-firefox
########################################
######### Keepass DB #########
########################################
src="<path>"
dest="<path>"
myfile="$date--root.kdbx"
function keepass-database(){
if [ -e $dest/"$myfile".gpg ];
then
echo ""$myfile".gpg already exists."
echo "-----------------------------"
else
gpg2 -es -r akshay -o $dest/"$myfile".gpg $src/root.kdbx
echo "root.kdbx backed up in mega."
echo "-----------------------------"
fi
}
keepass-database
########################################
######### Keepass Cleanup #######
########################################
# Keep latest 30 files in Mega keepass sync folder
cd <path>
ls -tp | grep -v '/$' | tail -n +31 | xargs -d '\n' -r rm --
echo "Older keepass backups removed."
echo "-----------------------------"
########################################
########## Important stuff ########
########################################
cd <path>
src="<path>"
dest="<path>"
myfile="$date--important"
function important-stuff(){
if [ -d ./code ] && [ -d ./notes ];
then
echo "Directories exist, they will be removed."
rm -r <path> <path> <path> <path>
echo "-----------------------------"
else
echo "Directories do not exist. They will be created."
echo "-----------------------------"
fi
}
important-stuff
cp -r -p <path> <path>
cp -r -p <path> <path>
cp -r -p <path> <path>
cp -r -p <path> <path>
cp -p <path> <path>
cp -p <path> <path>
cp -p <path> <path>
function compress-important(){
if [ -e $dest/"$myfile".tar.zst.gpg ];
then
echo ""$myfile".gpg already exists."
echo "-----------------------------"
else
tar -cp <path> > $dest/"$myfile".tar
echo "Tar file ready - "$myfile".tar"
zstd -c -T0 -15 $dest/"$myfile".tar -o $dest/"$myfile".tar.zst && mv $dest/"$myfile".tar /var/tmp
echo "Zstandard compression applied, tar file removed."
gpg2 -es -r akshay $dest/"$myfile".tar.zst && mv $dest/"$myfile".tar.zst /var/tmp
echo "Archive encrypted, important stuff backed up."
echo "-----------------------------"
fi
}
compress-important
########################################
######## Mega backup ######
########################################
src="<path>"
dest="<path>"
# Removing older backups from Mega sync folder
cd $dest
ls -tp | grep -v '/$' | xargs -d '\n' -r rm --
echo "Removed old backups from Mega sync folder"
echo "-----------------------------"
cd $src
# Removing older backups from backup-daily on External HDD
ls -tp | grep -v '/$' | tail -n +22 | xargs -d '\n' -r rm --
echo "Removed old backups from backup-daily on External HDD, Kept latest 21 (3 each for 1 week)"
echo "-----------------------------"
# Copying latest backups to Mega sync folder
ls -tpr | grep -v '/$' | tail -n -3 | xargs -d '\n' -r cp -t "$dest"
echo "Copied all latest backups (3 for current day) to mega sync."
echo "-----------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment