Linux commands

From Group-Office Groupware and CRM Documentation
Jump to: navigation, search

restore from backup with rsync

rsync -navu foldertorestore

-n is to test

Find all symbolic links to a specific directory

find -maxdepth 4 -type l -exec stat {} \; | grep /usr/local/trunk

Remove all .svn directories except for the actual theme dirs

find ! -path "*/ThemeName/*" -name .svn -type d -exec rm -Rf {} \;

Migrate maildirs from Courier to Dovecot

Got from

Old structure of maildirs was domain/user/.maildir I needed to convert that to domain/user with the script below

./ --to-dovecot --recursive --convert domaindirectory
for f in `find -name .maildir`; do
  PARENT=`dirname $f`
  mv $f/* $f/.[^.]* $PARENT

Bulk rename files

Rename all en. to et. in files down the tree with find:

find -type f -exec rename -v -n 's/en\./et\./' {} \;


Copy a large amount of data to another server

This is faster then rsync because it doesn't do any file analysis:

tar cf - filesORdir | ssh HOSTorIP "(cd /where/to/copy; tar xf -)"

Bash Script to migrate a linux system to a fresh system



#Set to true to migrate the users above uid 500

#Adjust sources to transfer
SOURCES="/etc/ssh/ /etc/ssh/ssh_host_dsa_key \
 /etc/ssh/ssh_host_rsa_key /etc/ssh/ \
 /root/passwd.mig /root/shadow.mig /root/group.mig /quota.user \
 / /home/ /var/www/ /var/lib/mysql/ /vmail/ \
 /usr/share/groupoffice/ /usr/share/www/ \
 /usr/local/kringloopplanner/ /etc/postfix/ /etc/dovecot/ \ 
 /etc/apache2/ /etc/groupoffice/ /etc/mailname"

export UGIDLIMIT=500

awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/passwd.mig

awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > /root/group.mig

awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/shadow.mig

for source in $SOURCES; do
#-H is very important to preserve hard links!
	rsync -vaH -e "ssh -i /root/.ssh/id_rsa" $source $TARGET:$source

if [ $MIGRATEUSERS -eq 1 ]; then
	ssh -i /root/.ssh/id_rsa $TARGET "cat /root/passwd.mig >> /etc/passwd"
	ssh -i /root/.ssh/id_rsa $TARGET "cat /root/shadow.mig >> /etc/shadow"
	ssh -i /root/.ssh/id_rsa $TARGET "cat /root/group.mig >> /etc/group"

Delete specific files with find

Backup the files:

find -maxdepth 3 -type f -name default.sieve -size 18c -exec cp -v --parents '{}' /root/sievebak/ ';'

Backup the files:

find -maxdepth 3 -type f -name default.sieve -size 18c -exec rm '{}' ';'

Delete mail from specific sender

Show active sending domains:

qshape -s active

Delete from single domain:

postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /@example\.com/ { print $1 }' | tr -d '*!' | postsuper -d -

Delete from user:

postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /username@example\.com/ { print $1 }' | tr -d '*!' | postsuper -d -