Skip to content

Instantly share code, notes, and snippets.

@anwas
Last active October 13, 2022 20:05
Show Gist options
  • Save anwas/7a089d8f8ce040dbacfd1d4f076010c3 to your computer and use it in GitHub Desktop.
Save anwas/7a089d8f8ce040dbacfd1d4f076010c3 to your computer and use it in GitHub Desktop.
Linux command line – CLI #chmod #archive #compress #delete #backup
#!/bin/bash
#
# Резервное копирование каталогов и файлов из домашнего каталога
# Этот командный скрипт можно автоматически запускать при помощи cron
#
DATE=`date +%F` # This Command will add date in Backup File Name.
TIME=`date +%H-%M-%S` # This Command will add time in Backup File Name.
COMPNAME=EB
DESDIR=/home/anwas/clouds/nextCloud-eb/personal-share/backups_srv # Destination of backup file.
# DESDIR=/media/Data/www/tests
FULL_PATH=0 # archyve kurti nepilną kelią
while getopts ":d::c" opt; do
case $opt in
d)
DESDIR="${OPTARG}" >&2
;;
c)
FULL_PATH=1 >&2
echo "Pasirinktas FULL PATH"
;;
\?)
echo "Netinkamas parametras: -${OPTARG}" >&2
echo "Galima nenurodyti parametro arba turi būti: -d /direktorija_kur_išsaugoti"
exit 1
;;
:)
echo "Parametrui -${OPTARG} reikia argumento. Pvz.: -d /direktorija_kur_išsaugoti" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ ! -d "$DESDIR" ]
then
mkdir -p "$DESDIR"
fi
if [ $# -eq 0 ] ; then
echo "Nurodyk bent vieną failą ar aplanką."
echo "Komandos pavyzdys:"
echo "$0 aplankas1 aplankas2 failas2"
echo "Arba su parametru kur išsaugoti:"
echo "$0 -d /direktorija_kur_išsaugoti archyvuojamas_aplankas1 archyvuojamas_failas2"
exit 1
else
nerasta_kiek=0
sugadintas_kiek=0
kiek=0
mydirs=("$@") # archyvas iš cli parametrų
echo
for dir in "${mydirs[@]}" ; do
if [ -L "$dir" ]; then
echo "$dir yra SIMBOLINĖ NUORODA"
continue
elif [[ -d "$dir" || -f "$dir" ]]; then
DIR_PATH="$(dirname "$dir")"
TMP_PATH="/tmp"
base_name="$(basename "$dir")"
archive_name="${DATE}__${base_name// /_}__BCKP_${TIME}__${COMPNAME}.tar.gz"
echo "VYKDOMA: ${archive_name}"
if [ $FULL_PATH -gt 0 ]; then
tar czf ${TMP_PATH}/${archive_name} "${dir}"
else
tar czf ${TMP_PATH}/$archive_name -C "$DIR_PATH" "$base_name"
fi
tar -tzf "${TMP_PATH}/$archive_name" >/dev/null # patikriname archyvą
if [ $? = 0 ] ; then
SIZE=$(du -sb "${TMP_PATH}/$archive_name" | awk '{s+=$1} END{print (s/1024/1024), MB}')
sukurta[$kiek]="$archive_name"
((kiek++))
if [[ -f "$dir" ]]; then
mv "${TMP_PATH}/$archive_name" "$DESDIR"
else
if [ ! -d "${DESDIR}/${base_name// /_}" ]; then
mkdir -p "${DESDIR}/${base_name// /_}"
fi
mv "${TMP_PATH}/$archive_name" "$DESDIR/${base_name// /_}"
fi
echo "SUKURTA: $kiek iš ${#mydirs[@]}. Dydis: ${SIZE}MB"
else
sugadinti[$sugadintas_kiek]="$archive_name"
((sugadintas_kiek++))
fi
else
nerasta[$nerasta_kiek]="$dir"
((nerasta_kiek++))
fi
done
fi
if [ $kiek -lt 0 -a $nerasta_kiek -lt 0 ] ; then
echo "$TIME – KLAIDA ($?) atsarginio kopijavimo metu."
exit 1
fi
if [ $kiek -gt 0 ] ; then
echo
echo "/***************************************/"
echo "/** SUKURTA **/"
echo "/***************************************/"
echo
for i in "${sukurta[@]}"; do
echo "SUKURTA: $i"
done
echo
echo
fi
if [ $nerasta_kiek -gt 0 ] ; then
echo
echo "/***************************************/"
echo "/** !!! NERASTA !!! **/"
echo "/***************************************/"
echo
for i in "${nerasta[@]}"; do
echo "NERASTAS: $i"
done
echo
echo
fi
if [ $sugadintas_kiek -gt 0 ] ; then
echo
echo "/***************************************/"
echo "/** !!! SUGADINTI archyvai !!! **/"
echo "/***************************************/"
echo
for i in "${sugadinti[@]}"; do
echo "SUGADINTA: $i"
done
echo
echo
fi
exit 1
#!/bin/bash
# new from: https://docs.expressionengine.com/latest/installation/installation.html
find dir/path/from/where/search \( -type d -exec chmod 755 {} \; \) -o \( -type f -exec chmod 644 {} \; \)
# To recursively give directories read&execute privileges:
find /path/to/base/dir -type d -exec chmod 755 {} +
# To recursively give files read privileges:
find /path/to/base/dir -type f -exec chmod 644 {} +
# Or, if there are many objects to process:
chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
# Or, to reduce chmod spawning:
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644
#!/bin/bash
## Compress files
tar -czvf archive-filename.tar.gz ./
## Uncompress tar.gz
# The below does both the uncompressing and untarring in a single command and puts the contents in the same directory you are in:
tar zxvf file.tar.gz
# The z argument basically does the gunzip work for you.
## Uncompress tar.gz into different directory
# Use the -C argument to specify the path to place the files:
tar zxvf file.tar.gz -C /path/to/somedirectory
## Uncompress first, untar second
# When you uncompress first using gunzip, it will strip the .gz file extension from the file leaving you with a .tar file:
gunzip file.tar.gz
tar xvf file.tar
## Uncompress tar.bz2
# You might also encounter a bz2 file that you need to uncompress and untar:
tar xvjf file.tar.bz2
## Common Tar Arguments
# With tar some of the arguments we used above mean the following:
# c - create
# x - extract
# v - verbose output
# j - filter the archive through bzip2
# f - read from a file
# z - filter the archive through gzip
#!/bin/bash
cd /web
tar xvfz web{TAB} ./web/
cd web
(shopt -s dotglob; mv -- * ..)
cd ..
rmdir web
rm web{TAB}
ls -lah
#!/bin/bash
### mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]
### gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]
mysqldump -h 127.0.0.1 -u root -p --add-drop-table dbaivaras_example > dbaivaras_example_2016-06-21_19-17.sql
mysqldump -h 127.0.0.1 -u root -p --add-drop-table dbaivaras_example | gzip -9 > dbaivaras_example_2016-06-21_19-17.sql.gz
mysql -h 127.0.0.1 -u root -p dbaivaras_example < dbaivaras_example_2016-06-21_19-17.sql
gunzip < dbaivaras_example_2016-06-21_19-17.sql.gz | mysql -u root -p dbaivaras_example
# There is more than one way to do this, however I tend to do it like this from command prompt:
## Uncompress tar.gz
# The below does both the uncompressing and untarring in a single command and puts the contents in the same directory you are in:
tar zxvf file.tar.gz
# The z argument basically does the gunzip work for you.
## Uncompress tar.gz into different directory
# Use the -C argument to specify the path to place the files:
tar zxvf file.tar.gz -C /path/to/somedirectory
## Uncompress first, untar second
# When you uncompress first using gunzip, it will strip the .gz file extension from the file leaving you with a .tar file:
gunzip file.tar.gz
tar xvf file.tar
## Uncompress tar.bz2
# You might also encounter a bz2 file that you need to uncompress and untar:
tar xvjf file.tar.bz2
## Common Tar Arguments
# With tar some of the arguments we used above mean the following:
# x - extract
# v - verbose output
# j - filter the archive through bzip2
# f - read from a file
# z - filter the archive through gzip
#!/bin/bash
# Recursively delete folder or file by name
find . -type d -name .metadata -exec rm -rf {} \;
find . -type f -name .what-is-the-filename -exec rm -rf {} \;
# Panaikinti visus JSON failus
find . -type f -name '*.json' -exec rm -rf {} \;
# Įdiegti sshfs
sudo apt install sshfs
# Gauti GROUP ID
getent group GROUP_NAME | awk -F: '{printf "Group %s with GID=%d\n", $1, $3}'
# Užmontuoti nuotolinį aplanką
sshfs -o idmap=user -o gid=GROUP_ID -p PORT_NUMBER USER_NAME@IP_ADDRESS:/var/www/ /home/USER_NAME/www_sshfs/
# Numontuoti nuotolinį aplanką
sudo umount /home/USER_NAME/www_sshfs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment