Backup linux server
Posted in Examples, Q&A on September 13th, 2010 by cmanolescu – 1 CommentA simple way to backup your server.
Backup mysql database:
mysqldump -u $mysqlUser -p$mysqlPassword database > dump.sql
or you can dump all databases to a single sql file
mysqldump -u $mysqlUser -p$mysqlPassword --all-databases > dump.sql
Backup postgresql database:
pg_dumpall -U user > dump.sql
but because this will execute on server there will be no user interaction so for this to work you need to create a .pgpass file in your home directory.
NOTE: make sure it has permission 600 or else it will be ignored.
cat > ~/.pgpass << ^D host:port:*:user:password ^D chmod 600 ~/.pgpass
You may need a remote server where to store your backup and you also want to connect without providing a password (a good tutorial can be found here).
NOTE:
a) make sure ~/.ssh exists on remote server and it has permissions 700
b) use “-p port” if ssh is not on the default port
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub "-p port remoteUser@remoteHost"
An easy way to backup your files on a remote server is to use rsync
rsync -e "ssh -p remotePort" -avrp localPath remoteUser@remoteHost:remotePath
Create cron job
cat > /etc/cron.weekly/remote_backup << ^D #!/bin/sh ######### Start backup script ######### ######### End backup script ########## ^D chmod 700 /etc/cron.weekly/remote_backup
Backup a folder to a tar archive
tar zcvf /archive/etc.tgz /etc/
or backup the entire server
tar -zcvpf /archive/full-backup-`date '+%d-%B-%Y'`.tar.gz \
--directory / --exclude=archive --exclude=mnt --exclude=proc --exclude=var/spool/squid
Download complete backup script
