Skip to content

Instantly share code, notes, and snippets.

@CodeWire
Last active December 20, 2023 04:06
Show Gist options
  • Save CodeWire/7d82fc521146f4d27348cffcabc8ab8e to your computer and use it in GitHub Desktop.
Save CodeWire/7d82fc521146f4d27348cffcabc8ab8e to your computer and use it in GitHub Desktop.
Backup & Restore Ubuntu Server The Simple Way

How I Backup My Ubuntu Server & Restore To Another Ubuntu Instance

How to backing up Ubuntu server and restoring to another Ubuntu instance

It is adviced to make TWO backups. 1 for / and 1 for /home if they are on different partition. If you want one you will have to add a lot of exceptions to that one command. Example:

1. Open Terminal

sudo -i cd /

tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system / tar -cvpzf backuphome.tar.gz --one-file-system /home/

What did you just run with that code?

For anyone not familiar with backup up Ubuntu server in terminal here is a little more information. To get the best backup without failing due to permissions you want your root and exclude ALL mounted partitions, like `/media` (you do not want to backup external devices (you really do not)) and you absolutely do NOT want to backup something like `/dev` or `/proc`. Then `/home` goes to another backup.

In above method the backup are stored in `/`. It will be bettter to store it on an external media; you can then put a directory in front of the backup.tar.gz and drop the `--exclude=...` from the 1st command.

backup.tar.gz is the backup The --exclude will prevent the actual backup getting backed-up.

Alternative is backing up just the root project folder like www public_html `tar -czvf my_directory.tar.gz my_directory'

2. Backup all the SQL databases

mysqldump -u root -p --all-databases > all_dbs.sql

cvpzf: create, verbose, preserve permissions, compress, use a file. one-file-system Do not include files on a different filesystem. If you want other filesystems, such as a /home partition, or extermedia mounted in /media backed up, you either need to back them up separately, or omit this flag. If you do omit tflag, you will need to add several more --exclude= arguments to avoid filesystems you do not want. These would/proc, /sys, /mnt, /media, /run and /dev directories in root. /proc and /sys are virtual filesystems that provwindows into variables of the running kernel, so you do not want to try and backup or restore them. /dev is a tmwhose contents are created and deleted dynamically by udev, so you also do not want to backup or restore it. Likewi/run is a tmpfs that holds variables about the running system that do not need.

cd /

tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude=/proc --exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys / 

3. Run SCP On Local Machine

Simply use the scp command on the Mac, like this: scp username@192.168.1.111/backup.tar.gz /local/path/to/myfile.txt. You may also just use FileZilla which is a graphical client. Connect to your Ubuntu with a URL like sftp://192.168.1.111, of course you need to use the valid IP address.

scp username@192.168.1.111/backup.tar.gz /MAC/Users/home/Sites/Backups/P/Pienode.com/backup.tar.gz

4. Restoring The Backup File

cd /
tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude=/proc 
--exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys / 

5. Import Database

mysql -u root -p < all_dbs.sql

I did not use sudo in front of the tar command since you will get error messages regarding permissions (because of files owned by root).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment