Skip to content

Instantly share code, notes, and snippets.

@delphym
Last active November 7, 2018 01:50
Show Gist options
  • Save delphym/62e99ef7ba936879580c66c6a9685ef3 to your computer and use it in GitHub Desktop.
Save delphym/62e99ef7ba936879580c66c6a9685ef3 to your computer and use it in GitHub Desktop.
Some handy shell and bash scripts for backups and other system maintanance
#!/bin/bash
## -i for interactive to allow pass the shell aliases to the bash subproces.
# Script origin @ https://gist.github.com/delphym/62e99ef7ba936879580c66c6a9685ef3#file-backup-sh
#set -x
LOG_LOCATION=/var/log/sbackup/
LOG_FILE=`basename "$0"`
LOG_FILE=`echo ${LOG_FILE%%.*}`
LOG_NAME=${LOG_LOCATION}${LOG_FILE}.log
BACKUP_BASE=/mnt/backups_and_builds/config/OS
echo2 () {
echo -e $@ | awk '{$1=$1;print}' | tee -a ${LOG_NAME}
}
if [ ! -d $LOG_LOCATION ]; then
mkdir -p $LOG_LOCATION
fi
# It makes sure the target directory exists and perform an archive copy of required files.
copyConf () {
if [ ! -d $2 ]; then
mkdir -p $2
fi
if [ $# = 3 ] && [ $3 = "toor" ]; then
/bin/cp -va $1 $2
else
if [ $# = 3 ] && [ $3 = "cron" ]; then
crontab -l -u $1 > /tmp/scheduler && mv /tmp/scheduler $2/$1;
test ${PIPESTATUS[0]} -eq 0 && echo2 "############## keeping CRON for $1 #########################"
else
cp -a $1 $2
fi
fi
}
# It proces user's home dir and selectivelly backup their configuration
# Regexps are horrible to use and even worse to understand them. I know....
# The stuff separeted by PIPE '|' are what ALL is included in the backup
# The stuff after EXCLAMATION '!' embaced by brackets is what is excluded from the backup
copyUserConf () {
echo2 -n "#### archiving a content of $1 ### "
find $1/ -mindepth 1 -prune -regextype posix-egrep -regex "$1/(\..*|.*~.*|.*bak|bin|etc|lib|plugins|gitolite-admin)" ! \( -name '*.swp' -o -name '.*cache' -o -name '*.lock' -o -iname '.android*' \) -exec bash -c 'copyConf "$@"' bash '{}' $BACKUP_BASE$1 toor \;
echo2 " SIZE is: `du -hs $BACKUP_BASE$1 | cut -f1` #####"
}
export -f copyConf
export -f copyUserConf
echo2 "####################################################"
echo2 "This is the backup script logger for"
echo2 "Backup script: $0"
echo2 "Logging into: $LOG_NAME"
echo2 "Start time:" `date`
echo2 "Executed by $SUDO_USER as $(whoami)"
echo2 "Its PID: $$"
echo2 "===================================================="
# SMTP Srv
echo2 "Backuping SMT conf"
copyConf /etc/postfix $BACKUP_BASE/SMTP/
postconf > $BACKUP_BASE/SMTP/postfix-maildrv.conf
# APT conf
echo2 "Backuping APT (packaging) conf"
copyConf /etc/apt/sources.list $BACKUP_BASE/APT/
apt list > $BACKUP_BASE/APT/pkg-list.txt
# local bin
echo2 "Backuping 'local bin' scripts"
copyConf /usr/local/bin $BACKUP_BASE/usr/local/
# ETC. It needs root's permissions, so that's why it's passing the 3rd param.
# See sudo visudo for the whole picture.
echo2 "Backuping sys 'etc' dir"
copyConf /etc/ $BACKUP_BASE/ toor
# Users' HOMEs configs files, etc.
echo2 "Backuping USERs HOME conf"
for user in /home/* ; do
copyUserConf $user
copyConf `echo $user | cut -d / -f3` $BACKUP_BASE/var/spool/cron cron
done
copyUserConf /root
copyConf root $BACKUP_BASE/var/spool/cron cron
echo2 "The whole configuration backup has completed."
echo2 "Finish time:" `date`
echo2 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
#!/bin/bash
# Script origin @ https://gist.github.com/delphym/62e99ef7ba936879580c66c6a9685ef3#file-backup2cloud-onremote-sh
## On remote:
# screen -ls
# screen -S backup -r
## On localhost
ssh daniel@mcs-ubuntu 'screen -dmLS backup sh -c "sudo grive --upload-only --dir backups_and_builds --path /mnt/; exec bash"'
#!/bin/sh
# Script origin @ https://gist.github.com/delphym/62e99ef7ba936879580c66c6a9685ef3#file-backup2cloud-sh
LOG_LOCATION=/var/log/sbackup/
LOG_FILE=`basename "$0"`
LOG_FILE=`echo ${LOG_FILE%%.*}`
LOG_NAME=${LOG_LOCATION}${LOG_FILE}.log
BACKUP_BASE=/mnt/backups_and_builds/config/OS
alias echo2='echo >> ${LOG_NAME}'
if [ ! -d $LOG_LOCATION ]; then
mkdir -p $LOG_LOCATION
fi
echo2 "####################################################"
echo2 "This is the backup script logger for"
echo2 "Backup script: $0"
echo2 "Logging into: $LOG_NAME"
echo2 "Start time: " `date`
echo2 "Executed by $SUDO_USER as $(whoami)"
echo2 "Its PID: $$"
echo2 "===================================================="
# Backup to cloud
echo2 "Backuping to cloud starting at "`date`
## in order to keep the log file, follow advices from
## https://superuser.com/questions/766903/how-to-set-the-gnu-screen-log-file-name-non-interactively
cp ~/.screenrc /tmp/
echo "logfile screeenlog-backupSession.log" >> /tmp/.screenrc
time screen -c /tmp/.screenrc -DmLS backupSession sh -c "sudo grive --upload-only --dir backups_and_builds --path /mnt/; "
rm /tmp/.screenrc
cat screeenlog-backupSession.log >> ${LOG_NAME}
#rm screeenlog-backupSession.log
echo2 "Backuping to cloud finished at "`date`
echo2 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
#!/bin/sh
sudo fdisk /dev/sdb -l
#Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 sectors
#Units: sectors of 1 * 512 = 512 bytes
#Sector size (logical/physical): 512 bytes / 512 bytes
#I/O size (minimum/optimal): 512 bytes / 512 bytes
#Disklabel type: gpt
#Disk identifier: 04361FB4-3666-4BD1-8D46-6609C315D81B
#Device Start End Sectors Size Type
#/dev/sdb1 2048 1023999 1021952 499M EFI System
#/dev/sdb3 1024000 1286143 262144 128M Microsoft reserved
#/dev/sdb3 1286144 990218239 988932096 471.6G Microsoft basic data
#/dev/sdb4 990218240 1000214527 9996288 4.8G Windows recovery environment
sudo sfdisk -d /dev/sdb | sudo sfdisk /dev/sdc
sudo dd if=/dev/sdb1 bs=512 count=1021952 conv=sync,noerror status=progress | sudo dd of=/dev/sdc1
sudo dd if=/dev/sdb2 bs=512 count=262144 conv=sync,noerror | sudo dd of=/dev/sdc2
sudo dd if=/dev/sdb3 bs=2048 count=247233024 conv=sync,noerror | sudo dd of=/dev/sdc3
sudo dd if=/dev/sdb4 bs=2048 count=2499072 conv=sync,noerror status=progress | sudo dd of=/dev/sdc4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment