Linux commands

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

restore from backup with rsync

rsync -navu foldertorestore user@domain.com:/path/to/parent/of/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 courier-dovecot-migrate.pl from http://wiki.dovecot.org/Migration/Courier

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

#!/bin/bash
./courier-dovecot-migrate.pl --to-dovecot --recursive --convert domaindirectory
for f in `find -name .maildir`; do
  PARENT=`dirname $f`
  mv $f/* $f/.[^.]* $PARENT
done


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\./' {} \;

source: http://tips.webdesign10.com/how-to-bulk-rename-files-in-linux-in-the-terminal

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

#!/bin/bash

TARGET=root@freshHostNameOrIP

#Set to true to migrate the users above uid 500
MIGRATEUSERS=1 

#Adjust sources to transfer
SOURCES="/etc/ssh/ssh_host_dsa_key.pub /etc/ssh/ssh_host_dsa_key \
 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_rsa_key.pub \
 /root/passwd.mig /root/shadow.mig /root/group.mig /quota.user \
 /quota.group /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
done

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"
fi


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 -